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++;
        }
    }
}
3
  • What are you trying to do with the loop? Are you uploading multiple files? Commented Jun 6, 2013 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++; } } } Commented Jun 6, 2013 at 23:35

1 Answer 1

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);
Sign up to request clarification or add additional context in comments.

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.