doujichan1399 2015-01-08 08:13
浏览 24
已采纳

多个文件到文件夹中

Hello I have a view file and controller, what makes multiple inputs where I can upload files in to folder, but its uploading only one file in to folder. I know what is problem, but I dont know how to fix this or how to do this.

My Controller:

public function uploadFile() {
        $filename = '';
            if ($this->request->is('post')) { // checks for the post values
                $uploadData = $this->data['files'];
                //print_r($this->data['files']); die;
                if ( $uploadData['size'] == 0 || $uploadData['error'] !== 0) { // checks for the errors and size of the uploaded file
                    echo "Failide maht kokku ei tohi olla üle 5MB";
                    return false;
                }
                $filename = basename($uploadData['name']); // gets the base name of the uploaded file
                $uploadFolder = WWW_ROOT. 'files';  // path where the uploaded file has to be saved
                $filename = $filename; // adding time stamp for the uploaded image for uniqueness
                $uploadPath =  $uploadFolder . DS . $filename;
                if( !file_exists($uploadFolder) ){
                    mkdir($uploadFolder); // creates folder if  not found
                }
                if (!move_uploaded_file($uploadData['tmp_name'], $uploadPath)) {
                    return false;
                } 
                echo "Sa sisestasid faili(d): $filename";

            }           
    }

My View file:

<?php
    echo $this->Form->create('uploadFile', array( 'type' => 'file'));
?>

    <div class="input_fields_wrap">

        <label for="uploadFilefiles"></label>
        <input type="file" name="data[files]" id="uploadFilefiles">

    </div>

<button type="button" class="add_field_button">+</button> <br><br>

    <form name="frm1" method="post" onsubmit="return greeting()">
        <input type="submit" value="Submit">
    </form>

<?php
echo $this->Html->script('addFile');

And this script what Im using in View :

$(document).ready(function() {
    var max_fields      = 3;
    var wrapper         = $(".input_fields_wrap");
    var add_button      = $(".add_field_button");

    var x = 1;
    $(add_button).click(function(e){
        e.preventDefault();
        if(x < max_fields){
            x++;
            $(wrapper).append("<div><input type='file' name='data[files]' id='uploadFilefiles'/><a href='#' class='remove_field'>Kustuta</a></div>");
        }
     });

      $(wrapper).on("click",".remove_field", function(e){ //user click on remove text
            e.preventDefault(); $(this).parent('div').remove(); x--;
        })
});

I think that, the problem is in input names. If Im doing more inputs, then the inputs names are same, and thanks to this its uploading only one file in to webroot/files folder, but I want these all.

Can anybody help me or give me some tips. Thanks !

  • 写回答

2条回答 默认 最新

  • dream198731 2015-01-08 08:22
    关注

    Here is someone with almost exactly the same issue as you have: Create multiple Upload File dynamically

    Try doing the same. I haven't programmed PHP for quite some time, but I guess you should replace data[files] to just data[], so it creates a new array item for each field. Now you are giving each field the same name.

    Then you can loop over them in your controller by using:

    foreach($_FILES['data'] as $file){
      //do stuff with $file
    }
    

    EDIT 2: As you are saying, you want to upload the files (not to a db). So I guess this should work:

    public function uploadFile() {
            $filename = '';
                if ($this->request->is('post')) { // checks for the post values
                    $uploadData = $this->data;
                    foreach($uploadData as $file){
    
                    if ( $file['size'] == 0 || $file['error'] !== 0) { // checks for the errors and size of the uploaded file
                        echo "Failide maht kokku ei tohi olla üle 5MB";
                        return false;
                    }
                    $filename = basename($file['name']); // gets the base name of the uploaded file
                    $uploadFolder = WWW_ROOT. 'files';  // path where the uploaded file has to be saved
                    $filename = $filename; // adding time stamp for the uploaded image for uniqueness
                    $uploadPath =  $uploadFolder . DS . $filename;
                    if( !file_exists($uploadFolder) ){
                        mkdir($uploadFolder); // creates folder if  not found
                    }
                    if (!move_uploaded_file($file['tmp_name'], $file)) {
                        return false;
                    } 
                    echo "Sa sisestasid faili(d): $filename";
    
                }       
             }    
        }
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(1条)

报告相同问题?

悬赏问题

  • ¥15 用windows做服务的同志有吗
  • ¥60 求一个简单的网页(标签-安全|关键词-上传)
  • ¥35 lstm时间序列共享单车预测,loss值优化,参数优化算法
  • ¥15 Python中的request,如何使用ssr节点,通过代理requests网页。本人在泰国,需要用大陆ip才能玩网页游戏,合法合规。
  • ¥100 为什么这个恒流源电路不能恒流?
  • ¥15 有偿求跨组件数据流路径图
  • ¥15 写一个方法checkPerson,入参实体类Person,出参布尔值
  • ¥15 我想咨询一下路面纹理三维点云数据处理的一些问题,上传的坐标文件里是怎么对无序点进行编号的,以及xy坐标在处理的时候是进行整体模型分片处理的吗
  • ¥15 一直显示正在等待HID—ISP
  • ¥15 Python turtle 画图