开发者

Codeigniter multiple file upload paths

I'm building an app which needs to take uploaded files and put them in separate directories for thumbnails and fullsize imag开发者_如何学Ces. But $config['upload_path'] = './uploads/'; only allows me to select one upload path. How do I define two or more upload paths?


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.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