Search code examples
phpftpput

PHP: Using ftp_put results in an error, how find reason?


I am creating an export file using PHP. After the file is created, I wish to move it to another ftp-location. The file creation goes well, also the ftp-connection seems to go well, but the transfer/uploading of the file results in an error.

I have checked the credentials and path using Filezilla and believe the FTP path is good.

What goes wrong with this code or how can I get more feedback from the code to find the reason it results in an error?

This is part of the code. I pick it up after the file creation is done (so this all happens in the same file):

 fclose($myfile);

 $conn->close();

//Move file to ftp-server
$ftp_server = "ftp.domain.com";
$ftp_username = "username"; 
$ftp_userpass = "password"; 
$ftp_conn = ftp_connect($ftp_server) or die("Could not connect to $ftp_server");
$login = ftp_login($ftp_conn, $ftp_username, $ftp_userpass);
$remote_location = "/public_html/testdir";
$file = "export.csv";

// upload file
if (ftp_put($ftp_conn, $remote_location, $file, FTP_ASCII))
  {
  echo "Successfully uploaded $file.";
  }
else
  {
  echo "Error uploading $file.";
  }

// close connection
ftp_close($ftp_conn);

So the error I get is: "Error uploading export.csv." The file exists in the same directory as the php file that is running the code.

Please be gentle...


Solution

  • As far as I know, the ftp_put accept 2nd parameter which is $remote_location in your example as a file name not a directory/folder name. So this variable

    $remote_location = "/public_html/testdir";
    

    is invalid assume that "/public_html/testdir" is a directory.

    Hence you should change it to point to a file: "/public_html/testdir/example.csv"

    Regards