0

This question follows on from one of my previous ones. The modified large file upload script I am now using is shown below

function uploadFile($local,$remote,$token)
{
 $args = 'Dropbox-API-Arg:'.   
 json_encode(array("path"=>$remote,"mode"=>"overwrite","mute"=>true));
 $headers = array("Authorization: Bearer {$token}",
                  'Content-Type: application/octet-stream',
                   $args);

 $ch = curl_init('https://content.dropboxapi.com/2/files/upload/');
 curl_setopt($ch,CURLOPT_HTTPHEADER,$headers);
 curl_setopt($ch,CURLOPT_PUT,1);
 curl_setopt($ch,CURLOPT_CUSTOMREQUEST,'POST');

 $fp = fopen($local,'rb');
 curl_setopt($ch,CURLOPT_INFILE,$fp);
 curl_setopt($ch,CURLOPT_INFILESIZE,filesize($local));
 curl_setopt($ch,CURLOPT_RETURNTRANSFER,true);
 $response = curl_exec($ch);
 curl_close($ch);
 fclose($fp);

 //@unlink($local);
 echo $response;
}

I have run into two issues here.

  1. From what I can see the "mute"=>true parameter is at times ignored by Dropbox and I end up getting a desktop file update notification anyway.
  2. More of an issue - look at the commented out @unlink line. I had this in because I need to discard the $local file once the Dropbox upload has succeeded. The problem is that if I do unlink the local file everything works but the uploaded file on Dropbox is empty.

It is not clear to me why this is happening. I have been unable to find any definitive information but my understanding was that cURL calls are always synchronous. If they are not then it is difficult to trust the response from the Dropbox API as being a definitive confirmation that the file has truly been uploaded to Dropbox.

I have come across vague comments about cURL can sometimes be asynchronous in my searches. If that is the case how can I force it to be synchronous? I can always deal with the local file unlink separately via a CRON job that checks its age but that is a messier solution.

Community
  • 1
  • 1
DroidOS
  • 8,530
  • 16
  • 99
  • 171
  • In this code, you're sending the file `$lfile`, but I don't see where that's defined. Is it the same value as `$local`? Are you sure that commenting and uncommenting the `unlink` call is the only change you're making between uploads that succeed and uploads that fail? – user94559 Dec 17 '15 at 06:27
  • 1
    Just as a guess, is there any chance you're running this function more than once? (The second time around, perhaps the file's missing from the previous `unlink`.) I'd suggest logging the size of the file before the upload and then make sure the logs look sensible. – user94559 Dec 17 '15 at 06:28
  • @smarx - lfile was just a left over when I copied my original code and changed a few variable names to make it easier for SO. Coding errors is not an issue - if I comment out the `unlink` the empty file issue disappears. – DroidOS Dec 17 '15 at 06:41
  • @smarx - the second time round idea bears investigating. I am actually firing off this PHP script from a Watcher job. I am a Watcher newbie but I know that its close cousin Incron can fire off the same event multiple times. – DroidOS Dec 17 '15 at 06:42
  • @smark - your guess was right. Watcher fires off the file modify event more than once. The second time round the original file was being overwritten with a zero byte file given that the `$local` file was no longer present. Write up this as an answer and I will accept + upvote it. Could you also comment on why it is that `"mute":true` is being ignored and I keep seeing Dropbox notifications on my computer? – DroidOS Dec 17 '15 at 07:16
  • Mute isn't guaranteed in general. I don't know under what circumstances the notification will be suppressed or not. Feel free to post on the Dropbox forum or send in a support ticket if you want someone to investigate. – user94559 Dec 17 '15 at 09:14

0 Answers0