I have a page.php where I load a .csv file, read it and eventually upload the values into a database. What I want to do is, once the file is loaded in my form, and I've read it, first I check into my database if I still have space available, and if there is no space left, I print a message saying there is no space left, do you want to upload it anyway? I can get till this point, but I don't know how to send the file that has already been loaded into my form, to the same page through a post form, or any other method.
Here is how I read the file when I upload it
$file = $_FILES['file']['tmp_name'];
but if I try to send $file variable through a hidden field in the new POST form, it doesn't work.
You cannot send a file through hidden inputs due to security reasons. what I would recommend is to save the uploaded file on server when you read it the first time. Then if user clicks on anyway, then pass this saved file as the input for this 2nd iteration.
It can not repopulate field input.
So you can
1) use ajax to check if there are enough space and show message if no space left
or
2) store data from file in file on web server, and store data about it in hidden field in html form and after second submit use stored file.
Related
I have a form that is just a file input, this file input is meant to attach CSV files to, and then you submit it and it gives you a list of all the headers to chose from. Once you are done making selections, I want to submit another form with these choices along with the CSV file to process.
My problem is that I can't figure out how to reattach the CSV file to the 2nd form.
I thought about just using an array to just POST the data, but wouldn't it be better to just reattach the CSV file and handle it properly?
This is what I have, I tried to look this up but couldn't find anything.
<input type="file" name="fileToUpload" value="<?=$_FILES['fileToUpload']['tmp_name'];?>">
That is in the 2nd form, after the initial attachment. How can I attach the CSV file to the form to process?
What you're describing is impossible - you can't use PHP to write a file into a "file" input - it represents a location from the user's disk (not known to you on the server side) and only retrieves the actual file contents at postback.
Instead I suggest that after the first form submission, you store the file on the server's disk in a temp location, and store the location in the session (or DB, or any other persistent storage), then on the 2nd postback retrieve the location and continue using the file. That way you don't have to request it again.
Once you're done with the file you can delete the temporary file, or if that's not practical within the lifetime of the request, have a secondary job that clears the temp files folder of all "processed" files (and any files where the user never submitted the second form and their session has ended) at regular intervals.
Okay so here's the scenario:
User is presented with form that contains file inputs.
User submits form.
Form does not pass validation/verification for one reason or another.
User is presented form with errors highlighted.
File inputs are now blank.
Question:
Is it possible to re-populate the file inputs with the paths to the files the user originally selected. A PHP solution would be ideal, but I'm also open to JavaScript solutions as well.
I think the short answer here is no. You can't repopulate file upload fields. However, you can work around it.
If a file has been selected and the form submitted, then you've already received the file. What you can do is keep a reference to the file on disk and put that in a hidden field and show a message to indicate to the user you still have their file uploaded so it does not need to be replaced/re-uploaded. When your form gets submitted again without a file, you can check for the hidden field value and use that to get your local copy of the file they uploaded in their last attempt.
The other way to do this is to either submit the form via ajax (using either flash or the iframe method for the file upload) or to do an ajax call to validate the form first, and then only submit as normal if valid (thus no form reload, no loss of file upload field data).
I solved this problem by let user pick upload file as the last field before submit the form ^^
I am building a form right now that returns the user back to the form itself if there is missing fields or fields are entered wrong. One of the inputs is a image file. I was wondering if you want to echo out the image location again, do you use $_FILES[tmp_name]?
e.g. value="$_FILES[tmp_name]" to echo back the location so the user doesn't have to reselect the image again.
its not possible this way.
the $_FILES[tmp_name] reflects the full path of the already uploaded image on the server.
an its not possible to pre-select the upload field in the browser and its also not possible to get the full client-side path of the uploaded file.
so in case of an error, you could copy this temporary file to another location and display the already uploaded image to the user instead of giving him the upload field again.
but you need to make sure to delete this copied image if the user didn't try to fix his invalid fields.
or you seperate the validation of the fields from the image upload part. using some ajax magic or seperate form.
No, you can't use $_FILES['tmp_name'] in this way, as that isn't the name it had on the users computer, but the temporary name that it has on your server in your tmp folder. Not sure if there is a way to do this. I'll look into it and let you know if I find anything, but I doubt there is.
If you used an AJAX file upload, you wouldn't have to worry about this though.
This is not possible, you could try to use a value attribute on a file input to see that it has no effect.
Imagine that if you could do this it would be a serious security problem (ie: hide a file input with a pre-filled value with a common path for an important file and get it the same time with some other details)
If the form is submitted via normal html form submission then you do not have access to the user's file path. Your best bet is to use an AJAX form submission.
My client wants to have a 3 page form. The first page allows the user to enter data including a uploaded file. the second page confirms this data. and the third page submits the data to the database and directories.
Via post, I can keep saving the data to a hidden input fields, thats no problem. My problem is the uploaded file. how do I hold that document from page to page ? I am using Cakephp but any advice would help, thanks
You can always just create the illustion that the form is utilising three different pages. Use AJAX to accept and validate/request the user confirm their submitted data. If in this view they accept it initiate a POST to submit all that data.
You really don't need three physically different files to achieve this but you can still let it appear in three stages to keep your client happy.
You just upload the file to temp directory and keep the value in hidden variables just like other form data . If form successfully submitted then the image copy to desired location other wise delete the image
You can easily fake these 3 pages using CSS. Or even 2, as "third page" is actually a server script which has nothing to do with pages in the browser.
Just make your form, then put an event on the submit button which changes divs to whatever "confirmation page" he wants. and then actually send the form using a button on this page.
that's all
An uploaded file is always held temporarily. The server env var should tell you where it is. In Ruby's rack it is stored in the params var. So I guess there is a similar params var in php which has a hash with all the necessary information.
Since the file would be uploaded on the first step, one option is to put the file's location in a hidden input field along with the rest of the data (either there, or put it in the session). With CakePHP, if your file field looks somewhat like that:
<input type="file" name="data[User][image]" id="UserImage" />
Then you will be able to capture the location through
$location = $this->data['User']['image']['tmp_name'];
Which will correspond to something like /var/tmp/xxxxxx
On the last page, if the user confirms all the data, you just use move_uploaded_file() to put the file wherever you want on the server.
move_uploaded_file($location, '/new/location');
Ok so I have a HTML form, 10 files get uploaded. 6 images and 4 pdf. some of theses files are required others not.
My problem is when my script verifys the data and retuns to the user that he/she has errors all the file inputs are come back as blank... and if the user re-submits... the files are not saved.
This form has other fields other than the files as well.
I tried putting $_POST in the value of my inputs but that does not repopulate with the local file names.
I would like it so my users dont need to re enter all the files they want to upload if they made 1 or a few mistakes.
edited:
SO is this normal that the post isnt giving me the file name?
is the file still saved as temp on the server?
The value attribute of the file input type doesn't exist as this is a security risk. You can't read it or set the value of it (client or server side) so even if you had the local path on the user's computer you couldn't fill it in.
I would try to do some sort of client side (even through ajax) form validation before they submit the form. If they do submit the form, the files will upload and should still be in the temp directory (at least until garbage collection kicks in). Maybe on error write the information in $_FILES to session and instead of showing the file upload dialog, show an icon for the file to try to let them know you got the file, but they need to fix the form for everything to finish. Then after they submit the corrections you can get the data from the session and try to continue.
Make sure to specify proper settings for:
file_uploads
upload_max_filesize
memory_limit
max_execution_time
post_max_size
See:
How to optimize your PHP installation to handle large file uploads
Also make sure that:
You have specified the enctype="multipart" in the form
Check the files array with print_r($_FILES);
Instead of redirecting them back to form use a bit of JavaScript and make client browser to go one page back. Then he should file that were selected before.
You could use something like:
<body onLoad="javascript:history.go(-1)">...</body>