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? Apr 13, 2015 at 23:43

2 Answers 2


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:


    You can then simply remove all files from the folder:

    rm  /tmp/phptmp/*

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.

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .