1. Advertising
    y u no do it?

    Advertising (learn more)

    Advertise virtually anything here, with CPM banner ads, CPM email ads and CPC contextual links. You can target relevant areas of the site and show ads based on geographical location of the user if you wish.

    Starts at just $1 per CPM or $0.10 per CPC.

Move_uploaded_file To A Different Server?

Discussion in 'PHP' started by fwcswebguy, Jan 24, 2013.

  1. #1
    I've done this a hundred times using move_uploaded_file but never realized till now that it only works adding files to a directory on the same server.
    $target_path = 'files/uploadedimage/';
    $target_path = $target_path .basename($_FILES['uploadedfile']['name']);

    Now I'm stuck with the task of an internal server for uploading files but the directory needs to end up on the external server. Is this even possible?
    $target_path = 'http://mywebsite/files/uploadedimage/';
    $target_path = $target_path .basename($_FILES['uploadedfile']['name']);

    Any help would be appreciated. I've been scouring Google for weeks trying to find the answer but having no luck. I'm running php on an IIS server if that matters, not my choice:(
     
    fwcswebguy, Jan 24, 2013 IP
  2. abraham26

    abraham26 Member

    Messages:
    73
    Likes Received:
    6
    Best Answers:
    2
    Trophy Points:
    48
    #2
    Connect to destination server via ftp_connect and use ftp_put (http://php.net/manual/en/function.ftp-put.php)

    
    $destination_directory="public_html/my_folder/";
    $source_directory="/home/username/my_source_folder/file1.jpg";
     
    $ftp_server = "site.com";
    $ftp_username = "username";
    $ftp_password = "password";
     
    $ftp_connection = ftp_connect($ftp_server);
    $connection_result = ftp_login($ftp_connection, $ftp_username, $ftp_password);
     
    $upload = ftp_put($ftp_connection, $destination_directory, $source_directory, FTP_BINARY);
    PHP:
    I hope this helps!
     
    abraham26, Jan 24, 2013 IP
  3. krakjoe

    krakjoe Well-Known Member

    Messages:
    1,795
    Likes Received:
    141
    Best Answers:
    0
    Trophy Points:
    135
    #3
    You will have to write it yourself. Still that's not so tricky, accept the upload, move it to a temporary location ( where you have permissions ), don't leave it in temp less another system service wipes the directory and you loose the data during the operation. The next part depends on what you can run on the destination, if you can't run anything at the destination then I'd go with ftp to move the file to the correct server, if you can script at the destination then you can send the file with cURL, or construct the request yourself, the code at the destination server should just accept the temporary file upload and move it to the final destination ....

    For the sake of script timeouts, you might want to avoid executing php, and instead use FTP/SCP commands to move the file, appending & to the exec'd command will force the process that does the actual donkey work into the background and your script should not timeout.

    Simples ...
     
    krakjoe, Jan 24, 2013 IP
  4. fwcswebguy

    fwcswebguy Peon

    Messages:
    10
    Likes Received:
    0
    Best Answers:
    0
    Trophy Points:
    1
    #4
    Thanks a lot. This does help big time but I will have to convince the grumpy old IT guy who is on vacation ATM to create an FTP user. This at least points me in the write direction.

    Again, Thanks!
     
    fwcswebguy, Jan 24, 2013 IP
  5. krakjoe

    krakjoe Well-Known Member

    Messages:
    1,795
    Likes Received:
    141
    Best Answers:
    0
    Trophy Points:
    135
    #5
    If the final destination is unix and you have a shell account you can use the login you use to get a shell ...

    If you have secure shells setup, and you should have, being that you have a grumpy IT guy, you shouldn't need to provide a password....

    Or wait for grumpy :)
     
    krakjoe, Jan 24, 2013 IP
  6. fwcswebguy

    fwcswebguy Peon

    Messages:
    10
    Likes Received:
    0
    Best Answers:
    0
    Trophy Points:
    1
    #6
    Thanks krakjoe but both servers are Microsoft IIS
     
    fwcswebguy, Jan 24, 2013 IP
  7. krakjoe

    krakjoe Well-Known Member

    Messages:
    1,795
    Likes Received:
    141
    Best Answers:
    0
    Trophy Points:
    135
    #7
    Yuk ... :)

    Then waiting is all there is I guess ...
     
    krakjoe, Jan 24, 2013 IP