CakePHP3编辑上传

CakePHP3编辑上传,php,cakephp,file-upload,cakephp-3.0,Php,Cakephp,File Upload,Cakephp 3.0,我遵循这一点在CakePHP3上进行上传。使用该指南,我选择一个文件,当我单击“上载”时,它会将该文件移动到项目中的指定文件夹,将该文件夹设置为路径,并在数据库中创建一个新实体 我现在正试图获得相同的上传,以便能够覆盖现有实体(即编辑而不是添加) 我复制了控制器和视图中的上载功能,并将其修改为用作编辑。然而,在尝试时,编辑失败,我甚至没有得到错误消息;页面只是刷新,就好像什么也没发生一样 相关表为文件,具有以下属性: ID(主键) 链接(保存上载文件的文件夹) 名称(文件的名称) 创造 修改

我遵循这一点在CakePHP3上进行上传。使用该指南,我选择一个文件,当我单击“上载”时,它会将该文件移动到项目中的指定文件夹,将该文件夹设置为路径,并在数据库中创建一个新实体

我现在正试图获得相同的上传,以便能够覆盖现有实体(即编辑而不是添加)

我复制了控制器和视图中的上载功能,并将其修改为用作编辑。然而,在尝试时,编辑失败,我甚至没有得到错误消息;页面只是刷新,就好像什么也没发生一样

相关表为文件,具有以下属性:

  • ID(主键)
  • 链接(保存上载文件的文件夹)
  • 名称(文件的名称)
  • 创造
  • 修改
这是我在控制器中的功能:

public function edit($id = null)
    {
        $uploadData = $this->Files->get($id, [
            'contain' => []
        ]);
        if ($this->request->is('post')) {
            $data = $this->request->data;
            if(!empty($data['link']['name'])){
                $fileName = $data['link']['name'];
                $uploadPath = 'uploads/template/';
                $uploadFile = $uploadPath.$fileName;
                if(move_uploaded_file($data['link']['tmp_name'],$uploadFile)){
                    $uploadData = $this->Files->patchEntity($uploadData, $data);
                    $uploadData->name = $fileName;
                    $uploadData->link = $uploadPath;
                    if ($this->Files->save($uploadData)) {
                        $this->Flash->success(__('File has been uploaded and updated successfully.'));
                    }else{
                        $this->Flash->error(__('Unable to upload file, please try again.'));
                    }
                }else{
                    $this->Flash->error(__('Unable to upload file, please try again.'));
                }
            }else{
                $this->Flash->error(__('Please choose a file to upload.'));
            }
        }

        $files = $this->Files->find('all', ['order' => ['Files.created' => 'DESC']]);
        $filesRowNum = $files->count();
        $blanks = $this->Files->find('all', ['order' => ['Files.created' => 'DESC']])->first();
        $this->set(compact('blanks', 'uploadData', 'files', 'filesRowNum'));

    }
这是我的表格:

<?php echo $this->Form->create($uploadData, ['type' => 'file']); ?>
<?php echo $this->Form->input('link', ['type' => 'file', 'class' => 'form-control', 'label' => 'Template Link' ]); ?><br>
<?php echo $this->Form->button(__('Upload File'), ['type'=>'submit', 'class' => 'btn btn-primary', 'style' => 'width:25%;']); ?>
<?php echo $this->Form->end(); ?>
更改此项:

if ($this->request->is('post')) {
为此:

if ($this->request->is('post') || $this->request->is('put')) {
编辑:正如ndm指出的,数组也可以工作:

 $this->request->is(['post', 'put', 'patch'])

你能在你的
保存
代码的正上方
pr($uploadData)
吗。。您将能够看到
errors
key response(如果有)中是否有任何错误。我尝试了,但没有得到任何结果。我也试过用pr($fileName)、pr($uploadPath)和pr($uploadFile)来检查它们是否正确,但也没有得到任何结果。您所说的
错误
键响应是什么意思?你在哪里找到的?你能进入你的if语句
if($this->request->is('post')){
?你也能进入你的this if语句
if(!empty($data['link']['name']){
?我想这两个都可以。使用
($this->request->is('post')){
我的请求中确实有POST数据,在该POST数据中,link数组包含name元素,该元素不为null。
Request::is()
也接受数组。您可能还希望包含
patch
。这样做是可行的,结果是我没有正确地将其放入数组中。
 $this->request->is(['post', 'put', 'patch'])