Welcome Guest, Not a member yet? Register   Sign In
How do I upload Multiple Files ?
#1

[eluser]sirwan.me[/eluser]
Hi, I want to let users upload 1 image and 1 zip file.

I want to let the image be uploaded to "./uploads/posts" and the zip to "./uploads/files" .. but my below code only allows me to upload 2 images, and not 1 image and 1 zip. When I upload the 2 images, it saves both in ./uploads/posts with both the same name.. except the second file will be the extension twice.. for example..
Quote:post_username_2342342342.jpg.jpg


Below is the controller code:
Code:
//getting the username of the logged in user, to later add the username on the file name
$this->load->model('user_model');
        $userdata = $this->user_model->get_online_user_data($this->session->userdata('username'));
        $username = $userdata['username'];
        


        //Upload PICTURE
        $picture['file_name'] = 'post_'.$username.'_'.now();
        $picture['upload_path'] = './uploads/posts';
        $picture['allowed_types'] = 'png|jpg|gif';
        $picture['max_size'] = '1000000';
        
        $this->load->library('upload', $picture);
        if(!$this->upload->do_upload("picture")){
            echo $this->upload->display_errors();
        }else{
            echo "<h1>Picture Uploaded</h1>";
        }
        
        
        //Upload ZIP
        $config['file_name'] = 'file_'.$username.'_'.now();
        $config['upload_path'] = './uploads/files';
        $config['allowed_types'] = 'zip|rar';
        $config['max_size'] = '1000000';
        
        $this->load->library('upload', $config);
        if(!$this->upload->do_upload("file")){
            echo $this->upload->display_errors();
        }else{
            echo "<h1>ZIP Uploaded</h1>";
        }
#2

[eluser]falkencreative[/eluser]
You need to call do_upload() twice, once for each file you are uploading. (and obviously make sure that both uploads are valid).

Keep in mind that do_upload() defaults to use the input named 'userfile', so you'll need to pass in the name of the input:
Code:
$this->upload->do_upload($your_input_name)

http://ellislab.com/codeigniter/user-gui...ading.html has more detailed information.
#3

[eluser]sirwan.me[/eluser]
@falkencreative.

I am calling it twice in my above code.

in the block of code for the picture i call it :

Code:
if(!$this->upload->do_upload("picture")){

and the block of code for the zip i call it:

Code:
if(!$this->upload->do_upload("file")){

isnt it where Im putting "picture" or "file" changing it from the default userfile ?
#4

[eluser]sirwan.me[/eluser]
Ah I done it!

basically I have to reinilize the config array and load the library ONLY ONCE!

load the library once above.

and the after the config arrays, inializite the config, with the inilizite function.
#5

[eluser]alvk4r[/eluser]
See http://ellislab.com/forums/viewthread/156352/




Theme © iAndrew 2016 - Forum software by © MyBB