$_FILES undefined PHP upload form. Can't figure out [duplicate] - php

This question already has answers here:
Closed 10 years ago.
Possible Duplicate:
Getting ‘undefined index’ error while trying to use $_FILE in PHP
I have made a php upload form for images. It uses session variables to determine the upload directory. There are two options for the uploads, the slider dir or the side dir so I have an if statement that determines the directory. If i remove this from the form then the whole thing works ok however with it in, $_FILES seems to not be declared and returns as an undefined index error.
The code can be found is as follows:
upload.php
<?php
include("resize-class.php");
$allowedExt = array('jpg', 'jpeg', 'JPG', 'JPEG');
$tmps = explode(".", $_FILES['file']['name']);
$extension = end($tmps);
session_start();
if ($_POST['dir'] == 'side'){
$dirent = $_SESSION['sideDir'];
}
else if($_POST['dir'] == 'slider'){
$dirent = $_SESSION['sliderDIR'];
}
else{
die();
}
echo $_POST['dir'];
print_r($_FILES);
if (($_FILES["file"]["type"] == "image/jpeg")&& ($_FILES["file"]["size"] < 4000000000)&& in_array($extension, $allowedExt)) {
if ($_FILES["file"]["error"] > 0) {
echo "Error: " . $_FILES["file"]["error"] . "<br />";
echo 'here';
} else {
echo "Upload: " . $_FILES["file"]["name"] . "<br />";
echo "Type: " . $_FILES["file"]["type"] . " <br />\n";
echo "Size: " . ($_FILES["file"]["size"] / 1024) . " Kb<br /> \n";
echo "Stored in: " . $_FILES["file"]["tmp_name"]. "<br />\n";
}
if (file_exists($dirent. $_FILES["file"]["name"])) {
echo $_FILES["file"]["name"] . "already exists";
} else {
$fName = $_FILES["file"]["name"];
$tmpname = $_FILES["file"]["tmp_name"];
move_uploaded_file($_FILES["file"]["tmp_name"], $dirent . $_FILES["file"]["name"]);
$number = FileCounter($dirent);
echo "Number of images in DIR: " . $number. " <br />\n ";
$number +1;
$resizeObj = new resize($dirent.$fName);
$resizeObj -> resizeImage(250, 150, 'crop');
$resizeObj -> saveImage($dirent.$number.".jpg", 100);
unlink ($dirent.$_FILES["file"]["name"]);
}
} else {
echo "Error: " . $_FILES["file"]["error"] . "<br />";
}
function FileCounter($dir) {
$counter = 0;
$iterator = new DirectoryIterator(dirname($dir));
foreach ($iterator as $fileinfo) {
if ($fileinfo->isFile()) {
if ($fileinfo->getExtension() == "jpg") {
$counter++;
echo $counter . "\n";
}
}
}
return $counter;
}
?>
HTML FORM:
<form action="includes/upload.php" method="post">
<label for="file">Filename:</label>
<input type="file" name="file" id="file" />
<div class="styled-select">
<label for="dir"> Upload to:</label>
<select size="2" name="dir" multiple="yes" id="dir">
<option value="side" >Side Images</option>
<option value="slider" >Slider Images</option>
</select>
</div>
<br />
<input type="submit" name="submit" value="Submit" />
</form>
I am guessing that there is either a really stupid error in my code that I am overlooking as I have been staring at it for an hour now or that there is something that I do not know about $_FILES and $_POST. (or possibly I have coded the form like a moron!).

Your form is missing
enctype="multipart/form-data"

Related

how to create multiple file upload in php

