multipart form-data with text and image - php

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

Related

How to check if $_POST[image] is set

Re asking how to check if $_POST[FILE] isset
I have a file input and if I submit my form without an image I want something to happen if I uploaded a file in the input I want something different to happen.
if (!isset($_POST[image])) { }
seems to trigger regardless of whether or not I have uploaded a file in the input or not.
<label>
<p>Profile Picture:</p>
<input type="file" name="image" value="" />
</label>
My last question was marked as a duplicate of this answer Check whether file is uploaded however
if (!file_exists($_FILE['image'])) { }
didn't work either it is still showing truthy even when an image is uploaded. So not the answer I need.
To check if there is a file uploaded is you need to check the size of the file.
Then to check if its an image or not is you need to use the getimagesize() function. See my script below:
HTML:
<form action="index.php?act=s" method="post" enctype="multipart/form-data">
<input type="file" name="image" value=""/>
<input type="submit">
</form>
PHP:
<?php
if(isset($_GET['act'])){
// Check if there is a file uploaded
if($_FILES["image"]["size"]>0){
echo "There is a file uploaded<br>";
// Check if its an image
$check_if_image = getimagesize($_FILES["image"]["tmp_name"]);
if($check_if_image !== false) {
echo "Image = " . $check_if_image["mime"] . ".";
} else {
echo "Not an image";
}
}
else{
echo "There is NO file uploaded<br>";
}
}
?>

Using HTML POST to upload file via PHP

I am trying to upload a local file to webserver using HTML POST method and PHP.
this is my php code:
<?php
if (isset($_POST["submit"])) {
$updir = "/var/tmp/";
$upfile = $updir.basename($_FILES['rawexcel']['name']);
if(is_uploaded_file ($_FILES ["rawexcel"]["tmp_name"]))
{
move_uploaded_file ($_FILES["rawexcel"]["tmp_name"], $upfile);
} else {echo "error uploading file ".$upfile;}
} else {echo "not isset post method";}
?>
and HTML code is:
<div class="container" id="upl">
<h4> Upload files</h4>
<form action="upl.php" enctype="mutipart/form-data" method="post">
<p> upload your files to DB</p>
<p><input type="file" name="rawexcel" id ="rawexcel">
<input type ="submit" value="Upload" name ="submit"></p>
</form>
</div>
$_FILES["rawexcel"]["error"] shows 0 and from running this peice of code i get
error uploading file /var/tmp
I guess file name was not retrieved from html?
Error is in enctype:
enctype="multipart/form-data"
not:
enctype="mutipart/form-data"
You have typo mistake in enctype="multipart/form-data" , instead of this you typed enctype="mutipart/form-data" . So "mutipart" spelling is need to correct.

How to upload 2 different file types via the same form php [duplicate]

This question already has answers here:
Multiple file upload in php
(14 answers)
Closed 6 years ago.
my form looks like this
<form action='uploadFile.php' method='POST'>
<p>select an image</p>
<p style='font-size: 10px'>280x280px</p>
<input type='file' name='imageFile' />
<p>select it</p>
<input type='file' name='uploadFile'/>
<p>title it</p>
<input type='text' id='post-title' name='title'/>
<p>tag it</p>
<input type='text' id='post-tags' name='tags'/><br />
<button id='submit-file'>submit it</button>
</form>
the php back end looks like
if(isset($_FILES['uploadFile']['tmp_name']))
{
if(isset($_FILES['uploadFile']['size']))
{
and
if(isset($_FILES['imageFile']['size']))
{
if(isset($_FILES['imageFile']['tmp_name']))
{
basically long story short; on submission the else statement to the if(isset($_FILES['uploadFile']['tmp_name']) and if(isset($_FILES['imageFile']['size'] is triggered (confirmed via echo statements) on both regardless of which is submitted . any idea on what would cause this?
Your html form tag must contain enctype="multipart/form-data" to upload a file.
You have missed enctype in your tag. It requires enctype="multipart/form-data" to upload files.
<form action="uploadFile.php" method="POST" enctype="multipart/form-data">
The following PHP helps to find whether file is given for upload or not:
// For imageFile
if (isset($_FILES['imageFile']) && $_FILES['imageFile']['size'] != 0) {
// input file selected for upload)
} else {
// input file not selected for upload
}
// For uploadFile
if (isset($_FILES['uploadFile']) && $_FILES['uploadFile']['size'] != 0) {
// input file selected for upload)
} else {
// input file not selected for upload
}

checking if the file upload is chosen or not in codeigniter

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!
}

<input type=file> not getting image

I am trying to get image file name by tag, But when I check it through isset($_FILES['imgFile']), it returns always false.
Here is my HTML tag for getting image file:
<input type="file" name="imgFile" accept="image/*" id="imgFile" src=""/>
Here is my php code to retrieve it:
if(isset($_FILES['imgFile']))
{
$img = $_FILES['imgFile']['name'];
echo $img";
}
else
{
echo "Image not set";
}
It always generate "Image not set" as an output though I have selected an image.
Are you using the correct enctype on the form?
<form enctype="multipart/form-data">
This is required when using a file upload element.
just use:
enctype="multipart/form-data"
Say this is your form:
<form action="same_page.php" method="post" enctype="multipart/form-data">
<input type="file" name="imgFile" accept="image/*" id="imgFile" src=""/>
<input type="submit" name="upload" value="Upload" />
</form>
and your php code to retrive name of the image is in the same page where the form is, then your code should be like this:
<?php
if (isset($_POST["upload"])) {
if (isset($_FILES['imgFile'])) {
$img = $_FILES['imgFile']['name'];
echo $img;
} else {
echo "Image not set";
}
}
?>
But if your php code is in another page, then you only need to use enctype="multipart/form-data" in the form as mentioned in the other answers.

Categories