Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

php:: how long to tmp files stay?

Tags:

php

temp

tmp

I am working on an upload script.

If a user uploads a file and it already exists I want to warn the user (this is all through ajax) and give them the option to replace it, or cancel.

Instead of moving the file, I was curious if I could just leave the file in tmp and pass back the path to that file in the ajax response.

If they user says overwrite the old file in that ajax request pass the path back to php which continues to work on the file.

For this to work however I need to know how long a file stays in php's tmp dir

like image 620
Hailwood Avatar asked Jan 11 '11 01:01

Hailwood


People also ask

How long do TMP files last?

By default, all the files and data that gets stored in /var/tmp live for up to 30 days. Whereas in /tmp, the data gets automatically deleted after ten days.

How often does tmp get deleted?

As you can see the directories /tmp and /var/tmp are scheduled to be cleaned up every 10 and 30 days respectively.

Are files in tmp automatically deleted?

No, there's nothing turned on by default in Windows that will automatically delete temp files. Not on shutdown, not on reboot, not ever. The Disk Cleanup tool has an option to do that when its run.

Is tmp persistent?

Typically, /var/tmp is for persistent files (as it may be preserved over reboots), and /tmp is for more temporary files.


2 Answers

Files uploaded through POST are deleted right after php script finishes its execution.

According to php.net: "The file will be deleted from the temporary directory at the end of the request if it has not been moved away or renamed."

like image 56
zerkms Avatar answered Sep 20 '22 23:09

zerkms


For uploaded files, the manual states:

The file will be deleted from the temporary directory at the end of the request if it has not been moved away or renamed.

Files that are to be kept should therefore be moved to another location.

More generally, as your question title might imply, temporary folders are left to be cleaned up by the system. This is true when using functions like tempnam or tmpfile, or simply when writing to the temporary directory (see sys_get_temp_dir).

In Ubuntu, this is done at every system reboot, or at a time interval, as defined in /etc/default/rcS.

In some Red Hat based distros, it is done using the tmpwatch utility from a cronjob. In others, the /tmp partition is mounted using the tmpfs filesystem, which is similar to a RAM disk (therefore being cleaned when the computer shuts down).

Another known mechanism is a size threshold, which means that the temporary directory will be cleaned up from the older files when it reaches a certain size.

like image 44
netcoder Avatar answered Sep 22 '22 23:09

netcoder