[eluser]Leon Stafford[/eluser]
I had a bit of trouble with the last script, so here is one which is working much better now.
There are many examples of either M,V or C in these forums, but I couldn't find one complete multi-uploader example which worked.
Hopefully this will save other CI beginners the hours I spent fiddling with this!
*haven't checked the error functionality of this yet
upload.php Controller file:
Code:
<?php
class Upload extends Controller {
function Upload()
{
parent::Controller();
$this->load->helper(array('form', 'url','html'));
}
function index()
{
$this->load->view('upload_form');
}
function do_upload()
{
$config['upload_path'] = './uploads/';
$config['allowed_types'] = 'gif|jpg|png';
$config['max_size'] = '3000';
$config['max_width'] = '1024';
$config['max_height'] = '768';
$this->load->library('upload', $config);
if ( ! $this->upload->do_upload())
{
$error = array('error' => $this->upload->display_errors());
$this->load->view('upload_form', $error);
}
else
{
$data = array('upload_data' => $this->upload->data());
//$this->load->view('upload_success', $data);
}
}
function multiple_upload($upload_dir = 'uploads', $config = array())
{
$CI =& get_instance();
$files = array();
if(empty($config))
{
$config['upload_path'] = realpath($upload_dir);
$config['allowed_types'] = 'gif|jpg|jpeg|jpe|png';
$config['max_size'] = '2048';
}
$CI->load->library('upload', $config);
$errors = FALSE;
foreach($_FILES as $key => $value)
{
if( ! empty($value['name']))
{
if( ! $CI->upload->do_upload($key))
{
$data['upload_message'] = $CI->upload->display_errors(ERR_OPEN, ERR_CLOSE); // ERR_OPEN and ERR_CLOSE are error delimiters defined in a config file
$CI->load->vars($data);
$errors = TRUE;
}
else
{
// Build a file array from all uploaded files
$files[] = $CI->upload->data();
}
}
}
// There was errors, we have to delete the uploaded files
if($errors)
{
foreach($files as $key => $file)
{
@unlink($file['full_path']);
}
}
elseif(empty($files) AND empty($data['upload_message']))
{
$CI->lang->load('upload');
$data['upload_message'] = ERR_OPEN.$CI->lang->line('upload_no_file_selected').ERR_CLOSE;
$CI->load->vars($data);
}
else
{
$data = array('upload_data' => $files);
$this->load->view('upload_success', $data);
}
}
}
?>
upload_form.php View file:
Code:
<html>
<head>
<title>Upload Form</title>
</head>
<body>
<?php
if(isset($upload_message))
{
echo $upload_message;
}
?>
<?php echo form_open_multipart('upload/multiple_upload');?>
<input type="file" name="userfile1" size="20" />
<br /><br />
<input type="file" name="userfile2" size="20" />
<br /><br />
<input type="file" name="userfile3" size="20" />
<br /><br />
<input type="file" name="userfile4" size="20" />
<br /><br />
<br /><br />
<input type="submit" value="upload" />
</form>
</body>
</html>
upload_success.php View file:
Code:
<html>
<head>
<title>Upload Form</title>
</head>
<body>
<h3>Your file was successfully uploaded!</h3>
<ul>
<?php foreach($upload_data as $item => $value):?>
<?php
/* EXAMPLE ARRAYS OUTPUT
[0] => Array //access file_name with $value['file_name'] inside of <?php foreach($upload_data as $item => $value):?>
(
[file_name] => main9.jpeg
[file_type] => image/jpeg
[file_path] => E:/xampp/htdocs/igniter/uploads/
[full_path] => E:/xampp/htdocs/igniter/uploads/main9.jpeg
[raw_name] => main9
[orig_name] => main.jpeg
[file_ext] => .jpeg
[file_size] => 4.58
[is_image] => 1
[image_width] => 160
[image_height] => 90
[image_type] => jpeg
[image_size_str] => width="160" height="90"
)
[1] => Array
(
[file_name] => Sunset.jpg
[file_type] => image/jpeg
[file_path] => E:/xampp/htdocs/igniter/uploads/
[full_path] => E:/xampp/htdocs/igniter/uploads/Sunset.jpg
[raw_name] => Sunset
[orig_name] => Sunset.jpg
[file_ext] => .jpg
[file_size] => 69.52
[is_image] => 1
[image_width] => 800
[image_height] => 600
[image_type] => jpeg
[image_size_str] => width="800" height="600"
)
*/
?>
<li><?php echo $value['file_name']; //outputs each files details as array?></li>
<?php endforeach; ?>
</ul>
<p><?php echo anchor('upload', 'Upload Another File!'); ?></p>
</body>
</html>