Check if image has been selected for upload - php

I have a form were a user can enter text and also upload images.
Saving the values and images works fine. If the user does not select an image, I get the error message that no file has been selected (Error 4).
How can I run the upload image part only if the user selected an image?
I have tried:
if (!empty($_FILES['files']['name'])){ Upload the Image }
if (!empty($_FILES['files'])){ Upload the Image }
if (!empty($_FILES['files']['name'])) { Upload the Image }
if ($_FILES['files']['error'] == UPLOAD_ERR_OK) { Upload the Image }
This the form:
<input type="file" name="files[]" title="Title" maxlength="10" accept="gif|jpg|jpeg|png|tiff">

Use is_uploaded_file() function to check if the user has uploaded any file or not, and then process inputs accordingly, like this:
if(is_uploaded_file($_FILES['files']['tmp_name'][0])){
// user has uploaded a file
}else{
// user hasn't uploaded anything
}
Above solution code is based on your name attribute of input tag,
<input ... name="files[]" ... />
If it was <input ... name="files" ... /> then the if condition would be like this:
if(is_uploaded_file($_FILES['files']['tmp_name'])){
...
}else{
...
}
Sidenote: Use var_dump($_FILES); to see the complete array structure.

Related

How to direct the uploaded image to the file i want

i'm asking user to input the image as;
Fotoğraf <input type="file" id="foto" name="foto" accept="image/*">
<br> <br> <br>
Then i am storing that data into my database with the following;
$j=$_POST['foto'];
$sql = "INSERT INTO customer_list (ad_soyad,telefon,e_posta,cari_kart_kodu,olusturma_tarihi,guncelleme_tarihi,sifre,foto2) VALUES('$a','$b','$c','$d','$e','$f','$h','$j')";
And then i am listing the files and the image i get from the user in the function below;
$sql = "SELECT ad_soyad,id,telefon,e_posta,cari_kart_kodu,olusturma_tarihi,guncelleme_tarihi,foto2 FROM customer_list";
$tdStyle='background-color:grey;';
echo "<td style=\"$tdStyle\"> <img src = ".$row['foto2']." width=200 height=200 ></td>";
However when user uploads the image,i want it to upload the image into my C:\xampp\htdocs\ea file.
Tried this Storing images in MySQL couldn't do it properly.Appriciated for the help.
you must set the enctype in form like this to upload the file
<form action="post" enctype="multipart/form-data">
<input type="file" id="foto" name="foto" accept="image/*">
</form>
to access the uploaded file in the action page you need to do this instead of your code
$j=$_POST['foto']; // you can't access file in $_POST
Use $_FILES to get file data
$j=$_FILES["foto"]["name"]; // get the name of image
echo $j; // print the image name just to check
upload the file to your destination if you want to upload the file in C:\xampp\htdocs\ea folder. If your form file exists in this format then use below code htdocs-
ea (your image path)
yourfoldername -
-form.php (your form)
if(is_uploaded_file($_FILES["foto"]["tmp_name"])){
move_uploaded_file($_FILES["foto"]["tmp_name"], $path1="../ea/".$_FILES["foto"]["name"])or die("couldn't copy.");
}
after file upload save the data to database
$sql = "INSERT INTO customer_list (ad_soyad,telefon,e_posta,cari_kart_kodu,olusturma_tarihi,guncelleme_tarihi,sifre,foto2) VALUES('$a','$b','$c','$d','$e','$f','$h','$j')";
Maybe try this solution for file upload with php.

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>";
}
}
?>

PHP image file upload error

In my form I have
<td>Boiler Image:</td>
<input type ="hidden" name="MAX_FILE_SIZE" value="1000000" />
<td><input type="file" name="boiler_image" id="boiler_image" /></td>
In my php code I have
if (is_uploaded_file($_FILES['boiler_image']['twp_name'])){
if (!move_uploaded_file($_FILES['boiler_image']['twp_name'], $upfile)){
echo 'Problem: Could not move file to destination directory';
exit;
}
}
else {
echo 'Problem: Possible file upload attack. Filename: ';
echo $_FILES['boiler_image']['name'];
exit;
}
Whenever I try to upload an image I get 'Problem: Possible file upload attack. Filename:' Did I happen to set up the input form incorrectly?
Ensure you have set the enctype="multipart/form-data" attribute on your form. It is required for all forms that have file uploads.
The temporary filename of the file in which the uploaded file was stored on the server is located at key "tmp_name". So, change your instances of twp_name to tmp_name.
$_FILES['boiler_image']['tmp_name']
Ref: http://php.net/manual/en/features.file-upload.post-method.php

