Codeigniter multiple file upload paths

僤鯓⒐⒋嵵緔 提交于 2019-11-30 23:45:54

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.

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.

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.

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!