I have a form which allows up to 6 file uploads…
<table style="width:90%; margin:0 auto" class="form">
<tr>
<td><label for='file1'>Image 1:</label></td><td><input type='file' name='file[]'></a></td>
<td><label for='file2'>Image 2:</label></td><td><input type='file' name='file[]'></a></td>
</tr><tr>
<td><label for='file3'>Image 3:</label></td><td><input type='file' name='file[]'></a></td>
<td><label for='file4'>Image 4:</label></td><td><input type='file' name='file[]'></a></td>
</tr><tr>
<td><label for='file5'>Image 5:</label></td><td><input type='file' name='file[]'></a></td>
<td><label for='file6'>Image 6:</label></td><td><input type='file' name='file[]'></a></td>
</table>
How do I check even if a file has baan selected, heres the code which runs on the form if a file has been selected to uipload
foreach ($_FILES["file"]["name"] as $index => $file_name) {
...
...
}
How do I run that code only if a file has been selected?
I’m thinking I have to run something like this…
if ($_FILES['file']['error'] != UPLOAD_ERR_NO_FILE) {
...
...
}
Dug this code up from an old thread:
function get_files($_FILES) {
$files=$_FILES['image'];
$file_count=count($files);
$count=0;
while ( $count <> $file_count-1 ) {
$sorted_files[$count]['name']=$files['name'][$count];
$sorted_files[$count]['type']=$files['type'][$count];
$sorted_files[$count]['tmp_name']=$files['tmp_name'][$count];
$sorted_files[$count]['error']=$files['error'][$count];
$sorted_files[$count]['size']=$files['size'][$count];
if (!is_uploaded_file($sorted_files[$count]['tmp_name'])) {
unset($sorted_files[$count]);
}
$count++;
}
return $sorted_files;
}
That should take the $_FILES multidimensional array and rearrange it so that each sub-array of the $sorted_files array will relate to a single file and it’ll deal with culling out any cases where no file has been uploaded.
Just change the “image” in
$files=$_FILES['image'];
to suit the form
So, when I run that function, I will get an array back of the uploaded files?
I’m guessing that would go something like
get_files();
foreach ($sorted_files["name"] as $index => $file_name) {
...
...
}
thanks
foreach ($sorted_files as $file) {
// do something with the file here
}
Each iteration of the loop you’ll have a single file in $file
the uploads dont seem to work, but I dont get an error…
Heres my php code…
function get_files($_FILES) {
$files = $_FILES['file'];
$file_count=count($files);
$count=0;
while ( $count <> $file_count-1 ) {
$sorted_files[$count]['name']=$files['name'][$count];
$sorted_files[$count]['type']=$files['type'][$count];
$sorted_files[$count]['tmp_name']=$files['tmp_name'][$count];
$sorted_files[$count]['error']=$files['error'][$count];
$sorted_files[$count]['size']=$files['size'][$count];
if (!is_uploaded_file($sorted_files[$count]['tmp_name'])) {
unset($sorted_files[$count]);
}
$count++;
}
return $sorted_files;
}
get_files();
foreach ($sorted_files as $file) {
if ($file == '') {
continue;
}
$Type = $file["type"];
$Name = $file["name"];
$tmp_name = $file["tmp_name"];
$Error = $file["error"];
$Size = $file["size"];
if($Error > 0){
echo 'An error ocurred when uploading.';
}
if(($Type != 'image/png') && ($Type != 'image/gif') && ($Type != 'image/jpeg') && ($Type != 'image/bmp'))
{
echo 'Unsupported filetype uploaded.';
}
if($Size > 102400){
echo 'File uploaded exceeds maximum upload size.';
}
$now = time();
while(file_exists($uploadFilename = $now.'-'.$Name))
{
$now++;
}
if(!move_uploaded_file($tmp_name, 'uploads/'.$uploadFilename)){
echo 'Error uploading file - check destination is writeable.';
}
}
Thanks, im really trying to understand this, but I sort of get lost on the function (think I need a parameter, but dont know what to use on the methods call)
Add the following to the start of the script, the next line, after the opening <?php does PHP then report any errors?
error_reporting(E_ALL | E_STRICT | E_ERROR | E_WARNING | E_PARSE | E_NOTICE | E_CORE_ERROR | E_CORE_WARNING | E_COMPILE_ERROR | E_COMPILE_WARNING | E_USER_ERROR | E_USER_WARNING | E_USER_NOTICE | E_RECOVERABLE_ERROR);
ini_set('display_errors', 1);
The top two lines are supposed to be one line, it’s gotten word-wrapped by the forum’s text editor