• About Me




    Lily@Melbourne, Australia

    Login

  • Follow 静水深流 || Still waters run deep on WordPress.com
  • Archives

Cakephp多文件上传

首先衷心感谢:
http://bakery.cakephp.org/articles/veganista/2012/01/31/html_5_multiple_file_upload_with_cake

花了一天半的时间企图完成类似的功能,结果不是因为FK的缺失或是鸡同鸭讲的PHP&Javascript所以沟通无效。
<input name='uploads[]' type=file multiple> //uploads这样定义为array, multiple顾名思义接受0~N个文件,但遗憾的是主流浏览器里,IE不支持。。。

在Form helper上 //add.ctp 

<?php echo $this->Form->create('ModelName', array('enctype' => 'multipart/form-data')); echo $this->Form->input('uploads.', array('type' => 'file', 'multiple')); //注意在upload后面有一个.

echo $this->Form->end(‘Upload’); ?>

Debug $this->request->data会显示如下

Array
(
    [ModelName] => Array
        (
            [files] => Array
                (
                    [0] => Array
                        (
                            [name] => 20120112_153642.jpg
                            [type] => image/jpeg
                            [tmp_name] => E:\wamp\tmp\phpD71.tmp
                            [error] => 0
                            [size] => 1922765
                        )
                    [1] => Array
                        (
                            [name] => 20120112_153845.jpg
                            [type] => image/jpeg
                            [tmp_name] => E:\wamp\tmp\phpD81.tmp
                            [error] => 0
                            [size] => 1122957
                        )
        )
)

在你的controller上foreach

foreach($this->request->data['ModelName']as $upload)
{
$this->ModelName->create();
$this->ModelName->save($this->$upload);
}

 
Advertisements

发表评论

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / 更改 )

Twitter picture

You are commenting using your Twitter account. Log Out / 更改 )

Facebook photo

You are commenting using your Facebook account. Log Out / 更改 )

Google+ photo

You are commenting using your Google+ account. Log Out / 更改 )

Connecting to %s

%d 博主赞过: