This has had me stumped for a couple of days. I am trying to upload multiple files to the server. I am adding more than one array of files because each group of files is added a different prefix to the filename while it is copied to the server.
The first array uploads perfectly...
<input name="image[]" type="file" id="image[]" size="30">
<br><input name="image[]" type="file" id="image[]" size="30">
<br><input name="image[]" type="file" id="image[]" size="30">
<br><input name="image[]" type="file" id="image[]" size="30">
<br><input name="image[]" type="file" id="image[]" size="30">
But when I try to add another array nothing is uploaded...
<input name="image2[]" type="file" id="image2[]" size="30">
<br><input name="image2[]" type="file" id="image2[]" size="30">
<br><input name="image2[]" type="file" id="image2[]" size="30">
<br><input name="image2[]" type="file" id="image2[]" size="30">
<br><input name="image2[]" type="file" id="image2[]" size="30">
This is the code that processes the request...
//This works for the first array on the page
while(list($key,$value) = each($_FILES[image][name])){ if(!empty($value)) {
$filename = $prefix1.$value;
$add = "../portfolio/gallery/upload/$filename";
copy($_FILES[image][tmp_name][$key], $add);
chmod("$add",0777);
}}
//This does not work for the second array on the page
while(list($key,$value) = each($_FILES[image2][name])){ if(!empty($value)) {
$filename = $prefix2.$value;
$add = "../portfolio/gallery/upload/$filename";
copy($_FILES[image2][tmp_name][$key], $add);
chmod("$add",0777);
}}
Any ideas what I may be doing wrong?
Thanks for all help!
You are accessing the image
key not the image2
key in your second while loop at the copy command. Besides that you should add quotes around the array key names.
Here is a slightly improved and cleaned version of your script. It still has one big flaw, you are trusting user input which enabled attackers to copy files to random locations etc.
The PHP Documentation has a nice entry about php file handling where you can readup more about move_uploaded_file
and file security.
<?php
foreach ($_FILES['image']['name'] AS $key => $filename) {
if(!empty($filename)) {
$add = "../portfolio/gallery/upload/" . $prefix1 . $filename;
move_uploaded_file($_FILES['image']['tmp_name'][$key], $add);
chmod($add, 0777);
}
}
foreach ($_FILES['image2']['name'] AS $key => $filename) {
if (!empty($filename)) {
$add = "../portfolio/gallery/upload/" . $prefix2 . $filename;
move_uploaded_file($_FILES['image2']['tmp_name'][$key], $add);
chmod($add, 0777);
}
}
If I var_dump($_FILES) I get the expected result, thus you might have an issue in the loop there, I tried to modify it in which it made sense as:
<form action="test2.php" method="post"
enctype="multipart/form-data">
<input name="image[]" type="file" id="image[]" size="30">
<br><input name="image[]" type="file" id="image[]" size="30">
<br><input name="image[]" type="file" id="image[]" size="30">
<br><input name="image[]" type="file" id="image[]" size="30">
<br><input name="image[]" type="file" id="image[]" size="30">
<br><br>
<br><input name="image2[]" type="file" id="image[]" size="30">
<br><input name="image2[]" type="file" id="image[]" size="30">
<br><input name="image2[]" type="file" id="image[]" size="30">
<br><input name="image2[]" type="file" id="image[]" size="30">
<input type='submit'>
</form>
<?php
function move_to_root($name,$tmp, $prefix1, $value)
{
$filename = $prefix1.$name;
$add = "upload/$filename";
move_uploaded_file($tmp, $add);
chmod("$add",0777);
}
if(!empty($_FILES))
{
$prefix = 'ab';//You should know
$value = 'cd';//the same
$nrImages = count($_FILES['image']['name']);
$nrImages2 = count($_FILES['image2']['name']);
for($index=0; $index<=$nrImages; $index++)
{
if(!empty($_FILES['image']['name'][$index]))
move_to_root($_FILES['image']['name'][$index],$_FILES['image']['tmp_name'][$index], $prefix, $value);
}
for($index=0; $index<=$nrImages2; $index++)
{
if(!empty($_FILES['image2']['name'][$index]))
move_to_root($_FILES['image2']['name'][$index],$_FILES['image2']['tmp_name'][$index], $prefix, $value);
}
}