Reputation: 4196
Problem: When filling out the form I need to be able to upload multiple files and after it when I submit the form to make the processing of all downloaded files.
On the client side to asynchronous file uploading I use a hidden iframe (because of IE7 support requirement). After successfully file upload I add information about this file to the form (its $_FILES["file"]["name"]
and $_FILES["application_add_file"]["tmp_name"]
).
When the form is submited I recieve array of the files like:
files[0][file_path]:/tmp/phpR8eHLx
files[1][file_path]:/tmp/phpDVh3Aw
How can I get these files for processing?
I tried file_get_contents($_POST['files'][0]['file_path'])
but it's expected returns an error
Warning: file_get_contents(/tmp/phpR8eHLx): failed to open stream: No such file or directory
I know that I can use move_uploaded_file()
on each file upload, but if I do so how I can handle situation when user upload some files but do not submit the form? As I understand those uploaded files will remain on my server and I need to somehow remove them manually (and as i guess in the tmp directory files deleted after a while automatically).
Update: I dynamically add <input type="file">
to the form and submit all files at once. I want to convert uploaded files to base64 and send it to another server. Should I use move_uploaded_file()
(for safety reasons?) or I can just work with it in a tmp dir?
Upvotes: 0
Views: 50
Reputation: 6862
You will have to move_uploaded_file()
on each file upload, store those in an upload directory and wait for the form submit.
Then you can set up a cron job on the server to monitor the old files, so if some user doesn't submit the form, the job will clean it up after a predefined time.
I also suggest that you don't send the server path of these files back to the client, instead you can store them server side, either on Session or in the Database.
Upvotes: 1