Yii framework implements image upload source code sharing
The following is the source code for the yii framework to implement the image upload function. I hope it can be helpful to everyone.
(Learning video sharing: Programming video)
1, model
<?php namespace frontend\models; use yii\base\Model; use yii\web\UploadedFile; use yii\db\ActiveRecord; use yii\db\Query; class UploadForm extends ActiveRecord { /** * @var UploadedFile */ public $t_img; public $t_title; public $t_content; public function rules() { return [ [['t_img'], 'file', 'skipOnEmpty' => false, 'extensions' => 'png, jpg,bmp,jpeg'], ]; } public function attributeLabels() { return [ 't_img'=>'请上传文章图片', 'verifyCode' => '请在右面输入验证码', ]; } public function upload() { $imgName=time().rand(100,999).".".$this->t_img->extension; if ($this->validate()) { $this->t_img->saveAs('uploads/' .$imgName); $path='uploads/' .$imgName; return $path; } else { return false; } } } ?>
2, controller
$data=Yii::$app->request->post(); $data['t_addtime']=date('Y-m-d H:i:s'); $upload->t_img = UploadedFile::getInstance($upload, 't_img'); $path=$upload->upload();
3. View layer
<?php use yii\widgets\ActiveForm; use yii\helpers\Html; use yii\helpers\Url; ?> <?=Html::a('返回','?r=course/classspace&c_id='.$c_id)?> <?php $form=ActiveForm::begin( [ 'options' => ['enctype' => 'multipart/form-data'], 'method'=>'POST', ] );?> <table class="table"> <tr> <td> <input type="text" placeholder="请填写话题标题" name="t_title" id="t_title" value=<?=$coursedraft['d_title']?> > </td> </tr> <tr> <td> <textarea name="t_content" id="t_content" cols="30" rows="10" placeholder="请填写话题内容"><?=$coursedraft['d_content']?></textarea> </td> </tr> <tr> <td> <?=$form->field($upload,'t_img')->fileInput()?> </td> </tr> <tr> <div class="btn-group"> <td> <?=Html::submitButton('提交话题',['class'=>'btn btn-success'])?> </td> </div> </tr> </table> <?php ActiveForm::end();?> <input type="hidden" value=<?=$c_id?> id="c_id" /> </body> <?php $js = <<<END $(function(){ // $(document).on('click','#caogao',function() { // var title = $("#t_title").val(); // var content = $("#t_content").val(); // // $.ajax({ // type: "POST", // url: "?r=course/coursedraft", // data: {t_title: title, t_content: content, d_id: d_id} // }) // }) function show(){ var title=$("#t_title").val(); var content=$("#t_content").val(); var c_id=$('#c_id').val(); $.ajax({ type: "POST", url: "?r=course/coursedraft", data: {d_title:title,d_content:content,c_id:c_id,d_state:0} }); } setInterval(show,5000); }) END; $this->registerJs($js); ?> </html>
Related recommendations: yii framework
The above is the detailed content of Yii framework implements image upload source code sharing. 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

Video Face Swap
Swap faces in any video effortlessly with our completely free AI face swap tool!

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

As a high-level programming language, Python language is easy to learn, easy to read and write, and has been widely used in the field of software development. However, due to the open source nature of Python, the source code is easily accessible to others, which brings some challenges to software source code protection. Therefore, in practical applications, we often need to take some methods to protect Python source code and ensure its security. In software source code protection, there are a variety of application practices for Python to choose from. Below are some common

Yii framework middleware: providing multiple data storage support for applications Introduction Middleware (middleware) is an important concept in the Yii framework, which provides multiple data storage support for applications. Middleware acts like a filter, inserting custom code between an application's requests and responses. Through middleware, we can process, verify, filter requests, and then pass the processed results to the next middleware or final handler. Middleware in the Yii framework is very easy to use

How to display the source code of PHP code in the browser without being interpreted and executed? PHP is a server-side scripting language commonly used to develop dynamic web pages. When a PHP file is requested on the server, the server interprets and executes the PHP code in it and sends the final HTML content to the browser for display. However, sometimes we want to display the source code of the PHP file directly in the browser instead of being executed. This article will introduce how to display the source code of PHP code in the browser without being interpreted and executed. In PHP, you can use

You can use the browser's developer tools to view the source code of the website. In the Google Chrome browser: 1. Open the Chrome browser and visit the website where you want to view the source code; 2. Right-click anywhere on the web page and select "Inspect" or press the shortcut key Ctrl + Shift + I to open the developer tools; 3. In the top menu bar of the developer tools, select the "Elements" tab; 4. Just see the HTML and CSS code of the website.

Steps to implement web page caching and page chunking using the Yii framework Introduction: During the web development process, in order to improve the performance and user experience of the website, it is often necessary to cache and chunk the page. The Yii framework provides powerful caching and layout functions, which can help developers quickly implement web page caching and page chunking. This article will introduce how to use the Yii framework to implement web page caching and page chunking. 1. Turn on web page caching. In the Yii framework, web page caching can be turned on through the configuration file. Open the main configuration file co

With the rapid development of web applications, modern web development has become an important skill. Many frameworks and tools are available for developing efficient web applications, among which the Yii framework is a very popular framework. Yii is a high-performance, component-based PHP framework that uses the latest design patterns and technologies, provides powerful tools and components, and is ideal for building complex web applications. In this article, we will discuss how to use Yii framework to build web applications. Install Yii framework first,

Yii framework middleware: Add logging and debugging capabilities to applications [Introduction] When developing web applications, we usually need to add some additional features to improve the performance and stability of the application. The Yii framework provides the concept of middleware that enables us to perform some additional tasks before and after the application handles the request. This article will introduce how to use the middleware function of the Yii framework to implement logging and debugging functions. [What is middleware] Middleware refers to the processing of requests and responses before and after the application processes the request.

By understanding the Golang framework source code, developers can master the essence of the language and expand the framework's functions. First, get the source code and become familiar with its directory structure. Second, read the code, trace the execution flow, and understand dependencies. Practical examples show how to apply this knowledge: create custom middleware and extend the routing system. Best practices include learning step-by-step, avoiding mindless copy-pasting, utilizing tools, and referring to online resources.
