views:

346

answers:

6

I have a PHP web-application that allows users to upload images to my web site. I'm doing this using a simply HTML <form enctype="multipart/form-data">

However, instead of having those images uploaded to my web server - I would to have those images uploaded directly to my CDN (Cachefly - which is another server).

Is this possible ... to have a web-application allow a user to upload images directly to another server?

In case it helps, here's my PHP code:

$target_path = "/home/www/example.com/uploads/";
$target_path = $target_path . basename( $_FILES['uploadedfile']['name']); 

if(move_uploaded_file($_FILES['uploadedfile']['tmp_name'], $target_path)) {
    // file has been uploaded **LOCALLY**
    // HOWEVER, instead of it being upload locally, I would like the file 
    // to be directly uploaded to the CDN ('other' server)
    ...
} else{
    // error: file did not get uploaded correctly
    ....
}
A: 

Sure.

Somewhere in your code there is a "$target_directory" variable that needs to be set. It won't be called that, but depeding on how your function is set up it needs to be there -somewhere-. Just use an absolute path for the directory you want the files to land in. Also, make sure that directory is CHMOD'd to 777 so it can be written into.

Post your code and I can help more.

Alex Mcp
I've included my code in the original post
Tim
+1  A: 

I don't think it's possible to directly upload to another server, but I could be wrong. I had a similar problem, and I used PHP's FTP capabilities (http://us3.php.net/manual/en/book.ftp.php). I still used my server as a middle-man, meaning I uploaded the files to my server, then FTP transferred them to the target server, and then deleted the file from my server.

Arms
+1  A: 

You could recieve it on your webserver and then transfer it to the CDN via fileshare or FTP.

If the CDN is web-facing, you could re-direct the request to that server and send control back to your webserver form once the file is uploaded. It's probably better to do the file transfer in the back end though and keep the user connected to the web server.

Kendrick
That's the who point of my question ... instead of having it upload to me and then I forward it to my CDN via FTP ... can I simply have the HTML FORM upload, upload directly to the CDN?
Tim
+4  A: 

i think in case of a CDN ... u will first have to receive files on ur server and then using the CDN API upload to their 'bucket'. i dont think u can upload directly to a CDN unless there is a way to map it as a directory on ur server.

Sabeen Malik
My CDN gives me FTP access to my fileshare on their servers. Does that make any difference?
Tim
Not really ... the process would be:1 : get the file uploaded on ur server.2 : then write a PHP script maybe to send batch or single files to the CDN via FTP in the background.
Sabeen Malik
So, the short answer is "No" - I cannot have an HTML FORM upload point to another server directly for the file upload. Is that correct?
Tim
no .. its not possible like that.and it has good reasons behind it .. for instance if the form was sending directly to the CDN .. how would u validate which file is ok and which isnt? secondly.. if u have 2 other text fields on the form .. how would u deal with them? so there must be a receiving script on the CDN .. which validates the data and then stores it and u already know how cant run a PHP/CGI script on the CDN .. its just storage.
Sabeen Malik
Another thing i realized from ur last comment .. when u say .. "another server" .. its a bit vague .. i was pointing specifically to the CDN case .. if this other server is in ur control .. then its very much possible .. u just simply change the form action and put to a receiving script on ur other server and deal with the data there as u please.
Sabeen Malik
the 'other server' is the CDN ... so no, I can't place a PHP script to run on the CDN to process the form. Like you said, the CDN is simply web storage to serve
Tim
so that settles it :)
Sabeen Malik
+2  A: 
  • Moving / Uploading a file to a service or for you non-direct-accesable server is usually done by using the provider's API
  • Moving / Uploading a file to a server 'owned' by yourself can be done by using PHP + FTP extensions (for more information: pear.php.net or pecl.php.net)
  • Moving / Uploading a file to a server 'owned' by yourself and being one of many in a cluster is usually done by uploading the file temporary on 1 server and afterwards a .sh, .bash or whatever is called which activates further transfer processes to another server.
daemonfire300
Cachefly, the 'other' server, provides me FTP access to upload the files to them. So, to make sure I understand correctly ... I would still upload the file to MY server first, and then FTP to Cachefly. So what you're saying is that I can't have the end user use the HTML form to directly upload to Cachefly
Tim
Is my understanding correct?
Tim
Right, I did not know any solution which allows you to leave the upload-on-your-server process out.
daemonfire300
A: 

Yes amazon web services already allows you to upload to amazon S3 directly from the user's browser: Documentation: http://doc.s3.amazonaws.com/proposals/post.html

Additionally that S3 bucket can be exposed via the amazon CDN (or any other cdn that can point to a customer's origin server)

Tim