Why i get these errors when i open the page but when i try to upload it works fine ?

<?php
    function upload_images() {
        $target_dir = "uploads/";
        $target_file = $target_dir . basename($_FILES["fileToUpload"]["name"]);
        $uploadOk = 1;
        $imageFileType = 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;
            }
        }
        // Check if file already exists
        if (file_exists($target_file)) {
            echo "Sorry, file already exists.";
            $uploadOk = 0;
        }
        // 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" && $imageFileType != "mp4" && $imageFileType != "wmv" 
            && $imageFileType != "mp3") {
            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.";
            }
        }
    }
?>

Notice: Undefined index: fileToUpload in C:xampphtdocsUpstreyincludesfunctions.php on line 5 Sorry, file already exists. Notice: Undefined index: fileToUpload in C:xampphtdocsUpstreyincludesfunctions.php on line 25 Sorry, only JPG, JPEG, PNG & GIF files are allowed.Sorry, your file was not uploaded.

The specific error raises because the index key does not exists in the array, example:

$data['a'] = 'hello';

print $data['a']; # will print 'hello'
print $data['b']; # will raise the error

By using array_key_exists('index', $_FILES) you can avoid such problems. But you have to consider that, usually, the $_FILES array is populated after you send an upload request. It seems you're running upload_images() without testing the request method or if the $_FILES array exists.

As simple solution, try:

if($_FILES)
    upload_images();

And in general you should modify the function to work with arbitrary input and paths.

Member Avatar for diafol

Seems like you are running the function on page load. You shouldn't do that:

Something like:

if($_POST)
{
    upload_images();
    //other stuff for processing POST elements
}

should do it. It is generally held that you do not post form data to the same page, rather you post it to its own form handler file. There are variations on a theme, e.g. Controllers in MVC, but regardless, posting to its own page has issues, not least the effect of a page refresh following submission.

Oh man I can believe :-) but thank you very much :-)

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.