views:

457

answers:

1

Is there any way to resume a broken download via an ftp connction established with php? Can php resume a broken download?

+1  A: 

Yes, it can, using the optional $resumepos parameter of the ftp_fget() function.

Example:

$remote_file_name = "/test.txt";
$local_file_name = "test.txt";
$ftp_server = "ftp.your.server";
$username = "anonymous";
$password = "my@email";

$ftp_stream = ftp_connect($ftp_server);
$result = ftp_login($ftp_stream, $username, $password);
if ((!$ftp_stream) || (!$result)) {
  echo "FTP connection failed\n";
} else {
  echo "connected to FTP\n";
}

if (file_exists($local_file_name)) {
  $resume_pos = filesize($local_file_name);
} else {
  $resume_pos = 0;
}

$local_file_handle = fopen($local_file_name, "w");
$result = ftp_fget($ftp_stream, $local_file_handle, $remote_file_name, FTP_BINARY, $resumepos);

fclose($local_file_handle);
ftp_close($ftp_stream);

You could use the ftp_size() function to see if a file needs to be resumed or not, but it is not supported on all FTP servers so you'd have to check for that.

matja
Very cool, thank you matja
Neil C. Obremski