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.
Related
I'm trying to upload a pdf file in my Web Host database but it doesn't work .
I have a php file : upload.php
<form action="<?php print $PHP_SELF?>" enctype="multipart/form-data" method="post">
Last name:<br /> <input type="text" name="name" value="" /><br />
class notes:<br /> <input type="file" name="classnotes" value="" /><br />
<input type="submit" name="submit" value="Submit Notes" />
</form>
<?php
define ("filesplace","uploads/"); //uploads is the folder in my server database
if (is_uploaded_file($_FILES['classnotes']['tmp_name'])) {
if ($_FILES['classnotes']['type'] != "application/pdf") {
echo "<p>Class notes must be uploaded in PDF format.</p>";
} else {
$name = $_POST['name'];
$result = move_uploaded_file($_FILES['classnotes']['tmp_name'], filesplace."/$name.pdf");
if ($result == 1) echo "<p>Upload done .</p>";
else echo "<p>Sorry, Error happened while uploading . </p>";
}
}
?>
It shows that the file is being uploaded but in the end it shows the error message : Sorry, Error happened while uploading
The file upload.php has all the permissions (777) as well as the uploads folder has.
Im am a free client in 000Webhost , this could be a problem ?
Could you please help me with that situation? :D
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">
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>
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.