Codeigniter multiple file upload paths

后端 未结 3 1310
春和景丽
春和景丽 2021-01-06 17:05

I\'m building an app which needs to take uploaded files and put them in separate directories for thumbnails and fullsize images. But $config[\'upload_path\'] = \'./uploads/\

相关标签:
3条回答
  • 2021-01-06 17:44

    You're conflating two issues here. You need to further decompose the problem into discrete tasks.

    First of all, you need to set the appropriate upload directory. On my site, each user is directed to their own upload directory:

    /images/member/1
    /images/member/2
    /images/member/3
    

    My controller sets the upload directory dynamically, based on user id

    $config['upload_path'] = "/images/member/$user_id";
    

    Second, you need to process your uploaded file, creating resized and thumbnail images. My image processing library uses the same path that I passed to $config['upload_path'] as its root directory, and places its output into subdirectories relative to that dynamic root:

    /images/member/1/resized
    /images/member/1/thumbnails
    

    My site is actually a little more complex than that. But the point is that setting $config['upload_path'] dynamically lets you have as many upload paths as you want.

    0 讨论(0)
  • 2021-01-06 17:52

    The short (and disappointing) answer is: CodeIgniter's file uploading class was designed to accept 1 uploaded file per form.

    The long answer is somewhere around here.

    0 讨论(0)
  • 2021-01-06 17:59

    Actually all you need to do is "re-initialize" the upload class. Codeigniter does not allow you to call the class twice with new parameters (it ignores the second request), however you can tell the class to manually load new parameters. Here are the basics: (notice the line "$this->upload->initialized($config)" This is the key.

    // this is for form field 1 which is an image....
    $config['upload_path'] = './uploads/path1';
    $config['allowed_types'] = 'gif|jpg|png';
    $config['max_size'] = '100';
    $config['max_width'] = '1024';
    $config['max_height'] = '768';
    $this->upload->initialize($config); 
    $this->upload->do_upload($fieild_1);
    
    // this is for form field 2 which is a pdf
    $config['upload_path'] = './pdfs/path2';
    $config['allowed_types'] = 'pdf';
    $config['max_size'] = '300';
    $this->upload->initialize($config); 
    $this->upload->do_upload($fieild_2);
    

    I wrote an entire article about it here: http://taggedzi.com/articles/display/multiple-file-uploads-using-codeigniter

    You can change any of the allowed parameters in your second initialize script so that your second file can be of a completely different makeup. You can do this as many times as you like you just have to re-initialized for each file type. (You can even setup a config array and loop through if you like...)

    Hope this helps.

    0 讨论(0)
提交回复
热议问题