I'm a beginner in php and now doing a project in php. I want to upload images(maximum four image files only)
.I used the following code to upload images.
<?php if(isset($_POST['submit'])) {
$count=count($_FILES["images"]["name"]);
for($i=0;$i<$count;$i++) { if ((($_FILES["images"]["type"][$i] == "image/gif") || ($_FILES["images"]["type"][$i] == "image/jpeg") || ($_FILES["images"]["type"][$i] == "image/pjpeg")) && ($_FILES["images"]["size"][$i] < 100000)) {
if ($_FILES["images"]["error"][$i] > 0) { echo "File Error : " . $_FILES["images"]["error"][$i] . "<br />"; } else { echo "Upload File Name: " . $_FILES["images"]["name"][$i] . "<br />"; echo "File Type: " . $_FILES["images"]["type"][$i] . "<br />"; echo "File Size: " . ($_FILES["images"]["size"][$i] / 1024) . " Kb<br />";
if (file_exists("public/images/".$_FILES["images"]["name"][$i] )) { echo "<b>".$_FILES["images"]["name"][$i] . " already exists. </b>"; } else {
move_uploaded_file($_FILES["images"]["tmp_name"][$i] ,"public/images/". $_FILES["images"]["name"][$i] );
echo "Stored in: " . "public/images/" . $_FILES["images"]["name"][$i] ."<br />"; ?> Uploaded File:<br> <img src="public/images/<?php echo $_FILES["images"]["name"][$i] ; ?>" alt="Image path Invalid" > <?php } } }else { echo "Invalid file detail ::<br> file type ::".$_FILES["images"]["type"][$i] ." , file size::: ".$_FILES["images"]["size"][$i] ; } } }?>
First: Please, learn to indent!
Your count is bad. You are counting the $_FILES['images']['name'] size, but you need to count how many $_FILES['images'] there are. So change your code like this (note the new position of [$i] keys):
<?php
if (isset($_POST['submit'])) {
$count = count($_FILES["images"]);
for ($i = 0; $i < $count; $i++) {
if ((($_FILES["images"][$i]["type"] == "image/gif") || ($_FILES["images"][$i]["type"] == "image/jpeg") || ($_FILES["images"][$i]["type"] == "image/pjpeg")) && ($_FILES["images"][$i]["size"] < 100000)) {
if ($_FILES["images"][$i]["error"] > 0) {
echo "File Error : " . $_FILES["images"][$i]["error"] . "<br />";
} else {
echo "Upload File Name: " . $_FILES["images"][$i]["name"] . "<br />";
echo "File Type: " . $_FILES["images"][$i]["type"] . "<br />";
echo "File Size: " . ($_FILES["images"][$i]["size"] / 1024) . " Kb<br />";
if (file_exists("public/images/" . $_FILES["images"][$i]["name"])) {
echo "<b>" . $_FILES["images"][$i]["name"] . " already exists. </b>";
} else {
move_uploaded_file($_FILES["images"][$i]["tmp_name"], "public/images/" . $_FILES["images"][$i]["name"]);
echo "Stored in: " . "public/images/" . $_FILES["images"][$i]["name"] . "<br />";
?> Uploaded File:
<br>
<img src="public/images/
<?php echo $_FILES["images"][$i]["name"]; ?>" alt="Image path Invalid" >
<?php
}
}
} else {
echo "Invalid file detail ::<br> file type ::" . $_FILES["images"][$i]["type"] . " , file size::: " . $_FILES["images"][$i]["size"];
}
}
}?>
This code works assuming your html is something like this:
<input type="file" name="images[]" />
<input type="file" name="images[]" />
<input type="file" name="images[]" />
<input type="file" name="images[]" />
<input type="file" name="images[]" />
<input type="file" name="images[]" />

$_FILES not returning any value [duplicate]

