Welcome Guest, Not a member yet? Register   Sign In
file upload in multiple paths
#1

[eluser]PHP Programmer[/eluser]
I am trying to upload files in two different paths by residing in a single form.

I have two fields - Photo Uplaod (Requires .jpg|.gif|.png ) and Resume Upload (Requires .doc|.rtf)

I am trying following:

View
<?php echo form_open_multipart('upload/do_upload');?>

<input type="file" name="userfile" size="20" />
<input type="file" name="testfile" size="20" />

<br /><br />

&lt;input type="submit" value="upload" /&gt;

&lt;/form&gt;

Controller
function do_upload()
{
$config['upload_path'] = './uploads/file1/';
$config['allowed_types'] = 'gif|jpg|png';
$config['max_size'] = '100';
$config['max_width'] = '1024';
$config['max_height'] = '768';

$config1['upload_path'] = './uploads/file2/';
$config1['allowed_types'] = 'gif|jpg|png';
$config1['max_size'] = '100';
$config1['max_width'] = '1024';
$config1['max_height'] = '768';

$this->load->library('upload', $config);
$this->load->library('upload', $config1);

$this->upload->initialize($config);
$this->upload->initialize($config1);
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);
}
}

But it is not uploading the files in both folders (file1 and file2). How can I solve this problem?
#2

[eluser]xwero[/eluser]
You can only load a library once and adding two initialize methods you just overwrite one setting array with the other. You have to use a loop to make this happen. The file fields can't be named userfile so you have to use separate names.
Code:
// view
&lt;input type="file" name="image"&gt;
&lt;input type="file" name="resume"&gt;
// controller
$files = array('image'=>array(’upload_path’=>‘./uploads/file1/’,’allowed_types’=>‘gif|jpg|png’,’max_size’=>100),
               'resume'=>array(’upload_path’=>‘./uploads/file2/’,’allowed_types’=>‘doc|rtf’,’max_size’=>100)
              );

$this->load->library(’upload’);

foreach($files as $field=>$settings)
{
   $this->upload->initialize($settings);
   if ( ! $this->upload->do_upload($field))
   {
     //error
   }
   else
   {
     // success
   }  
}
#3

[eluser]PHP Programmer[/eluser]
thanx....it worked!! Smile
#4

[eluser]PHP Programmer[/eluser]
Also, 1 more addition in my prev question:

How to upload video along with the above two files??
#5

[eluser]xwero[/eluser]
video is also a file so you could add it but because video is most of the time a pretty big file so it's going to take long to upload it. I would suggest they upload it to a video service and they add the link to the video Wink




Theme © iAndrew 2016 - Forum software by © MyBB