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
Related
I really Dont know What should I do the problem is that $_Files is always empty when I try to upload a file.
<form method="get" action="pic.php" enctype="multipart/form-data">
<input type="file" name="file">
<input type="submit" name="submit">
</form>
<?php
$name=$_FILES["file"]["name"];
$tmp_name=$_FILES["file"]["tmp_name"];
echo "php is talking";
echo $name;
if(isset($name)&&!empty($name))
{
echo "OK";
$location="uploads/";
if(move_uploaded_file($name,$location.$name))
{
echo "the file has been Uploaded";
}
}
?>
You have to change the HTTP method in the form from GET to POST
<form method="POST" action="pic.php" enctype="multipart/form-data">
I have a form where multiple images can be selected...
HTML
<form action="" method="post" enctype="multipart/form-data">
<input type="text" name="name" class="form-field" placeholder="First Name">
<input type="file" name="files[]" multiple accept="image/*" class="form-field">
<input type="submit" value="UPLOAD" class="button" name="submit4">
</form>
Then, in my PHP I want to detect if at least 1 file has been uploaded.... I tried this, but the code still triggers as if a file was uploaded...
if(isset($_POST['submit4']) and $_SERVER['REQUEST_METHOD'] == "POST")
{
echo "post sucessful<br>";
if(is_uploaded_file($_FILES['files']['temp_name']))
{ echo "file exist";}
else{ echo "no file";}
}
THIS always triggers the "no file" even if there is a file..
and if I try:
if(!empty($_FILES['files']['temp_name']))
it always triggers the "no file" ... I'm really confused...
You can use following code
if(!file_exists($_FILES['files']['tmp_name']) || !is_uploaded_file($_FILES['files']['tmp_name'])) {
echo "No File";
}
else{
echo "Successfully uploaded";
}
Found my answer...
if(!empty($_FILES['files']['name'][0]))
works quit well... thanks everyone.
So, I have a form with two input elements: one to upload a file, and the other, a hidden input. Here it is:
<form action="upload.php" method="POST">
upload: <input enctype="multipart/form-data" name="pp" accept="image/png" type="file">
<input type="submit" value="go" />
<input type="hidden" name="eup" />
</form>
on my "upload.php" page (which is a different page), I get the undefined index error. Here's the code for that:
<?php
session_start();
if(isset($_POST["eup"])){
$fERR=false;
if(isset($_FILES["pp"])){ // undefined index error comes if this IF is removed...
$aExts = array("png");
$temp = explode(".", $_FILES["pp"]["name"]);
$tEXT = end($temp);
if ((($_FILES["pp"]["type"]=="image/x-png")
|| ($_FILES["pp"]["type"]=="image/png"))
&& ($_FILES["pp"]["size"]<300000)
&& in_array($tEXT, $aExts)){
if ($_FILES["pp"]["error"]>0){
$ppERR=true;
}
else{
// handle file upload here
}
}
else{
$fERR=true;
}
}
else{
$fERR=true;
}
if($fERR==true){
echo "ERROR";
}else{
echo "GOOD TO GO";
}
}
echo "<br />".ini_get("file_uploads");
?>
I've looked at a bunch of other posts and websites discussing this topic, but none of the solutions worked for me.
by the way, the output of that php is:
ERROR
1
<form action="upload.php" method="POST" enctype="multipart/form-data">
upload: <input name="pp" accept="image/png" type="file">
<input type="submit" value="go" />
<input type="hidden" name="eup" />
</form>
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?
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.