checking path in upload file and display

I have 2 php files that display uploaded picture.
first is form_upload.php
<html>
<head><title>File Upload</title></head>
<body>
<ol>
<li>Enter the file name of the product picture you want to upload or the the browse button to navigate to the picture file</li>
<li>when the path to the picture file shpws in the text field, click the upload picture</li>
</ol>
<form enctype="multipart/form-data" action="uploadFile.php" method="POST">
<input type="hidden" name="MAX_FILE_SIZE" value="500000"/>
<input type="file" name="pix" size="60"/>
<p><input type='submit' name="Upload" value="Upload picture"/></p>
</form>
</body>
</html>
second is uploadedFile.php
<?php
if(!isset($_POST['Upload']))
{
include("form_upload.php");
}
else
{
if($_FILES['pix']['tmp_name']=="") //check wether the file is larger than 2mb or not
{
echo "file did not successfully upload. Check the file size. File must be less than 500K";
include("form_upload.php");
exit();
}
if(!preg_match("/image\/jpeg/",$_FILES['pix']['type']))
{
echo "only jpg files are allowed. Please try another file";
include("form_upload.php");
exit();
}
else
{
$destination='C:\xampp\htdocs\test\hinh\ '.$_FILES['pix']['name'];
$temp_file=$_FILES['pix']['tmp_name'];
move_uploaded_file($temp_file,$destination);
echo "<p>the file has successfully uploaded :{$_FILES['pix']['name']} {$_FILES['pix']['type']} ({$_FILES['pix']['size']}) </p>";
echo "<img src='C:\\xampp\\htdocs\\test\hinh\\{$_FILES['pix']['name']}' alt='picture'><br/>";
echo "C:\\xampp\\htdocs\\test\hinh\\{$_FILES['pix']['name']}";
}
}
?>
I can not display the image after uploading. I checked the path is correct. But when i put
it in tag it doesn't display the picture here is my syntax:
echo "<img src='C:\\xampp\\htdocs\\test\hinh\\{$_FILES['pix']['name']}' alt='picture'><br/>";
You should not reference the file with a filesystem path on drive "C". You should instead use a HTTP link, preferredly a relative link, like so:
echo "<img src='/path/to/upload/{$_FILES['pix']['name']}' alt='picture'><br/>";
I don't know how that path is named on your webserver. You have to find out yourself what is right. I guess it might be /test/hinh/.

Image upload failing in PHP

So I'm trying to set up an image upload from a form via php on my localhost and after running the code and connecting to the database okay, I'm getting the error for the upload. Since all of the other parts of the form are working after a section by section check, I'll just add the html for the upload input and its relevant php script.
<input type="file" name="image" id="image-select" />
And the portion of the php that has to deal with the image upload and verification after upload:
$image = $_FILES ['image']['name'];
$type = #getimagesize ($_FILES ['image']['tmp_name']);
//Never assume image will upload okay
if ($_FILES['image']['error'] !== UPLOAD_ERR_OK) {
die("Upload failed with error code " . $_FILES['image']['error']);
}
//Where the file will be placed
$target_path = "uploads/";
// Add the original filename to target path
$path= $target_path . basename($image);
// Check if the image is invalid before continuing
if($type === FALSE || !($type[2] === IMAGETYPE_TIFF || $type[2] === IMAGETYPE_JPEG || $type[2] === IMAGETYPE_PNG)) {
echo '<script "text/javascript">alert("This is not a valid image file!")</script>';
die("This is not a valid image file!");
} else {
move_uploaded_file($_FILES['image']['tmp_name'], $path);
}
So error appears once the code hits the upload process. I was attempting to upload a small PNG file The relevant code I added is also in their respective orders when they appear in the script.
can you please put error here. If error means that after submit page file is not being upload. then please check your form enctype. see below an example
<form action="" method="post" enctype="multipart/form-data">
<input type="file" name="image" id="image-select" />
<input type="submit" value="Submit">
</form>
Just a wild stab in the dark here as you haven't provided your form tag but you have remembered the enctype attribute haven't you?
Sorry I don't have 50 reputation otherwise I would ask this as a comment.
<form enctype='multipart/form-data' action=''>
<input type="file" name="image" id="image-select" />
</form>
Also you should check if the file uploaded OK before calling getimagesize() (not that this would be the source of your issue)
if you are using PHP5 and Imagick, then you can try to use something like the following:
$image->readImageFile($f);

Categories