Home> PHP Framework> YII> body text

yii2 failed to upload file

王林
Release: 2020-02-18 15:17:13
Original
2012 people have browsed it

yii2 failed to upload file

Let’s take a look at the code first:

First look at the View part:


Copy after login

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

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 == 'datafile') { $this->enableCsrfValidation = false; } return parent::beforeAction($action); }
Copy after login

For more programming-related content, please pay attention to theProgramming Introductioncolumn 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!

Related labels:
source:yii2上传文件失败
Statement of this Website
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
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template
About us Disclaimer Sitemap
php.cn:Public welfare online PHP training,Help PHP learners grow quickly!