Welcome Guest, Not a member yet? Register   Sign In
uploading multiple files to different folders
#1

[eluser]A.M.F[/eluser]
hello guys,

i am working on file uploading for my control panel.
i have set a multipe file uploading that looks like that-
Code:
$config['upload_path'] = './uploads/';
$config['allowed_types'] = 'gif|jpg|png';
$config['max_size']    = '100';
$config['max_width']  = '1024';
$config['max_height']  = '768';
        
$this->load->library('upload', $config);

$keys = array_keys($_FILES);
$errors = array();
$uploads = array();

foreach($keys as $field){
    
        if ( ! $this->upload->do_upload($field))
        {
            $errors[] = $this->upload->display_errors();
        }    
        else
        {
            $uploads = $this->upload->data();
        }
}
(credit to xwero)

but in here, all the files goes to the same path: uploads.
how can i config a different upload_path for each file before calling the library?
i can't do it with an if statment, cuz i am uploading the files from the same form.

what can i do?
#2

[eluser]xwero[/eluser]
Code:
$this->load->library('upload'); // no config values

$config['allowed_types'] = 'gif|jpg|png';
$config['max_size']    = '100';
$config['max_width']  = '1024';
$config['max_height']  = '768';
        
$keys = array_keys($_FILES);
$errors = array();
$uploads = array();

foreach($keys as $field){
        
        if($field == 'somefield')
        {
           $config['upload_path'] = './uploads2/';
        }
        else
        {
           $config['upload_path'] = './uploads/';
        }        

        $this->upload->initialize($config) // the config values are bound here to the library

        if ( ! $this->upload->do_upload($field))
        {
            $errors[] = $this->upload->display_errors();
        }    
        else
        {
            $uploads[] = $this->upload->data(); // previous code oversight
        }
}
If you have several fields that need to upload to different paths you better use a switch instead of an if else structure.
#3

[eluser]xwero[/eluser]
as an alternative for if else structure you could do
Code:
$config['upload_path'] = ($field == 'somefield')?'./uploads2/':'./uploads/';
#4

[eluser]A.M.F[/eluser]
thanks.

xwero - can u explain a little bit aboutthis structure? i always see it but i don't understnd it
#5

[eluser]xwero[/eluser]
It's called Ternary Operator.

In short it's a compressed if else structure where you have the condition is followed by a question mark with the values behind the question mark separated by a colon.
The biggest benefit is that you don't have to rewrite the variable where you assign the values too.
I always use following structure;
$variable = condition ? true value : false value;
#6

[eluser]A.M.F[/eluser]
i see. thank u!

----

i have another problam. i want to replace the file name when i am uploading it. i know how to change it through the Upload library, but tht involving editing the code. i wonder if there is any other way to do it?
#7

[eluser]xwero[/eluser]
You could do the renaming while looping through the uploads array.




Theme © iAndrew 2016 - Forum software by © MyBB