0

I tried to create an upload script in PHP. I getting this error message:

Notice: Undefined index: file in path\upload\index.php on line 3

This is my form (pretty basic):

<form action="upload" method="post" enctype="multipart/form-data">
   <input type="file" name="file" id="fileUpload">
   <input type="submit" value="Upload Image" name="submitt">
</form>

This is my (not working) upload script:

<?php
    $name       = $_FILES['file']['name'];
    $temp_name  = $_FILES['file']['tmp_name'];

    if(isset($name)){
        if(!empty($name)){
            $location = '../data/';
            if(move_uploaded_file($temp_name, $location.$name)){
                echo 'File uploaded successfully';
            }
        }
    }  else {
        echo 'You should select a file to upload !!';
    }
?>

This is my php.ini config:

; Whether to allow HTTP file uploads.
; http://php.net/file-uploads
file_uploads=On

; Maximum allowed size for uploaded files.
; http://php.net/upload-max-filesize
upload_max_filesize=40M

; Maximum number of files that can be uploaded via a single request
max_file_uploads=20

; Maximum size of POST data that PHP will accept.
; Its value may be 0 to disable the limit. It is ignored if POST data reading
; is disabled through enable_post_data_reading.
; http://php.net/post-max-size
post_max_size=50M

So what did I do wrong. I checked every previously asked question but didn't find an answer.

EDIT 1:

After checking out this and altering my code the problem still consists.

<?php
    if (isset($_POST['submitt'])) {

        if (isset($_FILES['file']) && isset($_FILES['file']['name']) && isset($_FILES['file']['tmp_name'])) {

            $name       = $_FILES['file']['name'];
            $temp_name  = $_FILES['file']['tmp_name'];

            if(!empty($name)){
                $location = '../data/';
                if(move_uploaded_file($temp_name, $location.$name)){
                    echo 'File uploaded successfully';
                }
            }  else {
                echo 'You should select a file to upload !!';
            }

        }
    }
?>

EDIT 2:

I found the problem: Replacing action upload with upload/index.php resolved the problem. I am using XAMPP on my local machine to test my code. This is an error due to an incorrect Apache config by XAMPP itself.

2 Answers2

0

In your 'upload' logic, you need to ensure that the form has been submitted before declaring the values. Otherwise, it's attempting to set variables with a value that does not yet exist;

<?php
    //ensures the form was submitted before declaring variable values
    if (isset($_POST['submitt']) {

        $name       = $_FILES['file']['name'];
        $temp_name  = $_FILES['file']['tmp_name'];

        if(isset($name)){
            if(!empty($name)){
                $location = '../data/';
                if(move_uploaded_file($temp_name, $location.$name)){
                    echo 'File uploaded successfully';
                }
            }
        }  else {
            echo 'You should select a file to upload !!';
        }
    }
?>

Secondly, Change your form code so that the 'action' is it self (in this case, we're leaving action="" as blank):

<form action="" method="post" enctype="multipart/form-data">
   <input type="file" name="file" id="fileUpload">
   <input type="submit" value="Upload Image" name="submitt">
</form>
Adam
  • 1,149
  • 2
  • 14
  • 21
  • I tried this but the code inside `if (isset($_POST['submitt'])` isn't executed. Now is the question why. Clearly I am submitting the data. –  Oct 23 '17 at 19:34
  • See my update regarding the form code and it's "action" parameter. – Adam Oct 23 '17 at 19:43
  • I tried your updated version and seems not to work. I checked my whole directory but I didn't find my upload. Without setting action to `upload` there will be no upload logic or am I wrong? –  Oct 23 '17 at 19:49
  • The "action" parameter tells the form where to submit to, not what action to perform (in the sense you understand it). There is not and "upload" instruction per-say. Apart from my suggestions, are there any error logs you can provide? – Adam Oct 23 '17 at 19:50
  • The only thing I could find was from earlier (before checking if `submitt` is set): `PHP Notice: Undefined index: file in path\upload\index.php on line 5` –  Oct 23 '17 at 19:54
0

When user submit form and didn't pick file to upload your global variable (array) $_FILES won't have item file, hence you have this error, and you have to check if this key available, like this:

if (isset($_FILES['file']) && isset($_FILES['file']['name']) && isset($_FILES['file']['tmp_name'])) {
    // Now you have strict and robust code!
    $name       = $_FILES['file']['name'];
    $temp_name  = $_FILES['file']['tmp_name'];
    // Rest of your code ...
}
cn007b
  • 16,596
  • 7
  • 59
  • 74
  • 1
    Your answer might be more useful if you actually explain waht's going on, and why. – Adam Oct 23 '17 at 19:29
  • Thanks... I tried a cobination of your answer and @Adam A one –  Oct 23 '17 at 19:35
  • @Techassi I've added some explanation!) – cn007b Oct 23 '17 at 19:37
  • 1
    @AdamA I've added some explanation!) – cn007b Oct 23 '17 at 19:38
  • @Vladimir Kovpak Thanks a lot. I am actually not submitting the form without selecting a file. –  Oct 23 '17 at 19:39
  • @Techassi Probably you have some another use case... but the point is - if you submit form without file, you will have this error... and only `isset($_FILES['file'])` can prevent this error... – cn007b Oct 23 '17 at 19:43