yii2 failed to upload file
Let’s take a look at the code first:
First look at the View part:
<form action="<?= Url::to(['default/datafile']) ?>" 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 = '没有文件上传'; } 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; }
found after execution
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); }
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!

Hot AI Tools

Undresser.AI Undress
AI-powered app for creating realistic nude photos

AI Clothes Remover
Online AI tool for removing clothes from photos.

Undress AI Tool
Undress images for free

Clothoff.io
AI clothes remover

AI Hentai Generator
Generate AI Hentai for free.

Hot Article

Hot Tools

Notepad++7.3.1
Easy-to-use and free code editor

SublimeText3 Chinese version
Chinese version, very easy to use

Zend Studio 13.0.1
Powerful PHP integrated development environment

Dreamweaver CS6
Visual web development tools

SublimeText3 Mac version
God-level code editing software (SublimeText3)

Hot Topics



The article discusses best practices for deploying Yii applications in cloud-native environments, focusing on scalability, reliability, and efficiency through containerization, orchestration, and security measures.

The article discusses key considerations for using Yii in serverless architectures, focusing on statelessness, cold starts, function size, database interactions, security, and monitoring. It also covers optimization strategies and potential integrati

The article discusses tools for monitoring and profiling Yii application performance, including Yii Debug Toolbar, Blackfire, New Relic, Xdebug, and APM solutions like Datadog and Dynatrace.

Yii's built-in testing framework enhances application testing with features like PHPUnit integration, fixture management, and support for various test types, improving code quality and development practices.

The article discusses strategies for testing Yii applications using Codeception, focusing on using built-in modules, BDD, different test types, mocking, CI integration, and code coverage.

The article discusses implementing real-time data synchronization using Yii and WebSockets, covering setup, integration, and best practices for performance and security.

The article discusses key considerations for deploying Yii applications in production, focusing on environment setup, configuration management, performance optimization, security, logging, monitoring, deployment strategies, and backup/recovery plans.

The article discusses Yii's benefits for SaaS development, focusing on performance, security, and rapid development features to enhance scalability and reduce time-to-market.
