views:

887

answers:

2

I am doing a project where user can upload images like profile image or image for their image gallary. right now its uploading all the images to my server.

Now, i want to upload all this image to my rackspace cloud files directly using php script. For example:

  1. user select a file
  2. press submit with some information
  3. the selected file will be uploaded to the rackspace server and return the file location.
  4. then the file location along with other information will be saved in my database.
  5. then i will show the file or image from that location.

so do u have any idea, exactly how i can do this?

i am using:

  • Codeigniter framework
  • jQuery as javascript library

thanks in advance for any answer.

+2  A: 

You cannot load files -directly- to Rackspace Files, without trying something like embedding your Rackspace credentials into the form. Even if this were possible it would not be a good idea. (i.e. the user could add massive content using your credentials that have nothing to do with your application). So to get what you want, which is essentially to have the file stored at Rackspace, and the location of the file stored in your server you need to modify your work flow. It needs to be:

  1. Form uploads file to -your server-
  2. -your server- calculated the container and filename that the file will use
  3. -your server- uploads the temporary file to rackspace files

If you want the file to be publically downloadable...

  1. -your server- enables the file to be served via the CDN, making a public URL for the file that your user can access.
  2. when your server creates html it embeds the CDN urls and the user magically gets the files

If you want the file to be only downloadable by -certain- users....

  1. -your server- authenticates with rackspace and downloads the file temporarily
  2. -your server- serves the file AND the HTML both from your server

You do not pay for bandwidth between -your server- and rackspace files, if -your server- is also a rackspace cloud instance or a regular Rackspace Managed Server (or at least that is what customer support has told me). An important fact when calculating the rates between Amazon and Rackspace.

So you can either use Rackspace Files as a massive hard-drive for your server, which gives you control over access, or you can use it as a massive public distribution network... But in both cases you might need to change the order of your steps to give you what you want...

You might also look at a simple example of using php to upload a file gathered from an HTML form to rackspace files.

ftrotter
A: 

from welcome.php:

    function index()
    {
            if(!empty($_FILES)){
                    require('system/libraries/cloudfiles.php');
                    $upload_dir = 'upload/doc/2';
                    $tmp_name = $_FILES['upload']['tmp_name'];
                    $name = $_FILES['upload']['name'];
                    move_uploaded_file($tmp_name,"$upload_dir/$name");
                    $username = 'ABC';
                    $key = 'X';
                    $auth = new CF_Authentication($username, $key);
                    $auth->authenticate();
                    $conn = new CF_Connection($auth);
                    $container = $conn->get_container('documents');
                    $localfile = "$upload_dir/$name";
                    $object = $container->create_object($name);
                    $object->load_from_filename($localfile);
            }
            $this->load->view('global_header');
            $this->load->view('welcome_message');
    }

from welcome_message.php:

<form method="post" action="#" enctype="multipart/form-data"s>
<input type="file" name="upload"><br/>
<input type="submit">
</form>

problems I encountered:

cacert.pem ~ This file needs to go in your web directory and comes with the api.

you may need fileinfo from pecl, and for this you may need libmagic (if you don't have it already).

you also need the package php5-curl , and php doesn't report curl errors, so dig into the api and add some output if it isn't working.

Orbit