Making file input a not required field - bootstrap file input - php

I want to make file input field not required , I have 2 fields for file input one is for profile picture and another for uploading documents.
If I do not select any image it says "You did not select file to upload" for both documents and profile picture
If I select a file it works perfectly fine.
The file field should not be mandatory.
I am using this plugin : http://plugins.krajee.com/file-input/demo for front end design
Following is the code I tried on form submit
if(!array_filter($_FILES['docs']['name'])) {
//upload file
}
if($_FILES['profile_pic']['name']!='') {
//upload file
}
What is wrong in the above code. Can anyone please tell why it is not working...

Related

Different browsers show different behaviour with input type file

I have 2 file upload fields in my form. Both look like this:
<input type="file" id="file1" name="file1" accept="application/pdf,application/msword">
<span id="file1_status">Currently uploaded: <?php echo $file1_fileName; ?></span>
<input type="file" id="file2" name="file2" accept="application/pdf,application/msword">
<span id="file2_status">Currently uploaded: <?php echo $file2_fileName; ?></span>
where $file1/2_fileName; is either "none" when the page is first loaded or equal to $_FILES["file1/2"]["name"] after a post event.
Toward the end of the page, I do
$(document).ready(function () {
jQuery("input#file1").change(function () {
var val = $(this).val();
_('file1_status').innerHTML = "Currently uploaded: " + val.substr(12); // removes C:/fakedir/
});
Same for file2. So, basically if someone uploads "file.pdf", there is a text under the file input that reads: "Currently uploaded: file.pdf". I do this so the user sees that the file is still there if form validation fails after form submission.
OK so far so good.
Rather than using "required" in the input file field, upon form submission, I do the following:
if (is_uploaded_file($_FILES["file1"]["tmp_name"])) {
// check mime_type, file size, etc.
} else {
// display an error below the input field that a file is missing.
}
Same for file2. Now to the actual question/problem.
Imagine the following situation. A user only uploads file1 but forgets file2 and clicks submit. This is what happens:
Firefox (latest version): Below the file1 field the status still shows "Currently uploaded: file1.pdf" and below the file2 input field an error message is displayed to remind the user to upload this file also. If the user complies and uploads file2, then clicks on submit again, the form is submitted and all is fine, i.e., both files have been attached to the form submission. This is the expected behaviour.
Chome/Edge: For the same user behavouir everything is the same except for when the user clicks on submit a second time. For some reason, both these browsers now show an error below the file1 input field (although it still shows "Currently uploaded: file.pdf" which the user uploaded in the very beginning). So for some reason, and although $_FILES["file1"]["tmp_name"] is not empty, the test
is_uploaded_file($_FILES["file1"]["tmp_name"]) fails upon the second form submission in both Chrome and Edge but not in FF.
This is very confusing. Why is this and how can this be avoided/fixed?
It seems that the Edge/Chrome behaviour is indeed to expected/normal behaviour. To my great embarrassment I have not been able to reproduce the above behaviour in FF anymore so I am not exactly sure what happened because I really did see a different behaviour while testing it for about an hour or so.
Anyway, for all practical purposes, I was able to work around the issue by moving the files that met all my criteria (re mime types, file size, etc) into a temporary upload directory on my server, store the file name in a session variable, and modify this code:
if (is_uploaded_file($_FILES["file1"]["tmp_name"])) {
// check mime_type, file size, etc.
} else {
// display an error below the input field that a file is missing.
}
to this
if (is_uploaded_file($_FILES["file1"]["tmp_name"])) {
// check mime_type, file size, and if OK move to upload directory on server and store the name of the successfully uploaded file in $_SESSION["file1"]
} elseif (!empty($_SESSION["file1"])) { //basically this gets invoked the second time round, i.e., when the user uploads the 2nd file they forgot when submitting the form for the first time
$file1_fileName = $_SESSION["file1"]; // used to display under the input file element (see above)
} else {
// display an error below the input field that a file is missing.
}
Same for file2. So while the first "if" condition fails upon the 2nd form submission, the "elseif" condition is true and no error is issued.

How to keep the previous uploaded image (in db) after submitting a form without selecting a new image to be uploaded

I have a simple form for creating an article: Title,image,category,body , etc..
My problem is with the image input.
Selecting a new one and submitting everything works fine:
the image is being uploaded to the server
the title of the image is being saved to db
and i can print it in the article.
By editing the whole form, filling all fields but leaving the image field as it is, and finally submitting, the image field value in db is changing to nothing.
How can i configure the php file, so every time the user submits the
form without selecting an image (if there was an image pre uploaded in
that article ) keep as submitted the previous image (from db) instead
of nothing.?
If these informations can be of any help:
I can print the image like this : <?php echo '<img src="'.$results['article']->img.'" width="100px">'; ?>
Simple input field for image:
<input type="file" name="fileToUpload" id="fileToUpload" value="<?php echo $results['article']->img ?>" />
What about something like this?
if(!empty($_FILES['fileToUpload']['name'])) //new image uploaded
{
//process your image and data
$sql = "UPDATE table SET name=?, image=?,... WHERE id = ?";//save to DB with new image name
}
else // no image uploaded
{
// save data, but no change the image column in MYSQL, so it will stay the same value
$sql = "UPDATE table SET name=?,... WHERE id = ?";//save to DB but no change image column
}
//process SQL query in $sql
Make a if clause that checks if fileupload input is empty or not. If it is empty do not execute query.
Use input type="file" without attribute value.
<input type="file" name="fileToUpload" id="fileToUpload" />
Here is example for if clause
if(!empty($_FILES['fileToUpload']['name'])) {
$origFile = $_FILES['fileToUpload']['name'];
$tempFile = $_FILES['fileToUpload']['tmp_name'];
$sizeFile = $_FILES['fileToUpload']['size'];
/* UPLOAD CODE GOES HERE */
}
It worked , really thanks
short solution
check the file is not empty: if(!empty($_FILES['fileToUpload']['name']))
Take update query with mentioning image column in your table in IF statement,
if images not uploaded, then it will upload.
Take update query without mentioning image column in your table in ELSE statement,if file is empty, it will upload
CONCLUSION
Next time when you update any other field the image won't get disappeared, meaning file path wont be empty in your database, and if already present it won't change unless you manually change it..

save chosen file, when form validation fails

I've got a form with some input fiels. One of them is a file upload field.
When the user submits the form, every field will be validated and the page gets reloaded with the error messages, but all other fields still showing the users input. Now I want, that once a user has chosen a file, and the form can't be validated because of other errors, the chosen file still will be uploaded after a resubmission.
I display the entered values with the value property. I know that you can't set the value of an file input, because of security issues.
This is what I've got so far:
<?php
if(isset($fileUpload))
{
echo "<span>selected File: ".$fileUpload["name"]."<span>";
$_FILES["file-upload"]=$fileUpload;
} ?>
<input type="file" class="button" name="file-upload" id="file-upload" accept=".zip"/>
This happens after submission:
if (!isset($_FILES["file-upload"]))
{
$uploadError = "Please choose a file";
$hasError = true;
} else {
$fileUpload=$_FILES["file-upload"];
}
The submitted file is stored in $fileUpload. I tried to set the $_FILES["file-upload"] variable manually, but it doesn't seem to work.
After resubmission, $fileUpload["name"] is empty.
How can I save the selected file, so the user hasn't to reselect it after an unsuccessful submission?
You cant store the file value after loading page like other fields ( the other way is you can upload the file and in return you can pass the uploaded file value in hidden , if user add file again the unlink the previous file otherwise the use the file which uploaded in first step. )

PHP image upload and crop with url

I am using the below upload class for image upload and manipulating.
class.upload.php samples, a files uploading and images manipulation PHP class
Now I want to change form input type="file" to a text field where I will give a Image URL.
How can I make this uploading class to work.
Thanks In Advance.
this class seems to accept url's as arguments as well so just change the input type to text, name to for example image_url and use this line to manipulate the image as you wish after submitting the form:
$handle = new upload($_POST['image_url']);
Get the file name through text box and in background use php curl function to upload the file in the server and your uploading class will work fine
PHP Curl to upload file

How to show a preview of the uploaded image before uploading it

I have created an html form that contact text input fields as well as file input for image uploads. I have already created the submit.php that handles the image file uploaded as well as other text inputs that are typed in by the users and this php file works fine. However, I have also added an option for the user to preview form in the html form page so if they want to preview the form first before submitting it, they are taken to the form_preview.php page that shows them the preview of the completed form along with the forms beneath this preview so they can edit it further if they want. In this preview page, I am having a problem in showing a preview of the uploaded image before uploading it.
Here is my code and on info on what I've tried so far: In the form_preview.php page, I have the following:
<h1>Form Preview:</h1>
<p>Intro: <?php echo $_POST["intro"]; ?> </p>
<p>My Story: <?php echo $_POST["story"]; ?> </p>
<img src="<?php echo $_FILES["file"]["tmp_name"]; ?>" />
The above shows a broken image instead of showing an image. I thought the uploaded image will be stored in a temporary folder and if I added the $_FILES["file"]["tmp_name"]; as the image source it may show that image. Since its not showing, does it mean that this will not work? So is my only other option for this is to process the upload and store in the 'upload' folder first (similar to what I do in the submit.php file) and then add the link to the actual image that was added to this upload folder?

Categories