This question already has answers here:
Image - Upload not responding, no access to $_FILES
(4 answers)
Closed 8 years ago.
I am trying to upload a file but the $_FILES variable is not returning any value from the form.
The top code is of the HTML form which is accepting the file and the bottom one is of the file to which the form is redirecting.
When I am trying to print the json encoded value of $_FILES is coming out to be empty.
HTML code:
<form id='text' action ='http://getlegal.in/registration-details/' method = 'POST' enctype='multipart/form-data'>
<input type='hidden' name='MAX_FILE_SIZE' value='12412412' />
<label for='file'>Profile Pic :</label>
<input type='file' name='file' id='file' required='required'><br>
<Input type='submit' name='option' value='Submit'>
</form>
PHP code:
$allowedExts = array("jpeg", "jpg", "png");
$temp = explode(".", $_FILES["file"]["name"]);
$extension = end($temp);
//echo "yaba daba doo ";
//echo json_encode($temp);
echo json_encode($_FILES);
echo $_FILES['file']['name'];
die();
if (
$_FILES["file"]["type"] == "image/jpeg"
|| $_FILES["file"]["type"] == "image/jpg"
|| $_FILES["file"]["type"] == "image/pjpeg"
|| $_FILES["file"]["type"] == "image/x-png"
|| $_FILES["file"]["type"] == "image/png"
&&($_FILES["file"]["size"] < 500000000 )
&& in_array($extension, $allowedExts))
{
if ($_FILES["file"]["error"] > 0)
{
echo "Error: " . $_FILES["file"]["error"] . "<br>";
}
else
{
echo "Upload: " . $_FILES["file"]["name"] . "<br>";
echo "Type: " . $_FILES["file"]["type"] . "<br>";
echo "Size: " . ($_FILES["file"]["size"] / 1024) . " kB<br>";
echo "Stored in: " . $_FILES["file"]["tmp_name"];
echo "Temp file: " . $_FILES["file"]["tmp_name"] . "<br>";
if (file_exists("upload/" . $_FILES["file"]["name"]))
{
echo $_FILES["file"]["name"] . " already exists. ";
}
else
{
move_uploaded_file($_FILES["file"]["tmp_name"],
"upload/" . $_FILES["file"]["name"]);
echo "Stored in: " . "upload/" . $_FILES["file"]["name"];
}
}
} else {
echo "Invalid file<br>";
echo "Please uplaoad a valid file. Chech the file type(jpg, png, jpeg) and the size (500kb)";
echo $_FILES["file"]["name"];
echo $_FILES["file"]["size"];
die ();
TRY:
$out = array_values($FILES);
json_encode($out);

i got Undefined index: file error in pdf php

<?php
$allowedExts = array("pdf");
$temp = explode(".", $_FILES["file"]["name"]);
$extension = end($temp);
if ((($_FILES["file"]["type"] == "application/pdf") &&
($_FILES["file"]["size"] < 20000000)
&& in_array($extension, $allowedExts)))
{
if ($_FILES["file"]["error"] > 0)
{
echo "Return Code: " . $_FILES["file"]["error"] . "<br>";
}
else
{
echo "Upload: " . $_FILES["file"]["name"] . "<br>";
echo "Type: " . $_FILES["file"]["type"] . "<br>";
echo "Size: " . ($_FILES["file"]["size"] / 20000000) . " kB<br>";
echo "Temp file: " . $_FILES["file"]["tmp_name"] . "<br>";
if (file_exists("upload/" . $_FILES["file"]["name"]))
{
echo $_FILES["file"]["name"] . " already exists. ";
}
else
{
move_uploaded_file($_FILES["file"]["tmp_name"],
"upload/" . $_FILES["file"]["name"]);
echo "Stored in: " . "upload/" . $_FILES["file"]["name"];
}
}
}
else
{
echo "Invalid file";
}
?>
i am a newbie at php and studying pdf upload . can somebody have an idea what is wrong with my code . i have been researching . thank you so much
<form action="<?php echo $_SERVER["PHP_SELF"]; ?>" method="post"
enctype="multipart/form-data">
<label for="file">Filename:</label>
<input type="file" name="file" id="file"><br>
<input type="submit" name="submit" value="Submit">
</form>
here is my form please somebody help me i am kind of confuse right now .
The problem is, you are posting on the same file which actually generates the HTML.
So when you first generate your HTML, you don't have any posted values, so the script throws this error.
try this :
<?php
if(isset($_FILES['file']){
upload_file();
}else{
?>
<form action="<?php echo $_SERVER["PHP_SELF"]; ?>" method="post"
enctype="multipart/form-data">
<label for="file">Filename:</label>
<input type="file" name="file" id="file"><br>
<input type="submit" name="submit" value="Submit">
</form>
<?php
}
function upload_file(){
$allowedExts = array("pdf");
$temp = explode(".", $_FILES["file"]["name"]);
$extension = end($temp);
if ((($_FILES["file"]["type"] == "application/pdf") &&
($_FILES["file"]["size"] < 20000000)
&& in_array($extension, $allowedExts)))
{
if ($_FILES["file"]["error"] > 0)
{
echo "Return Code: " . $_FILES["file"]["error"] . "<br>";
}
else
{
echo "Upload: " . $_FILES["file"]["name"] . "<br>";
echo "Type: " . $_FILES["file"]["type"] . "<br>";
echo "Size: " . ($_FILES["file"]["size"] / 20000000) . " kB<br>";
echo "Temp file: " . $_FILES["file"]["tmp_name"] . "<br>";
if (file_exists("upload/" . $_FILES["file"]["name"]))
{
echo $_FILES["file"]["name"] . " already exists. ";
}
else
{
move_uploaded_file($_FILES["file"]["tmp_name"],
"upload/" . $_FILES["file"]["name"]);
echo "Stored in: " . "upload/" . $_FILES["file"]["name"];
}
}
}
else
{
echo "Invalid file";
}
}
?>

Undefined index in file while uploading image

I am trying to create a form where a user can upload images. I'm using php for validation of this file to see whether it is an image file or not but I am getting an error
"Undefined index file.."
I can't understand what's wrong.. Please help
HTML code..
<html>
<body>
<form action="upload.php" method="post" enctype="multipart/form-data">
<input type="file" name="photo" id="file" />
<input type="submit" name="submit" value="Submit" />
</form>
</body>
</html>
PHP code...
<?php
if ((($_FILES["photo"]["type"] == "image/gif")
|| ($_FILES["photo"]["type"] == "image/jpeg")
|| ($_FILES["photo"]["type"] == "image/png"))
&& ($_FILES["photo"]["size"] < 1000000))
{
if ($_FILES["photo"]["error"] > 0)
{
echo "Return Code: " . $_FILES["photo"]["error"] . " ";
}
else
{
echo "Upload: " . $_FILES["photo"]["name"] . "";
echo "Type: " . $_FILES["photo"]["type"] . "";
echo "Size: " . ($_FILES["photo"]["size"] / 1024) . " Kb";
echo "Temp file: " . $_FILES["photo"]["tmp_name"] . "";
if (file_exists("users/" . $_FILES["photo"]["name"]))
{
echo $_FILES["photo"]["name"] . " already exists. ";
}
else
{
move_uploaded_file($_FILES["photo"]["tmp… "users/" . $_FILES["photo"]["name"]);
echo "Stored in: " . "users/" . $_FILES["photo"]["name"];
}
}
else
{
echo "Invalid file";
}
?>
There were two things wrong with your handler.
1) There was a missing closing brace above your last else condition
2) The following line contained characters (dots in tmp...) that didn't belong:
move_uploaded_file($_FILES["photo"]["tmp… "users/" . $_FILES["photo"]["name"]);
Which was changed to:
move_uploaded_file($_FILES["photo"]["tmp_name"], "users/" . $_FILES["photo"]["name"]);
Reformatted code, tested
<?php
if ((($_FILES["photo"]["type"] == "image/gif")
|| ($_FILES["photo"]["type"] == "image/jpeg")
|| ($_FILES["photo"]["type"] == "image/png"))
&& ($_FILES["photo"]["size"] < 1000000))
{
if ($_FILES["photo"]["error"] > 0)
{
echo "Return Code: " . $_FILES["photo"]["error"] . " ";
}
else
{
echo "Upload: " . $_FILES["photo"]["name"] . "";
echo "<br>";
echo "Type: " . $_FILES["photo"]["type"] . "";
echo "<br>";
echo "Size: " . ($_FILES["photo"]["size"] / 1024) . " Kb";
echo "<br>";
echo "Temp file: " . $_FILES["photo"]["tmp_name"] . "";
echo "<br>";
if (file_exists("users/" . $_FILES["photo"]["name"]))
{
echo $_FILES["photo"]["name"] . " already exists. ";
}
else
{
// error line for you to compare the error
// move_uploaded_file($_FILES["photo"]["tmp… "users/" . $_FILES["photo"]["name"]);
move_uploaded_file($_FILES["photo"]["tmp_name"], "users/" . $_FILES["photo"]["name"]);
echo "Stored in: " . "users/" . $_FILES["photo"]["name"];
}
}
} // this was the missing closing brace
else
{
echo "Invalid file";
}
?>
Added bonus: I added a few echo "<br>"; to seperate the fields on successful upload.
Which will appear like this:
Upload: image_test.jpg
Type: image/jpeg
Size: 26.16015625 Kb
Temp file: /tmp/phpifKd7I
Stored in: users/image_test.jpg
instead of on one line.

PHP image uploading script won't run

I'm building a php upload script for images, and it refuses to run at all.
First off, I've got a simple post form like this:
<form action="variables.php" method="post" enctype="multipart/form-data" >
<p>Event title : <input type="text" name="name" required></p>
<p>Description : <input type="text" name="description" required></p>
<input type="file" name="file" id="file"/>
<input type="submit" value="submit">
</form>
This then feeds the submitted fields to "variables.php", which looks like this:
<?php
require("myfunctions.php");
$title = $_POST['name'];
$description =$_POST['description'];
$img = $_FILES["file"];
imgput($img);
generator($title, $description);
?>
"imgput" and "generator" are functions from "myfunctions.php", but the problem isn't in "genrator", so here's what "myfunctions.php" looks like:
<?php
function imgput($img) {
if ((($img["type"] == "image/gif")
|| ($img["type"] == "image/jpeg")
|| ($img["type"] == "image/pjpeg"))
&& ($img["size"] < 500000))
{
if ($img["error"] > 0)
{
echo "Return Code: " . $img["error"] . "<br />";
}
else
{
echo "Upload: " .$img["name"] . "<br />";
echo "Type: " . $img["type"] . "<br />";
echo "Size: " . $img["size"] / 1024) . " Kb<br />";
echo "Temp file: " . $img["tmp_name"] . "<br />";
if (file_exists("../uploads/" . $img["name"]))
{
echo $img["name"] . " already exists. ";
}
else
{
move_uploaded_file($img["tmp_name"],
"../uploads/" . "event1.jpg");
echo "Stored in: " . "../uploads/event1.jpg";
}
}
}
else
{
echo "Invalid file";
}
}
?>
Any help would be great. I tried running test echos right after "imgput" begins, but it won't even run it.
You're missing a ( here:
echo "Size: " . $img["size"] / 1024) . " Kb<br />";
^^^ no matching ( available
Most likely you wanted this:
echo "Size: " . ($img["size"] / 1024) . " Kb<br />";
If you had error_reporting turned on, you'd have seen the syntax error:
PHP Parse error: syntax error, unexpected ')', expecting ',' or ';' in /home/marc/test.php on line 16

Categories