I made a form at HTML with an option to upload an image. The problem is that when I check the $_FILES array at the php code, it is not found.
The HTML form:
<form method="POST" action="addProduct.php" onsubmit="return checkValidation()">
Image(optional): <input type="file" name="fileImg" accept="image/*" id="fileImg">
<input id="submitProduct" type="submit" name="submitBtn" value="Submit product" onclick="checkValidation()"/>
</form>
The checkValidation() function:
function checkValidation(){
var regex = /\.(jpg|JGP|jpeg|JPEG|png|PNG|gif|GIF)$/;
if (($('#fileImg').val()) && (!(regex.test($('#fileImg').val()))))
{
return false;
}
}
And the php code:
if(isset($_POST["submitBtn"])){
if(!(empty($_FILES)))
{
if (file_exists('uploads/'.$_FILES["fileImg"]["name"]))
{
echo $_FILES["fileImg"]["name"]." already exists. ";
}
else
{
move_uploaded_file($_FILES["fileImg"]["tmp_name"], 'uploads/'. $_FILES["fileImg"]["name"]);
$path = $_FILES["fileImg"]["name"];
}
}
else
$path = 'no_photo.jpg';
The problem is that at the php code, it always jumps to the else branch (which gives me some default image).
Thanks in advance.
Your form lacks the enctype='multipart/form-data' attribute. Without it, $_FILES would always be empty.
Read more about it here: What does enctype='multipart/form-data' mean?
Related
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
}
I am trying to retrieve the path of an image that I upload in the html page using php, but when i use the method $_FILES['file1']['tmp_name'] it displays an error saying "undifined index: file1".
This is the code I am trying:
HTML:
<form name="form1" method="GET" action="image.php">
<br/>
<input type="file" name="file1"/>
<input type="submit" value="submit"/>
</form>
PHP:
<?php
$files=$_FILES['file1']['tmp_name'];
$folder="images";
if(is_uploaded_file($_FILES['file1']['tmp_name']))
{
move_uploaded_file($_FILES['file1']['tmp_name'], $folder . '/'.$_FILES['file1']['name']);
$image=$folder . '/'.$_FILES['file1']['name'];
echo "Successfully Uploaded";
echo $image;
}
else
die("Not Uploaded");
?>
Uploading files require a POST method and not a GET.
Consult:
http://php.net/manual/en/features.file-upload.post-method.php
Also make sure the folder you are wanting to upload to, has proper permissions to write to it.
Sidenote edit: Plus, as stated in the other answer, a valid enctype is required.
I'd also use:
if ($_SERVER['REQUEST_METHOD'] == 'POST'){...}
In order to ensure of a POST method, while using full bracing for your else and adding another else{...} for the above.
if ($_SERVER['REQUEST_METHOD'] == 'POST'){
$files=$_FILES['file1']['tmp_name'];
$folder="images";
if(is_uploaded_file($_FILES['file1']['tmp_name']))
{
move_uploaded_file($_FILES['file1']['tmp_name'], $folder . '/'.$_FILES['file1']['name']);
$image=$folder . '/'.$_FILES['file1']['name'];
echo "Successfully Uploaded";
echo $image;
} else {
die("Not Uploaded");
}
} else{
echo "A POST method was not used here.";
}
Also a conditional !empty() against your file input.
http://php.net/manual/en/function.empty.php
Change:
<form name="form1" method="GET" action="image.php">
to
<form name="form1" method="POST" enctype="multipart/form-data" action="image.php">
pls i need help am trying to use the mkdir for user profile pic upload and its not working ... but when i use it normally i.e ordinarilly for example when i created a new php page and used " mkdir("newdir") "it worked
<?php
if (isset($_FILES['profilepic'])) {
if (((#$_FILES["profilepic"]["type"]=="image/jpeg") ||
(#$_FILES["profilepic"]["type"]=="image/png") || (#$_FILES["profilepic"]
["type"]=="image/gif"))&&(#$_FILES["profilepic"]["size"] < 1048576)) {
//1 Megabyte
$chars = "abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789";
$rand_dir_name = substr(str_shuffle($chars), 0, 15);
mkdir("userdata/profile_pics/$rand_dir_name");
}
else{
}
}
?>
<p>UPLOAD YOUR PROFILE PHOTO:</p> <br />
<form action="" method="POST" enctype="multipart/formdata">
<img src="./img/default_pic.png" width="70" />
<input type="file" name="profilepic" /><br /><br />
<input type="submit" name="uploadpic" value="Upload Photo"><br />
</form>
its not because of php, the php code is written well the problem is in the html where the html form enctype is written wrong
this
<form action="" method="POST" enctype="multipart/formdata">
should be
<form action="" method="POST" enctype="multipart/form-data">
Edit some code for your code with this:
Notice that it is best practise to use the variable outside of the string. Also you place the code in an if statement to check it executed correctly.
edit
You also need to start the mkdir call with a / or even better start it with $_SERVER['document_root'] so you know you always get an absolute path to the directory you want to generate.
if(mkdir("/userdata/profile_pics/".$rand_dir_name)){
print "this worked!";
}
else {
print "This failed";
}
Also you need to check that the permissions for the parent directory are correct, that the PHP usergroup is allowed to create directories in the /profile_pics/ folder.
You can also check that PHP is in the correct working directory, you can check this with print gwtcwd();
May be you should try like this (variable name along with a single-inverted comma)
mkdir("userdata/profile_pics/'$rand_dir_name'");
I hope it will work.
there occured a suspect problem during my image upload, i´ll show you:
<form action="check.php" method="POST">
File: <input type="file" name="picture" value="" id="picture-field">
<span class="error" id="file-error"></span>
<br><br>
<input type="submit" name="submit" value="send">
</form>
my check.php:
require 'ImageChecker.php';
if (!$_FILES)
echo "exit because no FILES";
$imageChecker = new ImageChecker();
$imageError = "";
if(!$imageChecker->php_error($_FILES['picture']['error'])) {
$imageError = "php error ocurred!";
echo $imageError;
the class ImageChecker.php:
class ImageChecker {
// function for php-error-check
public function php_error ($php_error) {
if ($php_error === UPLOAD_ERR_OK) {
return TRUE;
} else {
return FALSE;
}
}
// more functions..
}
every time, i upload an image, i get:
exit because no FILES
and
php error ocurred!
WHY ? what did i wrong? really need your help, thanks and greetings!!
In the form tag you need to give
enctype="multipart/form-data"
<form action="check.php" method="POST" enctype="multipart/form-data">
this is very important while uploading files
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.