php - 上传文件中的数据

标签 php html mysql cakephp

我有一个代码,其中包含上传到文件夹中的文件数量,我希望将这些文件的名称、大小和 URL 存入数据库,但我的 Controller 无法正常工作。 (我正在使用 CakePHP 框架)。我想将这些文件数据添加到数据库中(所有文件数据),但出现错误。

错误:

Notice (8): Undefined index: tmp_namā€ā€‹ā€ā€‹e [APP\Controller\UploadFilesController.php, line 24]

我的 Controller

public function uploadFile() {
        $filename = '';
            if ($this->request->is('post')) {           // checks for the post values
                $uploadData = $this->request->data;
                //print_r($this->request->data); die;
                foreach($uploadData as $file){
                $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 "Sa sisestasid faili: $filename<br>";
            }
             foreach($this->request->data['UploadFile']['file_upload'] as $file){
                if (!empty($this->request->data)  && is_uploaded_file($this->request->data['UploadFile']['file_upload']['tmp_nam‌​‌​e'])) {  //THIS IS LINE 24
                    $fileData = fread(fopen($this->request->data['UploadFile']['file_upload']['tmp_name'], "r"), $this->request->data['UploadFile']['file_upload']['size']);
                    $this->request->data['UploadFile']['name'] = $this->request->data['UploadFile']['file_upload']['name'];
                    $this->request->data['UploadFile']['size'] = $this->request->data['UploadFile']['file_upload']['size'];
                    $this->request->data['UploadFile']['URL'] = $this->request->data['UploadFile']['file_upload']['tmp_name'];
                    $this->request->data['UploadFile']['data'] = $fileData;
                    $this->UploadFile->create();
                    $this->UploadFile->save($this->request->data);
                }
            }
         }
     }    
    }

这是我的 View 文件:

<?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');

如果有必要我也可以添加AddFile脚本。

这是我的表 (upload_files) 结构:

enter image description here

最佳答案

我认为这是最好的方法:

public function uploadFile() {
    $filename = '';
        if ($this->request->is('post')) {           // checks for the post values
            $uploadData = $this->request->data ['UploadFile']['file_upload'];
            //print_r($this->request->data); die;
            foreach($uploadData as $file){
                $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 "Sa sisestasid faili: $filename<br>";

                    $this->request->data['UploadFile']['name'] = $file['name'];
                    $this->request->data['UploadFile']['size'] = $file['size'];
                    $this->request->data['UploadFile']['URL'] = $file['tmp_name'];
                    $this->UploadFile->create();
                    $this->UploadFile->save($this->request->data);
            }           
        }
    }

我在我的主机上试过这个并且它工作正常。

关于php - 上传文件中的数据,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/27856966/

相关文章:

php - 我想获得特定列值mysql的排名

php - Mysqli查询随机选择

javascript - 在不更改链接的情况下在页面之间导航

php - 将选择选项插入数据库

html - 表单中的 CSS 定位帮助文本

Jquery 仅在屏幕上进行更改

mysql - 使用 INNER JOIN 或 MIN 进行更新?

mysql - 带数据库 ID 的 Htaccess 重定向 url

php - Symfony + Doctrine - 定义完整性约束错误时的错误信息

PHP 循环操作