Trying to upload multiple files at once
Posted: Fri Sep 24, 2010 11:09 am
Hi All. I am trying to implement a way to upload multiple files. I am using a file input in the form with multiple=true with the hopes of eventually being able to open a folder and select a bunch of images, and it upload all of them. Thus far, I have been unsuccessful.
Here is the form code that I am using, it seems to be working, as I can select multiple files, and hit upload.
Here is where the problem is, I believe. I don't think I am properly bringing in the array of files...
Thanks for any help!
Here is the form code that I am using, it seems to be working, as I can select multiple files, and hit upload.
Code: Select all
<input type="hidden" name="MAX_FILE_SIZE" value="<?php echo $CONFIG['uploads']['max_file_size']; ?>"/>
<label>File to upload</label>
<input class="file" multiple="true" name="document[]" type="file"/>
Code: Select all
function add_file($client_id, $desc, $project, $phase, $uploaded_by)
{
global $CONFIG;
/**
* Ensure te file was uploaded,
* fail otherwise
*/
if (!is_uploaded_file($_FILES['document[0]']['tmp_name']))
{
trigger_error('Upload Error: ' . 'Error ' . $_FILES['document['.$i.']']['error'], E_USER_NOTICE);
return false;
}
else
{
/**
* Get all necessary information
* about the file
*/
$i = 0;
while ($_FILES['document['.$i.']']['name'])
{
$file['temp'] = $_FILES['document['.$i.']']['tmp_name'];
$file['name'] = $this->rename_file($_FILES['document['.$i.']']['name']);
$file['size'] = $_FILES['document['.$i.']']['size'];
$file['type'] = preg_replace("/^(.+?);.*$/", "\\1", $_FILES['document['.$i.']']['type']);
$file['type'] = strtolower($file['type']);
$file['ext'] = $this->file_extension($_FILES['document['.$i.']']['name']);
$file['size'] = round($file['size'] / 1024, 2);
$file['name'] = preg_replace("/\s+/", "_", $file['name']);
/**
* Move the file from the temp directory
* to the upload directory
*/
if (!copy($file['temp'], $CONFIG['uploads']['path'] . $file['name']))
{
if (!move_uploaded_file($file['temp'], $CONFIG['uploads']['path'] . $file['name']))
{
trigger_error('Upload Error: ' . 'Error moving from temp dir', E_USER_NOTICE);
exit;
return false;
}
}
/**
* Create a db record for the uploaded document
*/
if ($this->insert_asset_row($client_id, $project, $phase, $uploaded_by['id'], $desc, $file['ext'],
$file['size'], $file['name']))
{
$file_id = mysql_insert_id();
if ($this->get_type($file['ext']) == 'image')
{
$this->create_thumb($CONFIG['uploads']['path'] . $file['name'], $CONFIG['uploads']['path'] . 'thumbs/' . $file['name'], 120);
}
}
$i++;
if (!$_FILES['document['.$i.']'])
{return $file_id;}
}
return false;
}
}Thanks for any help!