多文件上传CakePHP无法正常工作

时间:2014-10-23 11:25:47

标签: cakephp file-upload cakephp-2.0 multiple-file-upload

从昨天起我尝试在我的项目中实现多个文件上传,数据库得到了新的文件名更新,但文件没有上传(到localhost)。

所以我的观点现在看起来像这样:

<?php echo $this->Form->create('Upload', array('type' => 'file')); ?>
<fieldset>
<br/>
<?php echo $this->Form->input('files.', array('type' => 'file', 'multiple' => 'multiple', 'label' => '')); ?>
</fieldset>
<?php echo $this->Form->end(__('upload'));?>`

我在我的控制器foreach()中添加了:

$files = $this->request->data['Upload']['files'];
        $counter = 0;
        foreach ($files as $row) {
            $counter += 1;
            $uploads = $this->Upload->find('all');
            $exists=false;
            foreach ($uploads as $upl) {
                if (strcmp($upl['Upload']['name'], $row['name'])==0)
                    $exists=true;
            }
            print_r($exists);

            if ($exists) {
                continue;
            } else {
                $this->Upload->create();
                $this->Upload->set('name', $row['name']);
                $this->Upload->set('type', $row['type']);
                $this->Upload->set('size', $row['size']);
                $this->Upload->save();

            }
        }
        if($counter > 1) {
            $this->Session->setFlash(__('Files uploaded'));
        } else if($exists && $counter == 1) {
            $this->Session->setFlash(__('File could not be uploaded. File already exists.'));
        } else {
            $this->Session->setFlash(__('File uploaded'));
        }

在我的日志中,它给了我以下错误:

Warning (512): FileUpload: A file was detected, but was unable to be processed due to a misconfiguration of FileUpload. Current config -- fileModel:'Upload' fileVar:'file' [APP/Plugin/file_upload/Controller/Component/FileUploadComponent.php, line 403]

另一个在:

Warning (2): Invalid argument supplied for foreach() [APP/Plugin/file_upload/Controller/Component/FileUploadComponent.php, line 341]

我的FileUploadComponent是以下

* @link http://www.webtechnick.com * @author Nick Baker * @version 4.0.4 * @license MIT

FileUploadComponent中的第341行如下:

function processAllFiles(){
foreach($this->uploadedFiles as $file){
  $this->_setCurrentFile($file);
  $this->Uploader->file = $this->options['fileModel'] ? $file[$this->options['fileVar']] : $file;
  $this->processFile();
}}

也许有些人已经知道这个问题,可以给我一个提示吗?

1 个答案:

答案 0 :(得分:0)

所以我只是禁用了FileUploadComponent并插入了

if ($exists) {
    continue;
} else {
--> move_uploaded_file($row['tmp_name'], WWW_ROOT . 'files/' . $row['name']);
    $this->Upload->create();
    $this->Upload->set('name', $row['name']);
    $this->Upload->set('type', $row['type']);
    $this->Upload->set('size', $row['size']);
    $this->Upload->save();
}

现在它可以正常工作:)