Unix & Linux Stack Exchange is a question and answer site for users of Linux, FreeBSD and other Un*x-like operating systems. Join them; it only takes a minute:

Sign up
Here's how it works:
  1. Anybody can ask a question
  2. Anybody can answer
  3. The best answers are voted up and rise to the top

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.

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

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/*
share|improve this answer

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.

share|improve this answer

Your Answer


By posting your answer, you agree to the privacy policy and terms of service.

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