简体   繁体   English

CakePHP,move_uploaded_file()无法正常工作,并且没有出现任何错误

[英]CakePHP, move_uploaded_file() does not work and I am not getting any errors

I am trying to upload a file using move_uploaded_file() in CakePHP3. 我正在尝试使用CakePHP3中的move_uploaded_file()上传文件。

I have used a if statement to check what move_uploaded_file() is returning, and its false. 我使用了if语句来检查正在返回的move_uploaded_file()及其false。 I have attached my code but I think I am using the function correctly. 我已经附上了我的代码,但是我认为我正确地使用了该函数。

My target location is webroot/img/work . 我的目标位置是webroot / img / work

I am not getting any errors. 我没有任何错误。

I have changed dir owner sudo chown www-data:www-data work/ 我已经更改了目录所有者sudo chown www-data:www-data work/

I have changed dir permissions sudo chmod 777 work/ 我已经更改了目录权限sudo chmod 777 work/

I am new to cakePHP so I don't know what else I could try. 我是CakePHP的新手,所以我不知道还能尝试什么。

Here are my files: 这是我的文件:

src/Controller/WorkController.php src / Controller / WorkController.php

    public function add()
    {
        // Get file to be uploaded.
        $file = $this->request->getData('image');
        // Set path to the upload location.
        $target = WWW_ROOT . 'img' . DS . 'work' . DS;

        $work = $this->Work->newEntity();
        if($this->request->is('post')) {
            $work = $this->Work->patchEntity($work, $this->request->getData());
            // Assign value.
            $work['image'] = $file['name'];
            // Move uploaded file.
            move_uploaded_file( $file['name'], $target );
            if($this->Work->save($work)) {
                $this->Flash->success(__('New work item added!'));
                return $this->redirect(['action' => 'index']);
            }
            $this->Flash->error(__('Unable to add new work item.'));
        }
        $this->set('work', $work );

    }

src/Template/Work/add.ctp src /模板/工作/add.ctp

<?php
    echo $this->Form->create($work, array( 'enctype' => 'multipart/form-data'));
    echo $this->Form->control('title');
    echo $this->Form->control('body', ['rows' => '5']);
    echo $this->Form->control('link');
    // echo $this->Form->control('image');
    echo $this->Form->control('image', array('type' => 'file'));
    echo $this->Form->button(__('Add Work'));
    echo $this->Form->end();
?>

Not a lot of information given so here is a couple things: 给出的信息很多,因此这里有几件事:

According to php.net on move_uploaded_file() 根据move.uploaded_file()上的php.net

Returns TRUE on success.

If filename is not a valid upload file, then no action will occur, and move_uploaded_file() will return FALSE.

If filename is a valid upload file, but cannot be moved for some reason, no action will occur, and move_uploaded_file() will return FALSE. Additionally, a warning will be issued.

So obviously it does not issue a warning, so it must not be a valid uploaded file. 因此很明显它不会发出警告,因此它一定不是有效的上传文件。

Looking at your code you are trying to move the file name. 查看您的代码,您正在尝试移动文件名。 When you upload a file via POST, PHP gives it a "tmp_name" which you can access via $file["tmp_name"]. 通过POST上传文件时,PHP会为其提供一个“ tmp_name”,您可以通过$ file [“ tmp_name”]进行访问。

So you should be doing move_uploaded_file($file["tmp_name"], $target); 因此,您应该执行move_uploaded_file($ file [“ tmp_name”],$ target);。

I am assuming it failed because it is looking for (myimg.png) instead of (asdagasfas.png) or whatever PHP named it. 我假设它失败了,因为它正在寻找(myimg.png)而不是(asdagasfas.png)或任何名为它的PHP。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM