上传文件到 Files 文件夹

Uploading files in to Files folder

我想上传文件到webroot/files文件夹,但我的控制器什么也没做,有什么错误吗?

查看文件名:uploadfile.ctp
控制器名称:UploadFileController.php
型号名称:UploadFile.php

在我的视图文件中有:

<div class="files">
  <input type="file" name="files[]" /><br/>
</div>
<button type="button" class="plus">+</button> <br><br>
<form name="frm1" method="post" onsubmit="return greeting()">
    <input type="submit" value="Submit">
</form>
<?php echo $this->Html->script('addFile');

添加文件脚本:

$(document).ready(function() {
  $(".plus").click(function() {
    $(".files").append("<input type='file' name='files[]'/><br/>");
  });
});

还有我的控制器,我认为这里某处有错误:

public function uploadFile() {

        $uploadedFile = $this->request->params['UploadFile']['files[]']['tmp_name'];
        $dir = WWW_ROOT . 'files/';
        if ( !is_dir( $dir ) ) {
            mkdir($dir);
            chmod( $dir , 777);
        }
        $fileName = 'file_' . date( 'Y_m_d_h_i_s', time() );
        move_uploaded_file( $uploadedFile, $dir. $fileName);

    }

我收到了一份通知:

Notice (8): Undefined index: UploadFile [APP\Controller\UploadFileController.php, line 7]

感谢您提供任何线索!

解决方案

查看文件 (uploadfile.ctp) :

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

        <div class="input_fields_wrap">

            <label for="uploadFilefiles"></label>
            <input type="file" name="data[]" 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');

控制器(UploadFileController.php):

 class UploadFileController extends AppController {
    public function uploadFile() {
        $filename = '';
            if ($this->request->is('post')) { // checks for the post values
                $uploadData = $this->data;
                //print_r($this->data); die;
                foreach($uploadData as $file){
                if ( $file['size'] == 0 ||  $file['error'] !== 0) { // checks for the errors and size of the uploaded file
                    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'], $uploadPath)) {
                    return false;
                } 
                echo "Filename: $filename<br>";
            }       
         }    
    }
  }

脚本 (addFile.js) :

$(document).ready(function() {
    var max_fields      = 2;
    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[]' id='uploadFilefiles'/><button href='#' class='remove_field'>Kustuta</button></div>");
        }
     });

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