Home  >  Article  >  PHP Framework  >  yii2 failed to upload file

yii2 failed to upload file

王林
王林Original
2020-02-18 15:17:132047browse

yii2 failed to upload file

Let’s take a look at the code first:

First look at the View part:

<form action="<?= Url::to([&#39;default/datafile&#39;]) ?>" method="post" enctype="multipart/form-data">
    <label for="file">Filename:</label>
    <input type="file" name="myFile" id="file" />
    <br />
    <input type="submit" name="submit" value="Submit" />
</form>

The above action is built using the YII helper class The url that can be recognized internally is actionDatafile()

in DeaufaultController.php (recommended tutorial: yii framework)

 public function actionDatafile(){
        if(empty($_FILES)){
            $status = 1;
            $info = &#39;没有文件上传&#39;;
        }
        if($_FILES[&#39;myFile&#39;][&#39;error&#39;] === 0 || $_FILES[&#39;myFile&#39;][&#39;error&#39;] === &#39;0&#39; ){
            //文件上传成功
            $tmp = pathinfo($_FILES[&#39;myFile&#39;][&#39;name&#39;]);
            $new_fname = $tmp[&#39;filename&#39;].&#39;_&#39;.rand(1000000,9999999).&#39;.&#39;.$tmp[&#39;extension&#39;];
            echo $new_fname;
            if(!move_uploaded_file($_FILES[&#39;myFile&#39;][&#39;tmp_name&#39;], &#39;../runtime/file/&#39;.$new_fname)){
                $status = 1;
                $info = &#39;上传(移动)失败&#39;;
            }else{
                $status = 0;
                $info = &#39;上传成功&#39;;
            }
        } else {
            //文件上传失败
            $info = &#39;文件上传失败&#39;;
            switch($_FILES[&#39;myFile&#39;][&#39;error&#39;]){
                case 1:
                    $info = &#39;上传文件超过php.ini中upload_max_filesize配置参数&#39;;
                    break;
                case 2:
                    $info = &#39;上传文件超过表单MAX_FILE_SIZE选项指定的值&#39;;
                    break;
                case 3:
                    $info = &#39;文件只有部份被上传&#39;;
                    break;
                case 4:
                    $info = &#39;没有文件被上传&#39;;
                    break;
                case 5:
                    $info = &#39;上传文件大小为0&#39;;
                    break;
            }
            $status = 1;
        }
        return $info;
    }

found after execution

yii2 failed to upload file

Solution:

1. Check the configuration (php.ini)

file_uploads, upload_max_filesize, post_max_size, and upload_tmp_dir have been set.

2. Check the parameters

Found crsf in the parameters. This parameter is included in the yii framework verification. When it comes to verification, it is similar to the error message. Add the cancellation verification code, as follows:

public function beforeAction($action)
    {
        if ($action->id == &#39;datafile&#39;) {
            $this->enableCsrfValidation = false;
        }
 
        return parent::beforeAction($action);
    }

For more programming-related content, please pay attention to the Programming Introduction column on the php Chinese website!

The above is the detailed content of yii2 failed to upload file. For more information, please follow other related articles on the PHP Chinese website!

Statement:
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn