如何在cakephp上传多个图像?

时间:2014-02-03 08:05:27

标签: php jquery file-upload cakephp-1.3 cakephp-2.3

我在加载多张图片时遇到问题,我只能加载单张图片,当我选择下一张图片时,上一张图片会丢失。

我的观看页面是这样的:

<div id="for_image0" class="inline-form" style="display:none;">
    <span>
        <input type='file' name="data[ReportManagement][imageUploader]" id="file_upload" onchange="readURL(this);" />
        <span id="extension_error" style="color:red;"></span>
        <br/>
    </span>         
    <span id="previewPane">
        <img style="width:150px;float:left;" id="img_prev" src="#" />
        <span id="x" class="delete-icon" style="display:none;"><?php echo $this->Html->image('close.png',array('width'=>'23px')); ?></span>
    </span>
</div>

主视图页面:

<div id="for_image0" class="inline-form" style="display:none;">
    <span>
        <input type='file' name="data[ReportManagement][imageUploader]" id="file_upload" onchange="readURL(this);" />
        <span id="extension_error" style="color:red;"></span>
        <br/>
    </span>
    <span id="previewPane">
        <img style="width:150px;float:left;" id="img_prev" src="#" />
        <span id="x" class="delete-icon" style="display:none;"><?php echo $this->Html->image('close.png',array('width'=>'23px')); ?></span>
    </span>
</div>

控制器页面:

if(!empty($this->request->data['ReportManagement']['imageUploader'.$i]['name'])){
    //debug($this->request->data['ReportManagement']['imageUploader'.$i]);
    $image_type = @getimagesize($this->request->data['ReportManagement']['imageUploader'.$i]['tmp_name']);
    $file_type = array('image/jpeg','image/gif','image/png','image/bmp');
    if(!in_array($image_type['mime'],$file_type)){
        return 1;
        exit;
    }           
    $name = $this->request->data['ReportManagement']['imageUploader'.$i]['name'];
    $tmp_name = $this->request->data['ReportManagement']['imageUploader'.$i]['tmp_name'];
    $random_number = $this->Smart->random_code();

    if(file_exists(WWW_ROOT.'issuereports/'.$name)){
        $name= $random_number.$this->request->data['ReportManagement']['imageUploader'.$i]['name']; 
    }
} else {
    $name = '';
}   

1 个答案:

答案 0 :(得分:0)

这是一种方法,你可以尝试这个..

在视图中:

<input type='file' multiple=true name="data[ReportManagement][imageUploader]" id="file_upload" onchange="readURL(this);" />

此方法允许您选择多个文件。