If ftp server on other machine supports REST
command (restart uploading from certain point) there is dirty way to implement this:
- create temp file
- put X bytes to this file from the file you want to upload
- upload temp file
- write status to another file (or session, but not sure if it will work)
- append another X bytes to temp file
- Upload temp file starting form X bytes
- crite status to file
- repeat 5-7 until whole file is uploaded
- delete temp & status files.
Sample code:
$fs = filesize('file.bin');
define('FTP_CHUNK_SIZE', intval($fs * 0.1) ); // upload ~10% per iteration
$ftp = ftp_connect('localhost') or die('Unable to connect to FTP server');
ftp_login($ftp, 'login', 'pass') or die('FTP login failed');
$localfile = fopen('file.bin','rb');
$i = 0;
while( $i < $fs )
{
$tmpfile = fopen('tmp_ftp_upload.bin','ab');
fwrite($tmpfile, fread($localfile, FTP_CHUNK_SIZE));
fclose($tmpfile);
ftp_put($ftp, 'remote_file.bin', 'tmp_ftp_upload.bin', FTP_BINARY, $i);
// Remember to put $i as last argument above
$progress = (100 * round( ($i += FTP_CHUNK_SIZE) / $fs, 2 ));
file_put_contents('ftp_progress.txt', "Progress: {$progress}%");
}
fclose($localfile);
unlink('ftp_progress.txt');
unlink('tmp_ftp_upload.bin'); // delete when done
And file to check with ajax:
if(file_exists('ftp_progress.txt'))
echo file_get_contents('ftp_progress.txt');
else
echo 'Progress: 0%';
exit;