phpimagecross-domainuploadingcookieless

Cross-domain saving file uploaded by user


I have a website example.com where people can upload images. But I want to have those images saved in my cookieless domain examplestatic.com. So, I wrote:

move_uploaded_file($_FILES['image1']['tmp_name'], $folder_temp .$image_path_1 );

where folder temp is: www.examplestatic.com/image_folder/big_images

However, I get an error that it can't move the file. I used to have it move the uploaded files to a folder in the same domain and it would work. But since I changed the directory of $folder_temp to the new static domain, it doesn't work anymore. How can I allow users to upload files cross-domain?

Thanks!


Solution

  • I found a solution to it:

    It's uploading the file via FTP using php. The code goes like this:

    $ftp_server = "server";
    $ftp_username  = "username";
    $ftp_password   =  "password";
    $connection = ftp_connect($ftp_server) or die("could not connect to $ftp_server");
    if(@ftp_login($connection, $ftp_username, $ftp_password))
    { ftp_put($connection, $folder_destination, $location_original_file, FTP_BINARY) or die("could not find directory");
    } else { }
    ftp_close($connection);
    

    It took me a big while to figure out unexpected errors that were showing up. So a few things to keep in mind:
    1) Make sure the $folder_destination is the directory INSIDE the folder assigned to the ftp user you create.
    2) The $location_original_file has to be the directory where the picture is saved (same directory that you would reference a picture to in your php code).
    3) FTP_ASCII doesn't work for pictures. It has to be FTP_BINARY (at least it FTP_ASCII didn't work for me); And finally, make sure all folders that you have to access using ftp have writable permissions.
    4) For some reasons, ftp_put could not read the uploaded file. I have no idea why. It's like when I open the ftp connection, it loses the file uploaded by the user. So, what I did is save the file on a directory in the same domain first, before even opening the ftp connection (I did this last), then I moved the saved file to the other domain. I needed to move a few versions of the uploaded picture modified so I did all the modifications first, saved the modified versions on the same domain, then moved all those saved files to their respective directories in the new domain.

    That's about it.