This question already has an answer here:
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++;
}
}
}
</div>
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);