PHP Notice: Undefined index: fileToUpload


#1

I am getting user’s to submit a photo and that photo stores in the uploads folder in the directory. However I keep getting this error messages and am not sure how to fix them.
These are the errors i am getting:
PHP Notice: Undefined index: fileToUpload in G:\PleskVhosts\website\httpdocs\upload.php on line 55

PHP Notice: Undefined index: fileToUpload in G:\PleskVhosts\website\httpdocs\upload.php on line 80

Upload.php script

[php]<?php

$target_dir =  "/httpdocs/uploads/";
$target_file = $target_dir . basename($_FILES["fileToUpload"]["name"]);
$uploadOk = 1;
$imageFileType = strtolower(pathinfo($target_file,PATHINFO_EXTENSION));
// Check if image file is a actual image or fake image

if(isset($_POST["submit"])) {
    $check = getimagesize($_FILES["fileToUpload"]["tmp_name"]);
    if($check !== false) {
        echo "File is an image - " . $check["mime"] . ".";
        $uploadOk = 1;
    } else {
        echo "File is not an image. ";
        $uploadOk = 0;
    }
} 



// if file name if the same, adds time stamp
 if (file_exists($target_file)) {

$time = time();
$target_file .= $time;
}

// Check file size
if ($_FILES["fileToUpload"]["size"] > 500000) {
    echo "Sorry, your file is too large. ";
    $uploadOk = 0;
}

// Allow certain file formats
if($imageFileType != "jpg" && $imageFileType != "png" && $imageFileType != "jpeg"
&& $imageFileType != "gif" ) {
    echo " Sorry, only JPG, JPEG, PNG & GIF files are allowed. ";
    $uploadOk = 0;
} 

// Check if $uploadOk is set to 0 by an error
if ($uploadOk == 0) {
    echo " Sorry, your file was not uploaded. ";
// if everything is ok, try to upload file
} else {
   if (move_uploaded_file($_FILES["fileToUpload"]["tmp_name"], $target_file)) {
        echo "The file ". basename( $_FILES["fileToUpload"]["name"]). " has been uploaded. ";
    } else {
        echo "Sorry, there was an error uploading your file. ";
    }

} 

?> [/php]

my html form

[php]
Select image to upload:




[/php]


#2

See what is coming through.

Place this in the file around the error,
[php]echo ‘

’;
print_r($_FILES);
echo ‘
’;[/php]

#3

I still the the error messages. Nothing else appears


#4

Okay then…

At the top of the upload file add it, followed by,

exit();


#5

This is what appears

Array
(
)


#6

Then nothing is being sent, which is why you are getting an undefined index.

You are trying to upload a file when you test this, right?


#7

When i submit the form with a picture i get this outputted

Array
(
[fileToUpload] => Array
(
[name] => IMG Elephant close up.png
[type] => image/png
[tmp_name] => C:\Windows\Temp\phpED0D.tmp
[error] => 0
[size] => 917615
)

)


#8

When you get the errors:

PHP Notice: Undefined index: fileToUpload in G:\PleskVhosts\website\httpdocs\upload.php on line 55

PHP Notice: Undefined index: fileToUpload in G:\PleskVhosts\website\httpdocs\upload.php on line 80

Was that with or without the file being uploaded?


#9

I tried both with and without uploading a file. I have just tried again and it seems to work but i am now getting these errors

PHP Warning: move_uploaded_file(/httpdocs/uploads/main-menu-01.jpg): failed to open stream: No such file or directory in G:\PleskVhosts\website\httpdocs\upload.php on line 98

PHP Warning: move_uploaded_file(): Unable to move ‘C:\Windows\Temp\php1594.tmp’ to ‘/httpdocs/uploads/main-menu-01.jpg’ in G:\PleskVhosts\website.co.uk\httpdocs\upload.php on line 98


#10

This is line 98

if (move_uploaded_file($_FILES[“fileToUpload”][“tmp_name”], $target_file)) {


#11

Hi,

Have just copied your code over to my editor, looks like all code is run regardless of whether anything is uploaded / submit button clicked.That might explain why ,undefined indexes,.
Cut & copied parts of code so that most of it is enclosed by ,if(isset($_POST[“submit”])), statement curly brackets.
Getting no notices & it does upload on my server - works fine for me.

Your code (html & php in one file), note my upload folder path differs from yours!!:
[php]

Select image to upload:


<?php $target_dir = "/var/www/html/uploads/"; #$target_file = $target_dir . basename($_FILES['fileToUpload']['name']); $uploadOk = 1; #$imageFileType = strtolower(pathinfo($target_file,PATHINFO_EXTENSION)); // Check if image file is a actual image or fake image if(isset($_POST["submit"])) { $target_file = $target_dir . basename($_FILES['fileToUpload']['name']); $imageFileType = strtolower(pathinfo($target_file,PATHINFO_EXTENSION)); $check = getimagesize($_FILES["fileToUpload"]["tmp_name"]); if($check !== false) { echo "File is an image - " . $check["mime"] . "."; $uploadOk = 1; } else { echo "File is not an image. "; $uploadOk = 0; } // if file name if the same, adds time stamp if (file_exists($target_file)) { $time = time(); $target_file .= $time; } // Check file size if ($_FILES['fileToUpload']['size'] > 500000) { echo "Sorry, your file is too large. "; $uploadOk = 0; } // Allow certain file formats if($imageFileType != "jpg" && $imageFileType != "png" && $imageFileType != "jpeg" && $imageFileType != "gif" ) { echo " Sorry, only JPG, JPEG, PNG & GIF files are allowed. "; $uploadOk = 0; } // Check if $uploadOk is set to 0 by an error if ($uploadOk == 0) { echo " Sorry, your file was not uploaded. "; // if everything is ok, try to upload file } else { if (move_uploaded_file($_FILES["fileToUpload"]["tmp_name"], $target_file)) { echo "The file ". basename( $_FILES["fileToUpload"]["name"]). " has been uploaded. "; } else { echo "Sorry, there was an error uploading your file. "; } } } ?>

[/php]


#12

Hi thanks for your reply, I have managed to get it working now, im just having a problem with the file not uploading to the directory. Do you know how i can find the correct directory. Im using go daddy as my hosting server


#13

Try amended code if you get the same error.

No idea about GoDaddy, i run my scripts on my own debian server in virtual box.

PHP Warning: move_uploaded_file(/httpdocs/uploads/main-menu-01.jpg): failed to open stream: No such file or directory in G:\PleskVhosts\website\httpdocs\upload.php on line 98
In theory, because file was not uploaded it then can not be moved hence the error message.
But i may be well wrong on this.


#14

The target directory you are specifying, with the leading /, refers to the root of the current disk. Since the upload.php file is in the document root folder, change the target directory to be -

[php]$target_dir = “uploads/”;[/php]


#15

I tried what you said and this is the error message i get:

PHP Warning: move_uploaded_file(uploads/main-menu-01.jpg): failed to open stream: Permission denied in G:\PleskVhosts\snappyphoto.co.uk\httpdocs\upload.php on line 98

PHP Warning: move_uploaded_file(): Unable to move ‘C:\Windows\Temp\php1D96.tmp’ to ‘uploads/main-menu-01.jpg’ in G:\PleskVhosts\snappyphoto.co.uk\httpdocs\upload.php on line 98

It says permission denied does that mean i need to give permission in my hosting settings with go daddy


#16

Are you on shared hosting, a VPS, or a dedicated server?


#17

I using go daddy web hosting