【问题标题】:CakePHP 3 Edit UploadCakePHP 3 编辑上传
【发布时间】:2017-03-21 11:30:03
【问题描述】:

我正在关注guide 在 CakePHP 3 上进行上传。使用该指南,我选择了一个文件,当我单击上传时,它将该文件移动到项目内的指定文件夹,将该文件夹设置为路径,并在数据库中创建一个 newEntity。

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

我在控制器和视图中复制了上传功能,并将它们修改为用作编辑。但是,在尝试时,编辑失败,我什至没有收到错误消息;页面就像什么都没发生一样刷新。

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

  • ID(主键)
  • 链接(保存上传文件的文件夹)
  • name(文件名)
  • 已创建
  • 修改

这是我在控制器中的功能:

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(); ?>

在控制器中,我还尝试将 ($this-&gt;request-&gt;is('post')) { 替换为 ($this-&gt;request-&gt;is('patch', 'post', 'put')) {,类似于蛋糕烘焙的编辑页面。

如果说我要上传一个名为 Book 2.xlsx 的空白 Excel 电子表格并想覆盖文件表中的第 4 个数据条目,我会转到 URL localhost/project/files/edit/4,选择文件并提交。

这样做后,不会出现任何错误消息,无论是 Cake 错误还是 flash 错误,并且在 CakePHP 请求中,我在 POST 中有以下数组:

link (array)
    name: Book2.xlsx
    type: application/vnd.openxmlformats-officedocument.spreadsheetml.sheet
    tmp_name: C:\xampp\tmp\php3B10.tmp
    error: 0
    size: 7823

【问题讨论】:

  • 你能在save代码上方pr($uploadData)吗..你将能够看到errors键响应中是否有任何错误。
  • 我试过了,但一无所获。我也尝试过做 pr($fileName)、pr($uploadPath) 和 pr($uploadFile) 来检查这些是否正确,但也一无所获。 errors 关键响应是什么意思?你在哪里找到的?
  • 你能进入你的if语句if ($this-&gt;request-&gt;is('post')) {吗?
  • 你也可以进入你的这个if语句if(!empty($data['link']['name'])){吗?
  • 我认为两者都是。使用($this-&gt;request-&gt;is('post')) {,我确实在Request 中有POST 数据,并且在该POST 数据中,链接数组包含不为null 的name 元素。

标签: php cakephp file-upload cakephp-3.0


【解决方案1】:

改变这个:

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

到这里:

if ($this->request->is('post') || $this->request->is('put')) {

编辑:正如 ndm 指出的,数组也可以工作:

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

【讨论】:

  • Request::is() 也接受数组。您可能还想包含patch
  • 这行得通,结果我没有正确地完成它(没有把它放在一个数组中)。
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2016-03-23
  • 2015-09-29
相关资源
最近更新 更多