-2

I have a php script that uploads images to a folder.

It is working locally, but when I put it on the hosting server I get the following error message:

[Thu May 18 21:45:00.833970 2017] [fcgid:warn] [pid 37560] [client 151.229.83.93:44238] mod_fcgid: stderr: PHP Notice: Undefined index: file in /home/linnas01/g/domainname.com/user/htdocs/php/uploadphoto.php on line 9, referer: http://domainname.com/photoupload.html

Can anyone explain why this is happening and how to fix this?

The php looks like this

<?php

if ( 0 < $_FILES['file']['error'] ) {
     echo 'Error: ' . $_FILES['file']['error'] . '<br>';
 }
 else {
     move_uploaded_file($_FILES['file']['tmp_name'], 'images/' . $_FILES['file']['name']);
 }

?>

My html file looks like this

<form enctype="multipart/form-data">
    <input id="upload-input" type="file" name="file" multiple="multiple" enctype="multipart/form-data">
    <input type="submit">
</form>

The file gets posted with ajax.

 var formData = new FormData();

 for (var i = 0; i < files.length; i++) {
     var file = files[i];
     fileNames.push(file.name);

    // add the files to formData object for the data payload
    formData.append("image", file);
  }

$.ajax({
    url: 'uploadphoto.php',
    type: 'POST',
    data: formData,
    processData: false,
    contentType: false,
    success: function(data){
       console.log(data);
    }
});

I have also configured the directories and files as explained here.

Community
  • 1
  • 1
peter flanagan
  • 9,195
  • 26
  • 73
  • 127
  • 4
    Possible duplicate of [PHP: "Notice: Undefined variable", "Notice: Undefined index", and "Notice: Undefined offset"](http://stackoverflow.com/questions/4261133/php-notice-undefined-variable-notice-undefined-index-and-notice-undef) – Bytewave May 18 '17 at 20:51
  • tl;dr You don't have anything `POST`ing a file field with the `name` attribute `file`. If we knew what your form looked like, we could help more. – Bytewave May 18 '17 at 20:53
  • Check that `$_FILES['file']` is populated before trying to use it. – chris85 May 18 '17 at 20:53
  • @Bytewave added the html – peter flanagan May 18 '17 at 20:55
  • 1
    Maybe you want `enctype="multipart/form-data"` on the form tag? Also, there is no submit button. Are you not showing all form/code? – AbraCadaver May 18 '17 at 21:02
  • Then your question is not accurate and we can't help if you just post nonsensical code that you aren't using. – AbraCadaver May 18 '17 at 21:03
  • That's not the **form** tag now is it? That is an **input** tag. – AbraCadaver May 18 '17 at 21:05
  • If you're submitting it using AJAX, the form attributes are irrelevant. Show the code that creates the `formData` variable. – Barmar May 18 '17 at 21:07

1 Answers1

2

The problem is that the name you're using in $_FILES doesn't match the name you're giving in formData.append().

Also, since this is a multiple-file input, you need to use an array-style name to get all the files. So the code should be:

 var formData = new FormData();

 for (var i = 0; i < files.length; i++) {
     var file = files[i];
     fileNames.push(file.name);

    // add the files to formData object for the data payload
    formData.append("file[]", file);
  }

Then in the PHP code, you need to loop through the array to process all the uploads.

foreach ($_FILES['file']['tmp_name'] as $i => $tmp_name) {
    if ($_FILES['file']['error'][$i] != 0) {
        echo 'Error: ' . $_FILES['file']['error'][$i] . '<br>';
    } else {
        move_uploaded_file($tmp_name, 'images/' . $_FILES['file']['name'][$i]);
    }
}
Barmar
  • 741,623
  • 53
  • 500
  • 612