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