I'm trying to submit a form with several file
uploads using ajax with input text.
If the user have uploaded all the
files using ajax but forget to enter his
name and the form re-populate again. How do
i tell the user that he has already uploaded
these files
<input type=file id=file1 name=file1 />
<input type=file id=file2 name=file2 />
<input type=text id=name name=name />
The form reload showing error message that
the name has not being enter. How do i tell the
user that the file1 has already being uploaded.
Technology(PHP)
Thanks
I suggest writing code that:
1. When the form is activated, check what is there and what isn't.
2. Cache that data into cookies
3. Do uploading code
4. Upon reloading the form, check cookies for what was entered and what wasn't.
5. Display an alert/message to tell them what they entered and what is still needed.
Related
I have a site that allows user to upload files to a folder. First I check if the file exists, if it does then I check the value of a checkbox to determine if the user wants to overwrite the existing file. If upload is hit and the box is unchecked, I do a die() that has a message and a back button. However, when I hit back, check the checkbox (the file is still displayed as if it was selected), then submit again, it doesn't overwrite the file (but appears to, no die). Is the checkbox value not being changed or reset when I hit back or something?
Maybe worth mentioning, there is a form (if method=post) and a form for (else) but they are exactly identical.
Can someone help me out please? Thanks.
Here is my code:
Form:
<form id='upload' method='post' enctype='multipart/form-data' action='<?php $_server['PHP_SELF']; ?>'>
<input type='file' name='file_upload'>
<br><br>
<label><input type="checkbox" id="overwrite" name="overwrite" value="Yes"> Overwrite file (if file exists)</label>
<br><br>
<input type='submit' value="Upload">
</form>
Rest:
if(file_exists($fullFileName)){
$confirmation = $_POST["overwrite"];
if($confirmation == "Yes") {
unlink($fullFileName); //overwrite confirmed, unlink old file
}
else {
die("Upload aborted. Please check 'Overwrite file (if file exists)' box to overwrite existing file <input type='submit' href='#' onclick='history.back();' value='Back'>");
}
}
if(!move_uploaded_file($_FILES['file_upload']['tmp_name'], '' . $_FILES['file_upload']['name'])){ //move new file into place
die('Errrrrr! Error uploading file - check destination is writeable.');
}
Instead of a die(), you can just do a PHP header redirect which would essentially "clear the form" and is would be a new page load. If you wanted to retain the selected file, you could pass it back as a GET value.
Do you have an elegant solution to validate server side a form containing more than one input type file under these conditions:
If one file is not valid user will have to upload another
If one file if valid the server should keep it in memory so user don't have to upload it again.
Valid file should be shown on the form
User may just abort and leave the page. Whatever has been saved on the server should be deleted.
As I example let's take a form containing 1 input type text and 2 inputs type file.
<input type="text" name="title" />
<input type="file" name="file1" />
<input type="file" name="file2" />
User submit a valid title, a valid file1 but invalid file2.
The goal here is to save each valid file until the form is valid or until session expires or something expires :)
Suggestion 1
Generate a unique key for the user form (i.e form123) create a directory /tmp/uploads/form123 where valid file are saved.
A cron running every 10min go through all directories in /tmp/uploads/ and delete the one existing for more than 10min.
With this suggestion the big question is how to clean up the system if user abandon the form.
I think you're on the right track with your suggestion. keep track of valid/invalid entries for the user (you don't even have to save it to disk, just grab the reference and store it in the $_SESSION global), and adjust the form based on that.
if the form is invalid, store the submitted, valid files in $_SESSION
<?php
// ... if the form is invalid
if (isset($_SESSION['file_1']) { ?>
<!-- show a placeholder -->
<!-- i.e. something saying 'file 1' and its name or whatever -->
<?php } else { ?>
<!-- show the input -->
<?php } ?>
Then, when the form is finally valid, combine the values you have stored in $_SESSION with the information submitted to produce the final, valid form.
I´ve a multipart form with a mixture of default inputs (text, select etc.) and a file upload (<input type="file">).
I´m using a combination of form validation class and upload library of Codeigniter for form submission. That works great.
I´ve only one problem for what I haven´t found a solution yet: If the user selects an image but misses to fill another required field (like "name"), then the form validation class blocks the request and shows an error message to the customer.
But now I´ve the problem, that the image was already submitted successfully and I don´t want to let the user add the file again. So I want to pre-fill the file input with this data.
I´ve tried different things like:
<input type="file" name="image" value="<?php echo set_value('image','');?>" />
and also spent time on finding a solution on the web but without success.
On the server side, you do not get any information about where the file is located on the client's computer, so in the scenario of a user uploading an image successfully but the user hasn't filled out the rest of the fields properly, you have to simply omit the input type="file" field entirely but keep a store of where the file is located on your server. There's a few ways to go about this, but it all involves taking the absolute location of the uploaded file and:
Inserting it back as a hidden value using <input type="hidden" name="uploadedFile" value="<?php echo $absPath; ?>" /> then checking for the existence of $_POST['uploadedFile'] and utilizing it appropriately. But this isn't a solid idea as you're now exposing server paths to the end-user (opens yourself up to malicious attack.)
Starting a session and saving the absolute path in the $_SESSION variable while presenting the user with a simple token in their re-attempt form.
I'd stick with method 2, so assuming you've done all the work to validate the form and upload the file and your file is located in $absFilePath, you could do the following:
session_start(); // This needs to be at the very top of you PHP file
// ...
$formToken = md5(time());
$_SESSION['uploadedFile'][$formToken] = $absFilePath;
Then render the token as a hidden variable using:
if (!empty($_SESSION['uploadedFile'][$formToken]))
echo '<input type="hidden" name="formToken" value="'.$formToken.'" />';
and hide the file upload portion using
if (empty($_SESSION['uploadedFile'][$formToken]))
echo // <input type="file" output here...
finally inside of your form submission code check for the existence of a formToken value before attempting to load $_FILES['image'] using isset($_POST['formToken']), and handle it using:
$absFilePath = $_SESSION['uploadedFile'][$_POST['formToken']];
Bam! Now you have your absolute file path as if the file had been uploaded just like before.
Since you haven't given enough code, I can only given you enough instruction to get you started, but this should be more than enough.
Im providing a file preview option, where the user attaches a file, and then clicks preview.
Which uploads the file, and outputs a PHPExcel preview.
From that page, I want to include a button that says continue.
Which will then proceed with the actual upload and database row creation.
However once I redirect to the preview page, I have no idea how to attach this file to my hidden form for resubmission.
I can attach all the old input values....like so...
But how do I attach the file??? (using blade in laravel for templating)
/* These work and are correct*/
<input type="text" id="xlsColumnOrder" name="xlsColumnOrder" value="{{$input['xlsColumnOrder']}}"/>
<input type="text" name="researcher" value="{{$input['researcher']}}" />
<input type="text" name="leadcity" value="{{$input['leadcity']}}" />
<input size="16" id="survey_date" name="survey_date" type="text" value="{{$input['survey_date']}}" />
/* This doesnt attach anything to the file input
/* Need to attach the previously uploaded file here for resubmission */
<input class="file" id="csvfile" name="csvfile" type="file" value="{{$file['csvfile']['tmp_name']}}" />
You cannot automatically resend user files so I would do it this way:
When user sends form if any file is provided you save file in some directory (you treat it as temporary) and save file name/names to session and show user preview of file
When he clicks continue - you can do with other data whatever you want (for example save it to database or show as hidden in another form) and you still have uploaded filename in session so if you want you may even again show file preview
When you want to finally save all user data you move user file/files to final directory (and possible rename it) and save all the data with file(s) name (probably in database).
As addition you should have some kind of cron that removes data from your temporary directory that very created for example more than 3 hours
I have a Form that I am using to receive an uploaded .csv file, parse it and insert the data into my MySQL db on an Apache server. The page first checks to see if there is an uploaded file. If there is, it processes the data, if not the form (below) is displayed.
<form enctype="multipart/form-data" action="uploadfaculty.php" method="POST" id="UploadForm">
Choose a file to upload: <input name="uploadedfile" type="file" /><br />
<input type="submit" value="Upload File" />
</form>
My problem is that currently the user can simply F5 the browser over and over again and because the file is still on the server and in the $_FILES array, it processes it every time.
I've tried:
unlink($_FILES['uploadedfile']['tmp_name']),
unlink($_FILES['uploadedfile']),
unset($_FILES['uploadedfile']['tmp_name']), and
unset($_FILES['uploadedfile'])`
I've even reset the form via Javascript (which I knew would not work, but did it just to eliminate all doubt). All to no avail. I'm sure it's something simple I'm missing...it almost always is. Any thoughts?
It's not unsetting because the post action is stored on the browser's end and being re-uploaded (in a small amount of time as it's only a csv) when they hit F5. Which is essentially the same as them using the form to upload another csv.
You can do this:
if (isset($_POST['csv'])){
$DataProcessed = DataProcessingFunction();
}
if (isset($DataProcessed) && $DataProcessed){
header("Location: /path/to/form/page.php");
exit();
}
This will clear the post data sent in the earlier request. Refreshing will not resubmit the form.
You can header redirect them to that upload page after processing to prevent the post data from continually going in via a refresh. But the temporary file should be cleared once the processing is done. PHP does not keep the file unless you use the move_uploaded_file function.