我正在尝试将文件上传到 webroot/files 目录。我还在我的数据库表中包含了该文件的记录。
将文件保存到数据库有效,我使用 move_uploaded_file() 方法,但它不起作用。它不会返回任何错误,但文件不会显示在文件夹中。我检查了权限,它们都是 755。这是我控制器中的操作:
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.');
}
}
这是来自debug($filePath);
'webroot/files/filename' -> 的输出,其中 filename 是上传文件的实际名称。
任何帮助都会很棒。谢谢
更新------------------------------------------------ 我调试了 $this->request->data,这是我上传小文件时的输出。
array(
'Document' => array(
'MyFile' => array(
'name' => 'add.ctp',
'type' => 'application/octet-stream',
'tmp_name' => '/tmp/phpcxBA9B',
'error' => (int) 0,
'size' => (int) 3700
)
)
)
我还在下面添加了一条 else 语句move_uploaded_file()
,它确实将闪存设置为无法移动文件。
我仍然不确定为什么,它可以是$filename
和$filePath
变量吗?
谢谢