0

I can't seem to resume an FTP upload using curl. It always appends the full local file to the partially uploaded remote file, resulting in the remote file being larger then the local file.

I am wondering if I have missed an option or if curl_multi has anything to do with it?

$resume = true

$filesize is the current size of the remote file in bytes, fetched using a separate curl ftp request.

I have tried with CURLOPT_FTPAPPEND option on and off.

Any help would be great as I am banging my head against the wall trying to figure this out :)

...

$ch[$i] = curl_init();

$file = fopen($local_file, 'rb');

if ($resume)
{           
        curl_setopt($ch[$i], CURLOPT_RESUME_FROM, $filesize);

        // move file position forward to resume
        fseek($file, $filesize);
}

curl_setopt($ch[$i], CURLOPT_URL, $remote_file);
curl_setopt($ch[$i], CURLOPT_PROTOCOLS, CURLPROTO_SFTP);
curl_setopt($ch[$i], CURLOPT_USERPWD, "{$ftp['user']}:{$ftp['pass']}");
curl_setopt($ch[$i], CURLOPT_UPLOAD, true);
curl_setopt($ch[$i], CURLOPT_HEADER, false);
curl_setopt($ch[$i], CURLOPT_INFILE, $file);
curl_setopt($ch[$i], CURLOPT_INFILESIZE, filesize($local_file));

curl_multi_add_handle($mh, $ch[$i]);

...

UPDATED

Since hours of googling didn't help, I came up with a simple work around.

There are not many posts on using curl to resume an upload so I hope this helps someone else save time.

It is just one line using fseek(), but that is usually the case ;)

arbme
  • 4,831
  • 11
  • 44
  • 57

1 Answers1

0

UPDATED

Since hours of googling didn't help, I came up with a simple work around.

There are not many posts on using curl to resume an upload so I hope this helps someone else save time.

Use fseek() to move file position before calling curl_setopt($ch[$i], CURLOPT_INFILE, $file);

...

$ch[$i] = curl_init();

$file = fopen($local_file, 'rb');

if ($resume)
{           
        curl_setopt($ch[$i], CURLOPT_RESUME_FROM, $filesize);

        // move file position forward to resume
        fseek($file, $filesize);
}

curl_setopt($ch[$i], CURLOPT_URL, $remote_file);
curl_setopt($ch[$i], CURLOPT_PROTOCOLS, CURLPROTO_SFTP);
curl_setopt($ch[$i], CURLOPT_USERPWD, "{$ftp['user']}:{$ftp['pass']}");
curl_setopt($ch[$i], CURLOPT_UPLOAD, true);
curl_setopt($ch[$i], CURLOPT_HEADER, false);
curl_setopt($ch[$i], CURLOPT_INFILE, $file);
curl_setopt($ch[$i], CURLOPT_INFILESIZE, filesize($local_file));

curl_multi_add_handle($mh, $ch[$i]);

...
arbme
  • 4,831
  • 11
  • 44
  • 57