I have a form with input type file and its not compulsory to upload . But after the submit action occurs I want to check in the controller section if the file input is empty or not. I have a following view page and controller but the method is not working for me .
This is the form fillup page
<form enctype="multipart/form-data" method="post"
action="<?php echo site_url('welcome/do_upload');?>">
username:<input type="text" name="username">
<p>upload file</p>
<input type="file" name="image">
<input type="submit" name="submit" value="submit">
</form>
and this is the controller where am trying to check if the file is selected to upload or not .
if(isset($_FILES['image'])){
echo( "image selected");
} else {
echo("image not selected");
}
the output of the result is always the "image selected" though I don't select any image
The error is when you submit the FORM
$_FILES['image'] will always be created no matter what thus making your conditions true.
You are checking for the wrong conditions here, what you want to do is when a user uploads a file. You might want to do is to check if the $_FILES['image']['name'] length is > 0 or the $_FILES['image']['size'] is > 0.
example, code not for production, just for testing.
if ( strlen($_FILES['image']['name']) > 0){
//yeahhhh!
}
Related
I am facing an issue with the below code, I can upload multiple files but still need that the if condition not be executed till user chooses a file as it is not good to see Unique ID and the echo message run however no files chosen!
PHP
<?php
$path = "Uploads/Files/";
if( (isset($_POST['submit'])) && (!empty ($_FILES['myFile']['name'])) ){
$countfiles = count($_FILES['myFile']['name']);
for($i=0;$i<$countfiles;$i++){
$filename = uniqid(). $_FILES['myFile']['name'][$i];
// Upload file
move_uploaded_file($_FILES['myFile']['tmp_name'][$i], $path.$filename);
echo "<h3> $filename has been uploaded Successfully!</h3>";
}
}
?>
So I want the && part above to be valid as well and not below message to appear till files be chosen.
HTML
<form method='post' enctype='multipart/form-data'>
<input type="file" name="myFile[]" id="file" multiple>
<input type='submit' name='submit' value='Upload'>
</form>
Error:
there are two parts:
add validation for required field <input type="file" name="myFile[]" id="file" multiple required>
render whatever you want instead of echo "<h3> $filename has been uploaded Successfully!</h3>";
I'm using codeigniter for my project and I need to read contents of files. So,I need validation to check whether the file is selected or not.
Here is my code in controller
$this->form_validation->set_rules(
'estimation_file',
'Project Estimation File',
'required'
);
But while choosing a file it shows error saying - The Project
Estimation File field is required
In codeigniter you cannot check the validation of two dimension array or file field with form_validation, instead you can check it after posting the data.
$this->form_validation->set_rules('validation_check','any required field','required');
if($this->form_validation->run()==FALSE)
{
// your code before posting...
}
else
{
// check the file posting
if($_FILES['estimation_file']['name']!='')
{
// if file selected or not empty
}
else
{
// if file not selected | empty | redirect
}
}
do not forget to write enctype="multipart/form-data" within the form field, otherwise your file field will not pass the value of two dimension array.
<form method="post" enctype="multipart/form-data" name="upload_form" action="">
<input type="hidden" name="validation_check" value="TRUE" />
<input type="file" name="estimation_file" value="" />
<input type="submit" value="Post" />
</form>
I was wondering that each time i refresh my page do I have to somehow clear $_FILES , or why is it echoing back to me that the variable is set and also NOT empty? when i first load, or reload the page is obviously is at least empty by using print_r
<html>
<form action="" method="post" enctype="multipart/form-data">
<label for="file">Filename:</label>
<input type="file" name="file" id="file"><br>
<input type="submit" name="submit" value="Submit">
</form>
</html>
<?php
if(isset($_FILES["file"]) && !empty($_FILES["file"])){
echo 'You have just uploaded '. $_FILES["file"]["name"];
}
?>
Actually, You are facing this trouble because, after once you submit by selecting a file the browser caches the page response & when you refresh it by F5 or right click->reload it simply returns the cached result. You can try your old code by reloading it by a dummy GET request it will work fine. For example :
If the url to this page is :http://localhost/file.php then type in the address bar as : http://localhost/file.php/?dummy=abc then you will find your code works correctly... So, you must verify not just $_FILES but $_FILES["file"]["name"] instead, would go fine...
Try this, you wont have any trouble....
<html>
<form action="" method="post"
enctype="multipart/form-data">
<label for="file">Filename:</label>
<input type="file" name="file" id="file"><br>
<input type="submit" name="submit" value="Submit">
</form>
</html>
<?php
if(isset($_FILES["file"]) && !empty($_FILES["file"]["name"])){
echo 'You have just uploaded '. $_FILES["file"]["name"];
}
?>
check the form has been posted,else your code while page loading
<?php
if($_POST['submit']=="submit"){
if(isset($_FILES["file"]) && !empty($_FILES["file"])){
echo 'You have just uploaded '. $_FILES["file"]["name"];
}
}
?>
action="" means you post the data to the current page, but after you post the data, when you refresh your page, the browser will ask you whether to repost the data, and if you press yes, then the data will be posted again.
The right solution is redirect to the current page after post.
The Answer by #Karthick Kumar is wrong because when there is an <input type="file" name="file">, even if no file selected by user, the $_FILES["file"] would never be empty (use print_r() and see it for yourself)!
#Anirban's answer is completely true, I'm just gonna explain it in a more convincing way:
echo( !empty($_FILES["file"]["name"]) ); //works as expected
echo( !empty($_FILES["file"]["type"]) ); //works as expected
echo( !empty($_FILES["file"]["tmp_name"]) ); //works as expected
echo( $_FILES["file"]["size"] > 0 ); //more convincing way!
And the best way would be:
echo( $_FILES["file"]["error"] == 0 ); //you can check the cause of error this way!
Link to the list of error codes
I have a form in which the user has to input some text and an image.
<?php
if(isset($_POST['name']))
{
echo "name";
if(isset($_FILES['image']))
echo "image";
}
echo <<<F
<form method="post" action="test.php" enctype="multipart/form-data">
<input type="text" name="name">
<input type="file" name="image">
</form>
F;
?>
In this sample, even if i don't choose any image, the text "image" gets echoed. What modifications should i make such that "image" is only echoed when i select an image, if not, the rest of the form gets submitted.
Change with
if(is_uploaded_file($_FILES['image']['tmp_name'])) echo "image";
You can try it like this:
if (isset($_FILES['image']) && $_FILES['image']['error'] == 0)
Error field contains a PHP error code, it equals to 4 when no file was uploaded.
You can find more information here: http://php.net/manual/en/features.file-upload.errors.php
I am trying to create an avatar uploader, but I want to be able to click only one button that opens the browsing window and then automatically calls the upload method from the AvatarUpload class.
The problem I have identified is that the form doesn't seem to be posting anything.
Any help would be great!
var_dump(isset($_POST['uploaded']));
if( isset($_POST['uploaded']) )
{
$img = new AvatarUpload();
$img->startUpload();
}
else
{
?>
<form method="post" enctype="multipart/form-data" name="uploadAvatar">
<p>
<input type="file" name="uploaded" id="file" onchange="this.form.submit()" />
<p>
</form>
<?php
}
?>
when you upload files they are listed under the $_FILES, not $_POST.