0

I have a bit of cod the works fine on MAMP but gives me an error on WAMP. The code is:

$uploads = ""; 
for($x=0; $x<$count_data; $x++)
{
    $uploads .= $_FILES['file']['name'][$x] . ' ';
}

And the error is:

Notice: Undefined offset: 1

Notice: Undefined offset: 2

Notice: Undefined offset: 3

Notice: Undefined offset: 4

Any ideas?

Here is the file up load code:

$number_of_file_fields = 0;
$number_of_uploaded_files = 0;
$number_of_moved_files = 0;
$uploaded_files = array();
$upload_directory = dirname(__file__) . '/../uploads/'; //set upload directory
$count_data=count($_FILES['file']) ;
$uploads = ""; 
for($x=0; $x<$count_data; $x++)
{
    $uploads .= $_FILES['file']['name'][$x] . ' ';
}



for ($i = 0; $i < count($_FILES['file']['name']); $i++) {
    $number_of_file_fields++;
    if ($_FILES['file']['name'][$i] != '') { //check if file field empty or not
        $number_of_uploaded_files++;
        $uploaded_files[] = $_FILES['file']['name'][$i];
        if (move_uploaded_file($_FILES['file']['tmp_name'][$i], $upload_directory . $orderNumber . "_" . $_FILES['file']['name'][$i])) {
            $number_of_moved_files++;
        }
    }
}
Michael
  • 29
  • 1
  • 1
  • 7
  • possible duplicate of [i got a undefined index error when file upload in php](http://stackoverflow.com/questions/5280645/i-got-a-undefined-index-error-when-file-upload-in-php), http://stackoverflow.com/questions/15211950/undefined-offset-notice-in-loop, http://stackoverflow.com/questions/8828583/notice-undefined-offset-0 – PeeHaa Jun 06 '13 at 23:33
  • What are you trying to do with the loop? Are you uploading multiple files? – jcsanyi Jun 06 '13 at 23:34
  • Yes for ($i = 0; $i < count($_FILES['file']['name']); $i++) { $number_of_file_fields++; if ($_FILES['file']['name'][$i] != '') { //check if file field empty or not $number_of_uploaded_files++; $uploaded_files[] = $_FILES['file']['name'][$i]; if (move_uploaded_file($_FILES['file']['tmp_name'][$i], $upload_directory . $orderNumber . "_" . $_FILES['file']['name'][$i])) { $number_of_moved_files++; } } } – Michael Jun 06 '13 at 23:35

1 Answers1

0

There's 2 ways for multiple file uploads to be organized in the $_FILES array, depending on whether they have the same name attribute or not.

To handle both cases, try something like this:

$uploads = '';
foreach ($_FILES as $inputName => $fileInfo) {
    foreach ((array) $fileInfo['name'] as $n) {
        $uploads .= $n . ' ';
    }
}

To help debug the different between the two hosts, try echo'ing out the contents of the $_FILES array on each and see how it's different.

print_r($_FILES);
jcsanyi
  • 8,133
  • 2
  • 29
  • 52