The specific code is as follows:
(Recommended tutorial: yii)
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>
The above is the detailed content of yii implements image uploading. For more information, please follow other related articles on the PHP Chinese website!

Migratingalaravel Projecttoyiiishallingbutachieffable WITHIEFLEFLANT.1) Mapoutlaravel component likeroutes, Controllers, Andmodels.2) Translatelaravel's SartisancommandeloequentTooyii's giiandetiverecordeba

Soft skills are crucial to Yii developers because they facilitate team communication and collaboration. 1) Effective communication ensures that the project is progressing smoothly, such as through clear API documentation and regular meetings. 2) Collaborate to enhance team interaction through Yii's tools such as Gii to improve development efficiency.

Laravel'sMVCarchitectureoffersenhancedcodeorganization,improvedmaintainability,andarobustseparationofconcerns.1)Itkeepscodeorganized,makingnavigationandteamworkeasier.2)Itcompartmentalizestheapplication,simplifyingtroubleshootingandmaintenance.3)Itse

Yiiremainsrelevantinmodernwebdevelopmentforprojectsneedingspeedandflexibility.1)Itoffershighperformance,idealforapplicationswherespeediscritical.2)Itsflexibilityallowsfortailoredapplicationstructures.However,ithasasmallercommunityandsteeperlearningcu

Yii frameworks remain strong in many PHP frameworks because of their efficient, simplicity and scalable design concepts. 1) Yii improves development efficiency through "conventional optimization over configuration"; 2) Component-based architecture and powerful ORM system Gii enhances flexibility and development speed; 3) Performance optimization and continuous updates and iterations ensure its sustained competitiveness.

Yii is still suitable for projects that require high performance and flexibility in modern web development. 1) Yii is a high-performance framework based on PHP, following the MVC architecture. 2) Its advantages lie in its efficient, simplified and component-based design. 3) Performance optimization is mainly achieved through cache and ORM. 4) With the emergence of the new framework, the use of Yii has changed.

Yii and PHP can create dynamic websites. 1) Yii is a high-performance PHP framework that simplifies web application development. 2) Yii provides MVC architecture, ORM, cache and other functions, which are suitable for large-scale application development. 3) Use Yii's basic and advanced features to quickly build a website. 4) Pay attention to configuration, namespace and database connection issues, and use logs and debugging tools for debugging. 5) Improve performance through caching and optimization queries, and follow best practices to improve code quality.

The Yii framework stands out in the PHP framework, and its advantages include: 1. MVC architecture and component design to improve code organization and reusability; 2. Gii code generator and ActiveRecord to improve development efficiency; 3. Multiple caching mechanisms to optimize performance; 4. Flexible RBAC system to simplify permission management.


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

Atom editor mac version download
The most popular open source editor

SublimeText3 English version
Recommended: Win version, supports code prompts!

SAP NetWeaver Server Adapter for Eclipse
Integrate Eclipse with SAP NetWeaver application server.

PhpStorm Mac version
The latest (2018.2.1) professional PHP integrated development tool

MinGW - Minimalist GNU for Windows
This project is in the process of being migrated to osdn.net/projects/mingw, you can continue to follow us there. MinGW: A native Windows port of the GNU Compiler Collection (GCC), freely distributable import libraries and header files for building native Windows applications; includes extensions to the MSVC runtime to support C99 functionality. All MinGW software can run on 64-bit Windows platforms.
