yii使用activeFileField控件实现上传文件与图片的方法,
yii使用activeFileField控件实现上传文件与图片的方法,
本文实例讲述了yii使用activeFileField控件实现上传文件与图片的方法。分享给大家供大家参考,具体如下:
yii框架提供了activeFileField控件来完成上传文件(当然也包括了上传图片)的操作,下面介绍yii的activeFileField使用方法。
1、函数原型:
复制代码 代码如下:public static string activeFileField(CModel $model, string $attribute, array $htmlOptions=array ( ))
2、调用例子:
(1)首先,设置form,这一步一 定要做,把form设置为'multipart/form-data',具体请看我的:
<?php $form=$this->beginWidget('CActiveForm', array( 'id'=>'books-form', 'enableAjaxValidation'=>false, 'htmlOptions'=>array('enctype'=>'multipart/form-data'), )); ?>
(2) 接着,在view下的form里设置:
<div class="row"> <?php echo $form->labelEx($model,'BookImg'); ?> <?php echo CHtml::activeFileField($model,'BookImg'); ?> <?php echo $form->error($model,'BookImg'); ?> </div>
(3) 如果你想预览图片,那么请注意了,可以加上这么一段:
<div class="row"> <?php echo '图片预览' ?> <?php echo '<img src="/static/imghw/default1.png" data-src="http://www.XXXX.com/'.$model- alt="yii使用activeFileField控件实现上传文件与图片的方法," >BookImg.'" class="lazy".$model->BookImg.'" style="max-width:90%"/>'; ?> </div>
(4)最后,需要在控制类里加上下面的:
if($model->save()) { $image=CUploadedFile::getInstance($model,'BookImg'); if (is_object($image) && get_class($image)==='CUploadedFile') { $image->saveAs("D:/aaa/aa.jpg");//路径必须真实存在,并且如果是linux系统,必须有修改权限 } $this->redirect(array('view','id'=>$model->BookId)); }
请注意:这里是添加的时候使用的,修改的话要有所改变。
(5)限制上传的文件必须是图片,还有限制图片大小,那么请到model层里的rules新增这么一句:
array('BookImg', 'file','allowEmpty'=>true, 'types'=>'jpg, gif, png', 'maxSize'=>1024 * 1024 * 1, // 1MB 'tooLarge'=>'The file was larger than 1MB. Please upload a smaller file.', )
希望本文所述对大家基于Yii框架的PHP程序设计有所帮助。
您可能感兴趣的文章:
- Yii框架登录流程分析
- Yii中CGridView实现批量删除的方法
- Yii基于数组和对象的Model查询技巧实例详解
- yii权限控制的方法(三种方法)
- yii实现使用CUploadedFile上传文件的方法
- Yii中Model(模型)的创建及使用方法
- yii数据库的查询方法
- Yii中实现处理前后台登录的新方法

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











With the continuous development of social media, Xiaohongshu has become a platform for more and more young people to share their lives and discover beautiful things. Many users are troubled by auto-save issues when posting images. So, how to solve this problem? 1. How to solve the problem of automatically saving pictures when publishing on Xiaohongshu? 1. Clear the cache First, we can try to clear the cache data of Xiaohongshu. The steps are as follows: (1) Open Xiaohongshu and click the "My" button in the lower right corner; (2) On the personal center page, find "Settings" and click it; (3) Scroll down and find the "Clear Cache" option. Click OK. After clearing the cache, re-enter Xiaohongshu and try to post pictures to see if the automatic saving problem is solved. 2. Update the Xiaohongshu version to ensure that your Xiaohongshu

With the popularity of Douyin short videos, user interactions in the comment area have become more colorful. Some users wish to share images in comments to better express their opinions or emotions. So, how to post pictures in TikTok comments? This article will answer this question in detail and provide you with some related tips and precautions. 1. How to post pictures in Douyin comments? 1. Open Douyin: First, you need to open Douyin APP and log in to your account. 2. Find the comment area: When browsing or posting a short video, find the place where you want to comment and click the "Comment" button. 3. Enter your comment content: Enter your comment content in the comment area. 4. Choose to send a picture: In the interface for entering comment content, you will see a "picture" button or a "+" button, click

In PowerPoint, it is a common technique to display pictures one by one, which can be achieved by setting animation effects. This guide details the steps to implement this technique, including basic setup, image insertion, adding animation, and adjusting animation order and timing. Additionally, advanced settings and adjustments are provided, such as using triggers, adjusting animation speed and order, and previewing animation effects. By following these steps and tips, users can easily set up pictures to appear one after another in PowerPoint, thereby enhancing the visual impact of the presentation and grabbing the attention of the audience.

When deleting or decompressing a folder on your computer, sometimes a prompt dialog box "Error 0x80004005: Unspecified Error" will pop up. How should you solve this situation? There are actually many reasons why the error code 0x80004005 is prompted, but most of them are caused by viruses. We can re-register the dll to solve the problem. Below, the editor will explain to you the experience of handling the 0x80004005 error code. Some users are prompted with error code 0X80004005 when using their computers. The 0x80004005 error is mainly caused by the computer not correctly registering certain dynamic link library files, or by a firewall that does not allow HTTPS connections between the computer and the Internet. So how about

Apple's recent iPhones capture memories with crisp detail, saturation and brightness. But sometimes, you may encounter some issues that may cause the image to look less clear. While autofocus on iPhone cameras has come a long way, allowing you to take photos quickly, the camera can mistakenly focus on the wrong subject in certain situations, making the photo blurry in unwanted areas. If your photos on your iPhone look out of focus or lack sharpness overall, the following post should help you make them sharper. How to Make Pictures Clearer on iPhone [6 Methods] You can try using the native Photos app to clean up your photos. If you want more features and options

Some netizens found that when they opened the browser web page, the pictures on the web page could not be loaded for a long time. What happened? I checked that the network is normal, so where is the problem? The editor below will introduce to you six solutions to the problem that web page images cannot be loaded. Web page images cannot be loaded: 1. Internet speed problem The web page cannot display images. It may be because the computer's Internet speed is relatively slow and there are more softwares opened on the computer. And the images we access are relatively large, which may be due to loading timeout. As a result, the picture cannot be displayed. You can turn off the software that consumes more network speed. You can go to the task manager to check. 2. Too many visitors. If the webpage cannot display pictures, it may be because the webpages we visited were visited at the same time.

A file path is a string used by the operating system to identify and locate a file or folder. In file paths, there are two common symbols separating paths, namely forward slash (/) and backslash (). These two symbols have different uses and meanings in different operating systems. The forward slash (/) is a commonly used path separator in Unix and Linux systems. On these systems, file paths start from the root directory (/) and are separated by forward slashes between each directory. For example, the path /home/user/Docume

When using WPS office software, we found that not only one form is used, tables and pictures can be added to the text, pictures can also be added to the table, etc. These are all used together to make the content of the entire document look richer. , if you need to insert two pictures into the document and they need to be arranged side by side. Our next course can solve this problem: how to place two pictures side by side in a wps document. 1. First, you need to open the WPS software and find the picture you want to adjust. Left-click the picture and a menu bar will pop up, select "Page Layout". 2. Select "Tight wrapping" in text wrapping. 3. After all the pictures you need are confirmed to be set to "Tight text wrapping", you can drag the pictures to the appropriate position and click on the first picture.
