PHP Notice: Undefined index: fileToUpload

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]

See what is coming through.

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

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

I still the the error messages. Nothing else appears

Okay then…

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

exit();

This is what appears

Array
(
)

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?

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
)

)

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?

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

This is line 98

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

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]

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

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.

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]

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

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

I using go daddy web hosting

Sponsor our Newsletter | Privacy Policy | Terms of Service