Home PHP Framework YII yii2 failed to upload file

yii2 failed to upload file

Feb 18, 2020 pm 03:17 PM
yii2

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

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 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

Hot AI Tools

Undresser.AI Undress

Undresser.AI Undress

AI-powered app for creating realistic nude photos

AI Clothes Remover

AI Clothes Remover

Online AI tool for removing clothes from photos.

Undress AI Tool

Undress AI Tool

Undress images for free

Clothoff.io

Clothoff.io

AI clothes remover

AI Hentai Generator

AI Hentai Generator

Generate AI Hentai for free.

Hot Article

R.E.P.O. Energy Crystals Explained and What They Do (Yellow Crystal)
4 weeks ago By 尊渡假赌尊渡假赌尊渡假赌
R.E.P.O. Best Graphic Settings
4 weeks ago By 尊渡假赌尊渡假赌尊渡假赌
R.E.P.O. How to Fix Audio if You Can't Hear Anyone
4 weeks ago By 尊渡假赌尊渡假赌尊渡假赌
WWE 2K25: How To Unlock Everything In MyRise
1 months ago By 尊渡假赌尊渡假赌尊渡假赌

Hot Tools

Notepad++7.3.1

Notepad++7.3.1

Easy-to-use and free code editor

SublimeText3 Chinese version

SublimeText3 Chinese version

Chinese version, very easy to use

Zend Studio 13.0.1

Zend Studio 13.0.1

Powerful PHP integrated development environment

Dreamweaver CS6

Dreamweaver CS6

Visual web development tools

SublimeText3 Mac version

SublimeText3 Mac version

God-level code editing software (SublimeText3)

What Are the Best Practices for Using Yii in a Cloud-Native Environment? What Are the Best Practices for Using Yii in a Cloud-Native Environment? Mar 18, 2025 pm 04:39 PM

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.

What Are the Key Considerations for Using Yii in a Serverless Architecture? What Are the Key Considerations for Using Yii in a Serverless Architecture? Mar 18, 2025 pm 04:33 PM

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

What Are the Best Tools for Monitoring and Profiling Yii Application Performance? What Are the Best Tools for Monitoring and Profiling Yii Application Performance? Mar 17, 2025 pm 01:52 PM

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.

What Are the Key Features of Yii's Built-in Testing Framework? What Are the Key Features of Yii's Built-in Testing Framework? Mar 18, 2025 pm 04:41 PM

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.

What Are the Best Strategies for Testing Yii Applications with Codeception? What Are the Best Strategies for Testing Yii Applications with Codeception? Mar 18, 2025 pm 04:27 PM

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.

How to Implement Real-Time Data Synchronization with Yii and WebSockets? How to Implement Real-Time Data Synchronization with Yii and WebSockets? Mar 18, 2025 pm 04:34 PM

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

What Are the Key Considerations for Deploying Yii Applications in Production? What Are the Key Considerations for Deploying Yii Applications in Production? Mar 17, 2025 pm 01:58 PM

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.

What Are the Key Benefits of Using Yii for Building SaaS Applications? What Are the Key Benefits of Using Yii for Building SaaS Applications? Mar 18, 2025 pm 04:25 PM

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.

See all articles