I am working on a csv import option on my php page. Is there any way to allow the user to upload a csv for processing and then destroy the file when it is complete? I have seen plenty of tutorials for uploading files, but none seem to mention something like this.
+1
A:
Sure! Take an upload script, and unlink()
the file when you're done. Nothing to it.
Don't process the temporary file directly, though. Always move_uploaded_file()
it to a directory you control. Then, delete it there.
Pekka
2010-07-01 19:00:47
+1 If you have an open file handle to the temp file first then unlink will work.
Tim Santeford
2010-07-01 19:02:13
@tim yup, but you don't need an open file handle do you? http://www.php.net/unlink it needs, however, to be `fclose()` d if you did any previous operations on it, good point.
Pekka
2010-07-01 19:03:01
@Pekka I was under the impression that linux will delete the file immediately unless there is at least one open file pointer. unlink will ensure that no other process can open the file though which makes unlink a great option.
Tim Santeford
2010-07-01 19:06:56
@tim interesting! I had always assumed the temporary files had a longer lifetime than that, but it stands to reason a `move_uploaded_file()` straight away is the best thing to do then.
Pekka
2010-07-01 19:09:20
@Pekka I meant that unlinking will delete immediately unless you have an open pointer. Im not sure about unmoved file uploads however, they may be long lived.
Tim Santeford
2010-07-01 19:11:46
My CS prof always instructed us to immediately unlink any temp file, even before use, to both insure it gets removed at the end of the process and also prevent a malicious script from mucking with it while in use.
Tim Santeford
2010-07-01 19:15:08
@Tim : you are correct about unlink on Unix/Linux. The file will immediately become inaccessible but processes already have it open will not notice. It will be deleted right away of no one has it open, or deleted when all processes have released it. Compare windows, which will not let a file be deleted if *anyone* has it open, including a deadlocked / zombie process.
Stephen P
2010-07-01 19:17:29
A:
When you upload a file to PHP it's stored in a temporary location, you can move it from there to a working directory, do your processing and then remove it.
$working_file = "/temporary/file";
move_uploaded_file($_FILES["file"]["tmp_name"], $working_file);
... // Do your processing on $working_file
unlink($working_file);
All you have to do is unlink()
it when you've finished processing to delete it.
For more information on uploading files see:
http://php.net/manual/en/function.move-uploaded-file.php
http://www.w3schools.com/PHP/php_file_upload.asp
JustJohn
2010-07-01 19:02:53
Awww... I'm sure this will work in most cases, but I'm always reluctant to work directly in the temp directory, and deleting files there. Just a gut feeling though - it can be unfounded.
Pekka
2010-07-01 19:04:32
I realized that as soon as I posted it, I've updated my example with move_uploaded_file().
JustJohn
2010-07-01 19:06:29