This article gives an example of how Yii+upload implements Ajax upload images. Share to everyone for your reference, specific as follows:
Controller code:
/** * Get Ajax uploaded files. */Public Function Actiongetajax () {$model =new attachment ()//load attachment model $tmpFile = Cuploadedfile::getinstancebyname ( //Read the image upload domain, and use the system upload component upload $Directroy = Yii::app ()->params[' uploadpath '];//read upload configuration file, my configuration is/uploads//create File Store path
Path $y = date (' Y ');
$m = Date (' m ');
$d = Date (' d '); $Directroy = $Directroy. "
/"; $PATHD = $Directroy. $y. " /". $m." /". $d."
/"; Tool::makedir (DirName (Yii::app ()->basepath). $pathd);
Create a folder where you must add DirName (Yii::app ()->basepath) Otherwise there may be an error;
if (Is_object ($tmpFile) && get_class ($tmpFile) = = ' Cuploadedfile ') {$filename = time (). rand (0,9); $ext = $tmpFile->extensionname;//upload file extension if ($ext = = ' jpg ' | | $ext = = ' gif ' | | $ext = = ' png ') {$big = $pathd. $filename. ' _600. ' $ext; 310 thumbnail $small = $pathd. $filename. ' _310. ' $ext; 310 thumbnail $thumb = $pathd. $filename. ' _100. ' $ext; 100 thumbnail $moDel->zat_thumb = $thumb; thumbnail} $uploadfile = $pathd. $filename. '.' . $ext; The saved path $model->zat_url = $pathd. $filename. '.' . $ext; Re-assign $model->zat_file_name = $filename. '.' . $ext; File name $model->zat_title = $tmpFile->name; File title $model->zat_file_type = $tmpFile->type; File type $model->zat_file_size = $tmpFile->size;
File size $model->zat_image = 2; $model->zat_ip = Yii::app ()->request->userhostaddress;
Upload IP//print_r ($uploadfile); The IF ($model->save ()) {$tmpFile->saveas (Yii::app ()->basepath). $uploadfile);//Save to server if ($ex t== ' jpg ' | | $ext = = ' gif ' | | $ext = = ' png ') {$img = Yii::app ()->image->load (DirName (Yii::app ()->basepath). $uploadfile);//Use Image-kohana
Image processing Library extension $img->resize (600,600)->quality (85); $img->save (DirName (Yii::app ()->basepath).$BIG);//Generate 600 thumbnail $img->resize (310,310)->quality (85);
$img->save (DirName (Yii::app ()->basepath). $small);//Generate 310 thumbnail $img->resize (100,100)->quality (85); $img->save (DirName (Yii::app ()->basepath). $thumb);//Generate 100 thumbnail} if ($ext = = ' jpg ' | | $ext = = ' gif ' | | $ext = = ' png ') {$str = Json_encode (' upfile ' =>array (' zat_id ' => yi I::app ()->db->getlastinsertid (),//Insert ID ' file ' => $uploadfile,//original artwork ' small ' => $sma
ll,//310 thumbnail ' thumb ' => $thumb,//100 thumbnail)); }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>
For more information on YII-related content, readers who are interested in this site can view the topic: "Introduction to YII framework and common skills Summary", "PHP Excellent development Framework Summary", "Smarty Template Introductory Course", "Php+ajax Tips and Applications", "PHP object-oriented Programming Program", " Summary of PHP string usage, Introduction to PHP+MYSQL database operations, and a summary of PHP common database operations Tips
I hope this article will help you with your PHP programming based on the YII framework.