Search code examples
cakephpfile-uploadcakephp-2.0multiple-file-upload

Multiple File Upload CakePHP not work correctly


since yesterday i try to implement multiple file upload in my project, the database get's updated with the new filenames, but the files were not uploaded (to localhost).

So my view looks like this now:

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

I added in my controller 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'));
        }

In my logs it gives me the following errors:

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]

And the other on:

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

My FileUploadComponent is the following

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

And line 341 in FileUploadComponent is the following:

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

Maybe some of you know this problem already and can give me a hint for this?


Solution

  • So i just disabled the FileUploadComponent and inserted

    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();
    }
    

    Now it works fine :)