0
votes

I am trying to upload a file to the webroot/files directory. I also include a record of the file in my database table.

The saving of the file to the database works, and I use the move_uploaded_file() method, but it does not work. It doesn't return any errors, but the files doesn't show up in the files folder. I checked permissions and they are all 755. Here is the action in my controller:

if ($this->request->is('post'))
  {
      $uploadedFile = array();
      $filename = $this->request->data['Document']['MyFile']['name'];
      $fileData = fread(fopen($this->request->data['Document']['MyFile']['tmp_name'], "r"), $this->request->data['Document']['MyFile']['size']);

      $uploadedFile['MyFile']['name'] = $this->request->data['Document']['MyFile']['name'];
      $uploadedFile['MyFile']['type'] = $this->request->data['Document']['MyFile']['type'];
      $uploadedFile['MyFile']['size'] = $this->request->data['Document']['MyFile']['size'];
      $uploadedFile['MyFile']['data'] = $fileData;

  $filePath = WEBROOT_DIR . DS . 'files' . DS . $uploadedFile['MyFile']['name'];
 debug($filePath);
      if (move_uploaded_file($filename, $filePath))
      {
          echo "No Error";
          $this->Session->setFlash('Uploaded file has been moved SUCCESS.');
      }
      else
      {
          $this->Session->setFlash('Unable to Move file.');
      }
      if ($this->MyFile->save($uploadedFile))
      {
          $this->Session->setFlash('Uploaded file has been saved.');
      }
      else
      {
          $this->Session->setFlash('Unable to save file.');
      }
  }

This is the output from the debug($filePath); 'webroot/files/filename' -> where filename is the actual name of the file uploaded.

Any help would be great. Thanks

Update------------------------------------------------ I debugged $this->request->data and this is the output when i upload a small file.

array(
    'Document' => array(
        'MyFile' => array(
            'name' => 'add.ctp',
            'type' => 'application/octet-stream',
            'tmp_name' => '/tmp/phpcxBA9B',
            'error' => (int) 0,
            'size' => (int) 3700
        )
    )
)

I also added an else statement below the move_uploaded_file() and it does set the flash to Unable to move file.

I'm still not sure why, can it be the $filename and $filePath variables?

Thanks

3

3 Answers

1
votes

You need to move 'tmp_name', i.e.

$filename = $this->request->data['Document']['MyFile']['tmp_name'];
1
votes

Have you tried using the constant WWW_ROOT instead of WEBROOT_DIR? You need to provide the path to the file system.

1
votes

You May face problem with this:

$filePath = WEBROOT_DIR . DS . 'files' . DS . $uploadedFile['MyFile']['name'];

The Error message will be like:

move_uploaded_file(webroot\files\File_Name): failed to open stream: No such file or directory [APP\Controller\CustomerController.php cakephp

If so, Replace the above line with this:

$filePath = WWW_ROOT . DS . 'files' . DS . $uploadedFile['MyFile']['name'];

Means,

WEBROOT_DIR . DS . 

with

WWW_ROOT .

and it will move the Uploaded file.