I would to know how to retain information entered for photo upload in php form if either captcha or invalid entry occurs. All other fields retain their input, with the exception of upload path, i.e. person enters "C:\Users\inc\Desktop\3.png" and path is lost if either occurs during form submission (...php5#error)
<div class="photo">
<li class='field_block' id='field_35_div'>
<div class='col_label'>
<label class='form_field'>Photo 1</label>
<label class='form_required' > </label>
</div>
<div class='col_field'>
<input type="file" name="field_35" id="field_35" value="" class='text_box1' onchange="fmgHandler.check_upload(this);">
<div id='field_35_tip' class='instruction'></div>
</div>
I tried adding echo, but this does not work.
File fields cannot be pre-populated. And you probably don't want the user to have to upload the file every time again and again anyway, as that may potentially take a long time.
Save the file somewhere on your server and remember either in the session or in a hidden input field which file it was. Display something like "file uploaded" next to the file selector. If the user choses to upload another file, overwrite the previously uploaded file, otherwise use the previously uploaded file.
Related
I have been trying to update a form that has file inputs. I want users to only change the files if they are not satisfied with the previously set file, else the previously set file will be updated in the database while the form is submitted. The data fetched from my database (i.e the previously saved file info) is $query->document->name which is meant to represent the name of the file
My form code looks like this:
<div class="col-md-6">
<div class="form-group">
<label for="account_statement">{{$form->name}} :*</label>
<input class="form-control" value="{{$query->document->name}}" id="{{$form->short_name}}" name="{{$form->short_name}}" type="file">
</div>
</div>
My written code is however not getting the result I want done. The form is not even showing that a file has been set already.
For security reasons, the browser does not let you set the value of an input file. You would have to use javascript to determine if the file exists.
Your html code would contain two displays, one showing the file if it exists, the other showing the input file. You then use javascript to determine when to show which display. Something like this:
<span id="documentName">{{$form->document}}</span> <button id="removeFile">Remove</button>
<input class="form-control d-none" id="document" name="document" type="file"/>
Javascript
// I assume you are using bootstrap and hence jQuery
<script>
$('button#removeFile').click(function(e) {
if(#{{ form->name }})
$('documentName').addClass('d-none')
$('document').removeClass(d-none')
}
}
</script>
I have form which includes a file upload.After the form is submitted the selected filename is saved in the database.And i have a edit form of saved data.In that form in the file upload button,at the position where shows "no file chosen" i need to echo the saved file form the database.
<div class="col-md-3">
<div class="form-group">
<i class="fa fa-fw fa-cloud-upload"></i>
<label for="">File upload</label>
<?php echo $announcements_details['file'] ; ?>
<input type="file" id="captionfile" name="captionfile"></div>\
</div>
I need echo the $announcements_details['file'] in the no file choose chosen part. Can anyone help me?
It is impossible to do this, due to security reasons. you would be able to select and upload any file from the client to your website.
However, you can display a message below it saying a file has been uploaded. Or you could override the input style using css and display the text that way.
Yes, as Jerodev said it is not possible to takeout the same file in place of the choose file button. But there is a work around for this.
If the uploaded file is an image file, then you could pull up the filename from the database, and display a small thumbnail for the image below the choose upload button.
And if it is any other file, (pdf/txt..etc.), then you could display a link to the file below the choose upload button, so that the users can download the file and see what they have uploaded.
Hope this helps.
Thanks.
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'm using codeigniter and jquery to create a form that takes text values and a single file upload.
I want it so that if the form validation fails, the uploaded image is still visible to the user, but I'm at a loss now about how to do this without issues popping up.
My current solution is uploading the image regardless if the form validated or not, and then checking if a file with the same name as the name in $_FILES exists, and serving that. But this leads me to a problem with duplicates. I'm appending duplicate file name with a number (eg. file2.jpg), but that means the file uploaded will always find the image name "file.jpg" even if the file the user uploaded with allocated the name "file3.jpg".
There must be a simpler way.
How about save file with hashed name md5(name+actualTimestamp) and save this file name to user session (you could save this filename in cookies or use session mysql table with serialized session data).
How about you assign a unique id to the form, probably a combination of year, month, day, hour, minute and seconds then move uploaded image to a temp directory on your server, save the image with the same name as the form id....
your code should be something like:
<form method="post" action="process.php">
<input type="hidden" name="form_id" value="<?php if(isset($_POST['form_id'])){ echo $_POST['form_id']; }else{ echo date("Ymdhis"); }" />
<!-- other form objects goes here -->
</form>
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