1

How can I automatically cleanup failed uploads? PHP is storing them in /tmp and all file names look like phpAbCDeF (basically php followed by 6 characters).

I know that I can use the command:

find /tmp -name php\*

BUT this also removes other temp files that begins with php (created by other processes) that I don't want to delete.

Please suggest a solution.

  • Is this a third-party application or something you wrote/can modify? – cpugeniusmv Apr 13 '15 at 23:43
1

It depends on how your files are named. Here are some suggestions:

  1. delete all files whose name is php followed by exactly 6 characters:

    find /tmp -type f -name 'php??????' -delete
    
  2. Delete all files whose name starts with php and which are empty:

    find /tmp -type f -name "php*"  -empty -delete
    
  3. A better way would be to edit your server's php-ini file and set the variable upload_tmp_dir. That way, you can just delete everything in that directory. For example, if you set this in your php.ini:

    upload_tmp_dir=/tmp/phptmp
    

    You can then simply remove all files from the folder:

    rm  /tmp/phptmp/*
    
0

If this is an application that you wrote/can modify, I would recommend using register_shutdown_function at the time you create the temporary file. That way you can unlink even if your script is interrupted.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.