0

I m trying to upload a file from the Plesk server to the HostGator server using the FTP details I created in HostGator.

The code is from this query. Which I presume works fine. But for some reason, the file is not bing uploaded.

The code goes as:

<?php
    $ftp_server = "the.servername";
    $ftp_user_name = "the@username";
    $ftp_user_pass = "thepassword";
    $destination_file = " ";
    $source_file = $_FILES['files']['name']; 

    // set up basic connection
    $conn_id = ftp_connect($ftp_server);
    ftp_pasv($conn_id, true); 

    // login with username and password
    $login_result = ftp_login($conn_id, $ftp_user_name, $ftp_user_pass); 

    // check connection
    if ((!$conn_id) || (!$login_result)) { 
        echo "FTP connection has failed!";
        echo "Attempted to connect to $ftp_server for user $ftp_user_name"; 
        exit; 
    } else {
        echo "Connected to $ftp_server, for user $ftp_user_name";
    }

    // upload the file
    $upload = ftp_put($conn_id, $destination_file, $source_file, FTP_BINARY); 

    // check upload status
    if (!$upload) { 
    echo "FTP upload has failed!";
    } else {
    echo "Uploaded $source_file to $ftp_server as $destination_file";
    }

    // close the FTP stream 
    ftp_close($conn_id);
?>

Please note that the destination_file is empty as while I created the FTP logins in the HostGator, I clearly mentioned the folder the files have to upload. So I left this destination_file empty.

Now when I try to upload the error echo's as

Connected to ftp.vdofyfilex.com, for user files_from_vdofy@vdofyfilex.comFTP upload has failed!

As you can see, the connection was successful but the file was not uploading. could be because of destination_file is empty.

Things I have tried with $destination_file:

  1. $destination_file = 'added the full domain with directory'
  2. Added a sub folder as temp and $destination_file = 'temp/'
  3. Added a sub folder as temp and $destination_file = 'full domain with directory + temp/'
  4. $destination_file = ''

None of 'em worked.

Any help is greatly appreciated.

  • Well, the `$remote_filename` parameter of `ftp_put` (`$destination_file` in you code). definitely cannot be empty. You need to fix that. Why didn't you try fixing that before asking, if you know that it is a potential problem? – Martin Prikryl Dec 17 '21 at 08:44
  • @MartinPrikryl Ok, understood, but I did even tried add a temp folder to the directory and kept the `$destination_file` as `temp/` as well. it still gave me the same error. – Ambiguous Turtle Dec 17 '21 at 08:50
  • `temp/` is definitely not a valid file path. It should be like `/temp/filename` (or maybe `temp/filename`, if `temp` is a subfolder of your home folder). And *"full domain"* is also not file path. If you still do not make it working, post a screenshot of your favourite FTP client logged into your FTP server, so that we can see what are the paths like on your server. – Martin Prikryl Dec 17 '21 at 08:54
  • @MartinPrikryl I did these : `$destination_file = "/temp/tempfilename";` and `$destination_file = "/temp/".$source_file;` none worked... – Ambiguous Turtle Dec 17 '21 at 08:56
  • 1
    So post the screenshot I have asked for + Make sure you capture errors of the `ftp_put` to narrow down the problem. It seems that you have PHP reporting disabled. – Martin Prikryl Dec 17 '21 at 08:58
  • 1
    because in your $source_file you doesn't have a file, just a file name, if you want to use file at uplaod directyl use the tmp_name like this : $_FILES['files']['tmp_name']; But becarfull it's risky for cybersecurity this kind of usage; Because you upload file without any verification – Inazo Dec 17 '21 at 16:20

0 Answers0