I want to upload a CSV file and use it just for import content. Is necessary for this to use move_uploaded_file to move temp file? Or should I just use temp file uploaded without moving it in a specific folder? Which will be the best practice?
There is no need to move the file around if you do not need to keep it longer than the request:
The file will be deleted from the temporary directory at the end of the request if it has not been moved away or renamed.
from POST method uploads
However if you want to move the uploaded file around, you should use move_uploaded_file() for that operation.
Related
I'm uploading files using php. The file has to be below a certain size. If it's not, the script returns an error.
I've noticed that php stores the uploaded file in (my case) C:\Windows\Temp\filename.extension
Right now, if the file is above the Max size, the script only returns an error. So what happens to the file that got uploaded? Does php delete it automatically or do I have to do that? Even if the the file was the right size, the temp file still remains there I guess. Should I be deleting this file in my script?
No, from the manual:
The file will be deleted from the temporary directory at the end of
the request if it has not been moved away or renamed.
I can't seem to find clarification. I have 7 steps and the second one is to upload a file. The problem I am having is that on the final step when I finalize the form and try and access the temp file it says "Could not access file: ..." (saved in the default tmp folder).
Other multi-step tutorials say to keep it in a temp folder and then move to the appropriate folder when complete. So do they mean I move it from the default temp folder into a temp folder I manage and then when they complete the form move it again to a final folder?
So do they mean I move it from the default temp folder into a temp folder I manage and then when they complete the form move it again to a final folder?
Yes, exactly. You do the first move on the request that receives the files, because when that request is finished PHP would delete them if they still exist.
See move_uploaded_fileDocs and Handling File Uploads.
With "temp folder" they don't mean /temp.
After the upload, you should actually save the file in some folder, maybe with the user's session id as name. Then, with the last step, you should move that file to its definitive location.
Temp files (like, actual temporary file) are destroyed once the program who created them exits (that means usually, in php, after the next output page is sent to the client).
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.
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.
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
I'm uploading files via JS and storing the temp path in the session.
Than i use the following code to move the files.
if(move_uploaded_file($_SESSION['temp_img'][$key]['path'], $dest.$bigimg)){
$dest and $bigimg are defined earlier in the script with the id from the database.
Any Ideas or alternatives ?
MANCHUCK's answer was close but not quite there. You must call move_uploaded_file within the script where the file was uploaded. You cannot do what you're doing, that is, "storing temp path in the session" because that path is only valid for one request.
From the PHP manual:
The file will be deleted from the
temporary directory at the end of the
request if it has not been moved away
or renamed.
(Emphasis mine)
move_uploaded_file checks that a file has been uploaded to that page. You are actually uploading the file to a different PHP script then storing in a session. Instead of using move_upload_file use rename.
What is the output of $_SESSION['temp_img'][$key]['path'], also do you have permission to write to the web directory your placing the files. You may need to set it to 777 for some hosts to allow the webserver to write there.
I've seen ways to upload files directly to S3 and I've seen ways to upload a file already on the server. I need to modify the file's name before sending it on to S3, but I don't want to store it locally and then upload it, if I can help it. Is there a way to put it in a buffer or something? Thx.
The file will always end up in the temporary directory first while the upload completes, even before you're able to work with the uploaded file. You get all the file's chunks before, and then it get rebuilt in the /tmp directory by default. So no, there's no "pass-through". But I guess you could re-upload directly from the temporary directory afterwards instead of moving it to another working directory.