Two problems:
-
How can I check whether I receive a Single file input array or a Multiple files input array from a form?
-
How can I change the Single file input array into the Multiple files input array?
Single file input array,
Array
(
[file] => Array
(
[name] => Around the Shoreline of Plymouth01.jpg
[type] => image/jpeg
[tmp_name] => C:\\wamp\ mp\\php7FC5.tmp
[error] => 0
[size] => 258246
)
)
Multiple files input array,
Array
(
[file] => Array
(
[name] => Array
(
[0] => Around the Shoreline of Plymouth01.jpg
)
[type] => Array
(
[0] => image/jpeg
)
[tmp_name] => Array
(
[0] => C:\\wamp\ mp\\phpFF9D.tmp
)
[error] => Array
(
[0] => 0
)
[size] => Array
(
[0] => 258246
)
)
)
This is what I came out with but it is still not correct…
$array = array();
foreach ($_FILES as $key => $fileItSelf)
{
foreach ($fileItSelf as $key => $value)
{
$array[] = array($key => $value);
//$value[]
}
}
print_r($array);
result:
Array
(
[0] => Array
(
[name] => Around the Shoreline of Plymouth01.jpg
)
[1] => Array
(
[type] => image/jpeg
)
[2] => Array
(
[tmp_name] => C:\\wamp\ mp\\php4C50.tmp
)
[3] => Array
(
[error] => 0
)
[4] => Array
(
[size] => 258246
)
)
Thanks,
Lau
[FPHP]is_array[/FPHP] will be able to tell you if there’s more than one file.
As far as transformation: Why do you need to? But if you really wanted to:
$values = array_values($_FILES['file']);
foreach(array_keys($_FILES['file']) AS $index => $key) {
$out['file'][$key][0] = $values[$index];
}
Thanks StarLion,
Your code returns this,
Array
(
[file] => Array
(
[0] => Array
(
[name] => Around the Shoreline of Plymouth01.jpg
[type] => image/jpeg
[tmp_name] => C:\\wamp\ mp\\phpAB90.tmp
[error] => 0
[size] => 258246
)
)
But I actually need it like this,
Array
(
[file] => Array
(
[name] => Array
(
[0] => The Smashing Pumpkins
)
[type] => Array
(
[0] => image/jpeg
)
[tmp_name] => Array
(
[0] => C:\\wamp\ mp\\phpBE71.tmp
)
....
)
)
No, my code gives exactly what you want. Your input must be malformed.
You said the input was
Array
(
[file] => Array
(
[name] => Around the Shoreline of Plymouth01.jpg
[type] => image/jpeg
[tmp_name] => C:wamptmpphp7FC5.tmp
[error] => 0
[size] => 258246
)
)
So, when I translate this out into PHP to simulate the input:
<?php
$_FILES = array('file' => array('name' => "Around the Shoreline of Plymouth01.jpg",'type' => 'image/jpeg','tmp_name' => 'C:wamptmpphpAB90.tmp','error' => 0,'size' => 258246));
$values = array_values($_FILES['file']);
foreach(array_keys($_FILES['file']) AS $index => $key) {
$out['file'][$key][0] = $values[$index];
}
print_r($out);
?>
I get:
Array
(
[file] => Array
(
[name] => Array
(
[0] => Around the Shoreline of Plymouth01.jpg
)
[type] => Array
(
[0] => image/jpeg
)
[tmp_name] => Array
(
[0] => C:wamptmpphpAB90.tmp
)
[error] => Array
(
[0] => 0
)
[size] => Array
(
[0] => 258246
)
)
)
Which is exactly what you asked for.
Thanks StarLion!
Got it working as in your code now.
<?php
$values = array_values($_FILES['file']);
foreach(array_keys($_FILES['file']) AS $index => $key) {
$out['file'][$key][0] = $values[$index];
}
?>
but why it does work like this?
<?php
$values = array_values($_FILES);
foreach(array_keys($_FILES) AS $index => $key) {
$out[$key][0] = $values[$index];
}
?>
Thanks,
Lau
Because $_FILES is not the base array that you’re trying to walk through - $_FILES[‘yourformfieldnamehere’] is.
Clicky linky.
http://hu.php.net/manual/en/features.file-upload.post-method.php
Got it thanks for the help!
is_array will be able to tell you if there's more than one file.
I still cannot work it out to determine if there’s more than one file with is_array - could you please give me some hint?
Thanks!
if $_FILES[‘file’][‘name’] is an array, there are multiple files. If it’s just a string, there’s only 1 file.
Thanks. Got that!
if(!is_array($_FILES['file']['name']))
{
$values = array_values($_FILES['file']);
foreach(array_keys($_FILES['file']) as $index => $key)
{
$out['file'][$key][0] = $values[$index];
}
print_r($out);
}
Thanks so much for the help! 