skip to Main Content

I have a simple file FTP system, the problem I’m facing is that the FTP server may take 30 seconds to respond, if it takes more than 30 seconds to respond another process is starting and causing the file I’m trying to upload to be duplicated. I currently log after each successful file upload and I can see that after 30 seconds I get another log then nothing after that. What I want to happen is we let the request run its course and whenever the ftp responds we handle it from there.

$filename = date_format(date_create(), 'Y-m-d-H-i-s-u').'.txt';
$filePath = 'tmp' . $fileName;
$ediFile = fopen($filePath, 'w');
fwrite($ediFile, $fileContents);
fclose($ediFile);
$log = @$sftp->uploadFile($fileName, $filePath); //This takes roughly 30 seconds to come back with a response; 
unlink($filePath);
$log['id'] = $id;
$log['Invoice ID'] = $invoiceId;

$this->log($log, 'availity');
sleep(30); //Currently for testing as the uploadFile is disabled to not spam the server with requests

That code runs two times and I can see in my logs that at the 30 second mark we run the code again
Log Example

I have tried doing a set_time_limit(0); in the controller function so there should be no timelimit but we still get this code re-running after 30 seconds. I also tried the set_time_limit in the function calling the upload to no avail.

I expect the upload to only ever happen once and if it times out it should throw a 504 error rather than re-trying the function.

2

Answers


  1. I would change the filename to something that you can more easily check. Since you have a clinic id and an invoice id you could name it {clinic_id}-{invoice_id}.txt (assuming this is unique and what you’re afraid of duplicating). Then you can check to see if the file exists on your server since you are unlinking the file after the upload happens. If the file exists, exit the script:

    <?php
    #changed name to something you can check
    $filename = $id.'-'.$invoiceId.'.txt';
    #added escaping slash
    $filePath = 'tmp\' . $fileName;
    
    #stop the process if the file exists
    if( file_exists($filePath) ){
        exit;
    }
    
    $ediFile = fopen($filePath, 'w');
    fwrite($ediFile, $fileContents);
    fclose($ediFile);
    $log = @$sftp->uploadFile($fileName, $filePath); //This takes roughly 30 seconds to come back with a response; 
    unlink($filePath);
    $log['id'] = $id;
    $log['Invoice ID'] = $invoiceId;
    
    $this->log($log, 'availity');
    sleep(30); //Currently for testing as the uploadFile is disabled to not spam the server with requests
    
    Login or Signup to reply.
  2. Based on the code you’ve provided we can only make assumptions. You should be able to provide some timeout settings on what you are using as an (s)ftp client.

    However, I strongly advice you to use timeouts, set them to an acceptable level and never to infinite. Especially with set_time_limit(0) combination. Your request may never finish!

    The best way, in your case, is to verify if the file already exists before proceeding. However, you need to think of a different filename. You could also consider running a checksum on all the files to see if the same file is already uploaded, but that would take a long time.

    BTW, 60 seconds for an ftp server to respond is way too long! Maybe theres something wrong there.

    Login or Signup to reply.
Please signup or login to give your own answer.
Back To Top
Search