Home PHP Framework YII yii implements image uploading

yii implements image uploading

Nov 27, 2020 pm 04:54 PM
yii upload picture

yii implements image uploading

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 [
            [[&#39;t_img&#39;], &#39;file&#39;, &#39;skipOnEmpty&#39; => false, &#39;extensions&#39; => &#39;png, jpg,bmp,jpeg&#39;],
        ];
    }
    public function attributeLabels()
    {
        return [
            &#39;t_img&#39;=>&#39;请上传文章图片&#39;,
            &#39;verifyCode&#39; => &#39;请在右面输入验证码&#39;,
        ];
    }
 
 
    public function upload()
    {
        $imgName=time().rand(100,999).".".$this->t_img->extension;
        if ($this->validate()) {
            $this->t_img->saveAs(&#39;uploads/&#39; .$imgName);
            $path=&#39;uploads/&#39; .$imgName;
            return $path;
        } else {
            return false;
        }
    }
}
 
?>
Copy after login

2. Controller

 $data=Yii::$app->request->post();
            $data[&#39;t_addtime&#39;]=date(&#39;Y-m-d H:i:s&#39;);
            $upload->t_img = UploadedFile::getInstance($upload, &#39;t_img&#39;);
            $path=$upload->upload();
Copy after login

3. View layer

<?php
use yii\widgets\ActiveForm;
use yii\helpers\Html;
use yii\helpers\Url;
?>
<?=Html::a(&#39;返回&#39;,&#39;?r=course/classspace&c_id=&#39;.$c_id)?>
<?php $form=ActiveForm::begin(
    [
        &#39;options&#39; => [&#39;enctype&#39; => &#39;multipart/form-data&#39;],
        &#39;method&#39;=>&#39;POST&#39;,
    ]
);?>
<table class="table">
 
    <tr>
        <td>
 
            <input type="text" placeholder="请填写话题标题" name="t_title" id="t_title" value=<?=$coursedraft[&#39;d_title&#39;]?>   >
        </td>
        </tr>
    <tr>
        <td>
            <textarea name="t_content" id="t_content" cols="30" rows="10" placeholder="请填写话题内容"><?=$coursedraft[&#39;d_content&#39;]?></textarea>
        </td>
    </tr>
    <tr>
        <td>
           <?=$form->field($upload,&#39;t_img&#39;)->fileInput()?>
        </td>
    </tr>
    <tr>
        <div class="btn-group">
            <td>
                <?=Html::submitButton(&#39;提交话题&#39;,[&#39;class&#39;=>&#39;btn btn-success&#39;])?>
            </td>
        </div>
 
    </tr>
</table>
<?php ActiveForm::end();?>
<input type="hidden" value=<?=$c_id?>  id="c_id" />
</body>
<?php
$js = <<<END
    $(function(){
//        $(document).on(&#39;click&#39;,&#39;#caogao&#39;,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=$(&#39;#c_id&#39;).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>
Copy after login

The above is the detailed content of yii implements image uploading. 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)
2 weeks ago By 尊渡假赌尊渡假赌尊渡假赌
Hello Kitty Island Adventure: How To Get Giant Seeds
1 months ago By 尊渡假赌尊渡假赌尊渡假赌
Two Point Museum: All Exhibits And Where To Find Them
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)

WeChat applet implements image upload function WeChat applet implements image upload function Nov 21, 2023 am 09:08 AM

WeChat applet implements picture upload function With the development of mobile Internet, WeChat applet has become an indispensable part of people's lives. WeChat mini programs not only provide a wealth of application scenarios, but also support developer-defined functions, including image upload functions. This article will introduce how to implement the image upload function in the WeChat applet and provide specific code examples. 1. Preparatory work Before starting to write code, we need to download and install the WeChat developer tools and register as a WeChat developer. At the same time, you also need to understand WeChat

Steps to implement image uploading and display using CakePHP framework Steps to implement image uploading and display using CakePHP framework Jul 29, 2023 pm 04:21 PM

Steps to implement image upload and display using CakePHP framework Introduction: In modern web applications, image upload and display are common functional requirements. The CakePHP framework provides developers with powerful functions and convenient tools, making it simple and efficient to upload and display images. This article will introduce you to how to use the CakePHP framework to upload and display images. Step 1: Create a file upload form First, we need to create a form in the view file for users to upload images. The following is an example of

How to handle image uploading and compression in Vue technology development How to handle image uploading and compression in Vue technology development Oct 08, 2023 am 10:58 AM

How to handle image uploading and compression in Vue technology development In modern web applications, image uploading is a very common requirement. However, due to network transmission and storage reasons, directly uploading original high-resolution images may result in slow upload speeds and a large waste of storage space. Therefore, uploading and compressing images is very important. In Vue technology development, we can use some ready-made solutions to handle image uploading and compression. The following will introduce how to use vue-upload-comone

How to use PHP and Vue to implement image upload function How to use PHP and Vue to implement image upload function Sep 25, 2023 pm 03:17 PM

How to use PHP and Vue to implement the image upload function. In modern web development, the image upload function is a very common requirement. This article will introduce in detail how to use PHP and Vue to implement the image upload function, and provide specific code examples. 1. Front-end part (Vue) First, you need to create a form for uploading images on the front-end. The specific code is as follows:&lt;template&gt;&lt;div&gt;&lt;inputtype="fil

Problems encountered in image uploading and cropping when using Vue development Problems encountered in image uploading and cropping when using Vue development Oct 08, 2023 pm 04:12 PM

Title: Image uploading and cropping problems and solutions in Vue development Introduction: In Vue development, image uploading and cropping are common requirements. This article will introduce the image uploading and cropping problems encountered in Vue development, and give solutions and specific code examples. 1. Image upload problem: Selecting the image upload button cannot trigger the file selection box: This problem is usually because the event is not bound correctly or the bound event does not take effect. You can bind the click event in the template and trigger the file selection box in the corresponding method. Code example:

How to use PHP framework Yii to develop a highly available cloud backup system How to use PHP framework Yii to develop a highly available cloud backup system Jun 27, 2023 am 09:04 AM

With the continuous development of cloud computing technology, data backup has become something that every enterprise must do. In this context, it is particularly important to develop a highly available cloud backup system. The PHP framework Yii is a powerful framework that can help developers quickly build high-performance web applications. The following will introduce how to use the Yii framework to develop a highly available cloud backup system. Designing the database model In the Yii framework, the database model is a very important part. Because the data backup system requires a lot of tables and relationships

Yii2 vs Phalcon: Which framework is better for developing graphics rendering applications? Yii2 vs Phalcon: Which framework is better for developing graphics rendering applications? Jun 19, 2023 am 08:09 AM

In the current information age, big data, artificial intelligence, cloud computing and other technologies have become the focus of major enterprises. Among these technologies, graphics card rendering technology, as a high-performance graphics processing technology, has received more and more attention. Graphics card rendering technology is widely used in game development, film and television special effects, engineering modeling and other fields. For developers, choosing a framework that suits their projects is a very important decision. Among current languages, PHP is a very dynamic language. Some excellent PHP frameworks such as Yii2, Ph

Data query in Yii framework: access data efficiently Data query in Yii framework: access data efficiently Jun 21, 2023 am 11:22 AM

The Yii framework is an open source PHP Web application framework that provides numerous tools and components to simplify the process of Web application development, of which data query is one of the important components. In the Yii framework, we can use SQL-like syntax to access the database to query and manipulate data efficiently. The query builder of the Yii framework mainly includes the following types: ActiveRecord query, QueryBuilder query, command query and original SQL query

See all articles