Search code examples
phpuploadftp

trace if file has been uploaded correctly on ftp via php


I need to trace, store a value (1 or 0) into mysql database, if a file has been successfully upload. What I want is to be sure that the user uploaded the file and the transfer was ok, so with no errors/failed or similar.

Is there a way to do this?

$ftp_conn = ftp_connect($ftp_server) or die("Could not connect to $ftp_server");
$login = ftp_login($ftp_conn, $ftp_username, $ftp_userpass);
ftp_pasv($ftp_conn, TRUE);
$file = $filename2;
$rfile = $remote_dir.$file2;
if (ftp_put($ftp_conn, $rfile, $file, FTP_ASCII))  { }
ftp_close($ftp_conn);

Solution

  • The FTP protocol uses TCP, which ensures reliable data transfer. And the server doesn't return a success response until it has received the complete file. ftp_put() uses this to determine whether to return true or false. So just check for this in your code:

    $result = ftp_put($ftp_conn, $rfile, $file, FTP_ASCII);
    // store $result in the database