关注Prassanna D's method to upload files on CakePHP 3.x,我做了类似的事情:
public function upload()
{
$uploadData = $this->Files->get($id,[
'contain' => ['Sessions']
]);
if($this->request->is(['patch', 'put', 'post'])){
if (!empty($this->request->data)) {
if (!empty($this->request->data['link']['name'])) {
$file = $this->request->data['link'];
$ext = substr(strtolower(strrchr($file['name'], '.')), 1);
$arr_ext = array('xls', 'xlsx'); //set allowed extensions
$datestart = $uploadData->session->datestart;
$dateend = $uploadData->session->dateend;
$setNewFileName = 'file'.$datestart.'-'.$dateend;
if (in_array($ext, $arr_ext)) {
move_uploaded_file($file['tmp_name'], WWW_ROOT . '\uploads\files' . DS. $setNewFileName . '.' . $ext);
$filesFileName = $setNewFileName . '.' . $ext;
}
}
$getFormvalue = $this->Files->patchEntity($uploadData, $this->request->data);
if (!empty($this->request->data['link']['name'])) {
$getFormvalue->name = $filesFileName;
$getFormvalue->link = 'uploads/files';
}
if ($this->Files->save($getFormvalue)) {
$this->Flash->success('Your file has been successfully uploaded.');
return $this->redirect(['controller' => 'pages', 'action' => 'display', 'home']);
} else {
$this->Flash->error('File could not be uploaded. Please, try again.');
}
}
}
}
我的上传功能查看:
<?php echo $this->Form->create($uploadData, ['type' => 'file']); ?>
<?php echo $this->Form->input('link', ['type' => 'file', 'class' => 'form-control', 'label' => 'Link' ]); ?><br>
<?php echo $this->Form->button(__('Upload File'), ['type'=>'submit', 'class' => 'btn btn-primary', 'style' => 'width:25%;']); ?>
<?php echo $this->Form->end(); ?>
文件表的模型:
public function validationDefault(Validator $validator)
{
$validator
->integer('id')
->allowEmpty('id', 'create');
$validator
->allowEmpty('link')
->add('link',[
'validExtension' => [
'rule' => ['extension',['xls', 'xlsx']],
'message' => __('Only files in Excel formats .xls and .xlsx are allowed')
]
]);
$validator
->allowEmpty('name');
return $validator;
}
当我选择要上传的随机xlsx文件然后单击提交按钮时,在填写相关数据库属性时,文件本身不会上传到uploads目录。
我收到两条与move_uploaded_file
相关的警告:
move_uploaded_file(C:\xampp\htdocs\project\webroot\uploads\files\file 1/4/17-9/4/17.xlsx): failed to open stream: No such file or directory
move_uploaded_file() [<a href='http://php.net/function.move-uploaded-file'>function.move-uploaded-file</a>]: Unable to move 'C:\xampp\tmp\php7143.tmp' to 'C:\xampp\htdocs\project\webroot\uploads\files\file 1/4/17-9/4/17.xlsx'
然而,这确实存在。
如果我将move_uploaded_file($file['tmp_name'], WWW_ROOT . '\uploads\files' . DS. $setNewFileName . '.' . $ext);
更改为move_uploaded_file($file['tmp_name'], WWW_ROOT . '/uploads/files/'. $setNewFileName . '.' . $ext);
- 我收到同样的错误,只有网址显示为move_uploaded_file(C:\xampp\htdocs\project\webroot\/uploads/files/file 1/4/17-9/4/17.xlsx): failed to open stream: No such file or directory
如果我删除$ setNewFileName的所有实例(即我保持原始文件名完整),如下所示:
move_uploaded_file($file['tmp_name'], WWW_ROOT . '/uploads/files/' . $data['link']['name']);
$filesFileName = $data['link']['name'];
$getFormvalue->name = $filesFileName;
我可以让它工作,但我确实想在保存时实际更改文件名,这样当用户下载文件时,它对他们有意义。
更新:忘记我的日期格式是在日期时间。
修复:将我的日期格式化为字符串:
$datestart->format('d-m-y');
$dateend->format('d-m-y');