This article mainly introduces the method of Yii upload to implement AJAX upload pictures, and analyzes the related techniques of Yii combined with the upload plug-in to implement ajax upload. It has certain reference value. Friends in need can refer to this article
The example describes how Yii upload implements AJAX to upload images. Share it with everyone for your reference, the details are as follows:
Controller code:
/** * get ajax uploaded files. */ public function actionGetAjax(){ $model=new Attachment();//加载附件模型 $tmpFile = CUploadedFile::getInstanceByName('image');//读取图像上传域,并使用系统上传组件上传 $Directroy = Yii::app()->params['uploadPath'];//读取上传配置文件,我的配置是/uploads //创建文件存放路径 $y = date('Y'); $m = date('m'); $d = date('d'); $Directroy = $Directroy."/"; $pathd = $Directroy.$y."/".$m."/".$d."/"; Tool::makedir(dirname(Yii::app()->BasePath).$pathd); //创建文件夹,此处一定要加上dirname(Yii::app()->BasePath)不然可能会出错; if(is_object($tmpFile) && get_class($tmpFile)==='CUploadedFile'){ $filename = time().rand(0,9); $ext = $tmpFile->extensionName;//上传文件的扩展名 if($ext=='jpg'||$ext=='gif'||$ext=='png'){ $big = $pathd . $filename . '_600.' . $ext; //310缩略图 $small = $pathd . $filename . '_310.' . $ext; //310缩略图 $thumb = $pathd . $filename . '_100.' . $ext; //100缩略图 $model->zat_thumb = $thumb; //缩略图 } $uploadfile = $pathd . $filename . '.' . $ext; //保存的路径 $model->zat_url = $pathd . $filename . '.' . $ext; //重新赋值 $model->zat_file_name = $filename . '.' . $ext; //文件名称 $model->zat_title = $tmpFile->name; //文件标题 $model->zat_file_type = $tmpFile->type; //文件类型 $model->zat_file_size = $tmpFile->size; //文件大小 $model->zat_image = 2; $model->zat_ip = Yii::app()->request->userHostAddress; //上传IP //print_r($uploadfile); } if($model->save()){ $tmpFile->saveAs(dirname(Yii::app()->BasePath).$uploadfile);//保存到服务器 if($ext=='jpg'||$ext=='gif'||$ext=='png'){ $img = Yii::app()->image->load(dirname(Yii::app()->BasePath).$uploadfile); //使用image-Kohana图像处理库扩展 $img->resize(600,600)->quality(85); $img->save(dirname(Yii::app()->BasePath).$big);//生成600缩略图 $img->resize(310,310)->quality(85); $img->save(dirname(Yii::app()->BasePath).$small);//生成310缩略图 $img->resize(100,100)->quality(85); $img->save(dirname(Yii::app()->BasePath).$thumb);//生成100缩略图 } if($ext=='jpg'||$ext=='gif'||$ext=='png'){ $str = json_encode( array( 'upfile'=>array( 'zat_id' => Yii::app()->db->getLastInsertID(), //取插ID 'file' => $uploadfile,//原图 'small' => $small,//310缩略图 'thumb' => $thumb,//100缩略图 ) ) ); }else{ $str = json_encode( array( 'upfile'=>array( 'zat_id' => Yii::app()->db->getLastInsertID(), 'file' => $uploadfile, ) ) ); } echo $str; } }
VIEW code :
<input id="fileupload" type="file" name="image" multiple> <script src="<?php echo Yii::app()->baseUrl;?>/js/jquery.ui.widget.js"></script> <script src="<?php echo Yii::app()->baseUrl;?>/js/jquery.iframe-transport.js"></script> <script src="<?php echo Yii::app()->baseUrl;?>/js/jquery.fileupload.js"></script> <script> $(function () { $('#fileupload').fileupload({ dataType: 'json', url: '/Attachment/GetAjax', success: function (json) { $('#MemType_zmt_pic').attr('value',json.upfile.file); $("#images").attr('src',json.upfile.file); } }); }); </script>
Related recommendations:
Yii2 implements ActiveForm ajax submission
##
The above is the detailed content of Yii+upload implements AJAX method to upload images. For more information, please follow other related articles on the PHP Chinese website!

The article explains how to create, implement, and use interfaces in PHP, focusing on their benefits for code organization and maintainability.

The article discusses the differences between crypt() and password_hash() in PHP for password hashing, focusing on their implementation, security, and suitability for modern web applications.

Article discusses preventing Cross-Site Scripting (XSS) in PHP through input validation, output encoding, and using tools like OWASP ESAPI and HTML Purifier.

Autoloading in PHP automatically loads class files when needed, improving performance by reducing memory use and enhancing code organization. Best practices include using PSR-4 and organizing code effectively.

PHP streams unify handling of resources like files, network sockets, and compression formats via a consistent API, abstracting complexity and enhancing code flexibility and efficiency.

The article discusses managing file upload sizes in PHP, focusing on the default limit of 2MB and how to increase it by modifying php.ini settings.

The article discusses nullable types in PHP, introduced in PHP 7.1, allowing variables or parameters to be either a specified type or null. It highlights benefits like improved readability, type safety, and explicit intent, and explains how to declar

The article discusses the differences between unset() and unlink() functions in programming, focusing on their purposes and use cases. Unset() removes variables from memory, while unlink() deletes files from the filesystem. Both are crucial for effec


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

ZendStudio 13.5.1 Mac
Powerful PHP integrated development environment

MantisBT
Mantis is an easy-to-deploy web-based defect tracking tool designed to aid in product defect tracking. It requires PHP, MySQL and a web server. Check out our demo and hosting services.

SecLists
SecLists is the ultimate security tester's companion. It is a collection of various types of lists that are frequently used during security assessments, all in one place. SecLists helps make security testing more efficient and productive by conveniently providing all the lists a security tester might need. List types include usernames, passwords, URLs, fuzzing payloads, sensitive data patterns, web shells, and more. The tester can simply pull this repository onto a new test machine and he will have access to every type of list he needs.

Notepad++7.3.1
Easy-to-use and free code editor

DVWA
Damn Vulnerable Web App (DVWA) is a PHP/MySQL web application that is very vulnerable. Its main goals are to be an aid for security professionals to test their skills and tools in a legal environment, to help web developers better understand the process of securing web applications, and to help teachers/students teach/learn in a classroom environment Web application security. The goal of DVWA is to practice some of the most common web vulnerabilities through a simple and straightforward interface, with varying degrees of difficulty. Please note that this software
