標籤:
jquery uploadify是一款Ajax風格的批量圖片上傳外掛程式,在PHP中使用jquery uploadify很方便,請按照本文介紹的方法和步驟,為你的PHP程式增加jquery uploadify外掛程式的批量上傳圖片功能。
本文是以dilicms為基礎,為其增加圖片上傳功能。
1.增加資料表dili_fieldtypes新欄位:k=>image,V=>圖片上傳地區(VACHAR);
2.修改application/libraries/dili/Field_behavior.php,在switch中增加如下代碼:
case ‘image‘:$field=array(‘type‘=>‘VARCHAR‘,‘constraint‘=>255,‘default‘=>‘ ‘);break;
3. 修改 application/libraries/dili/Form.php 並且增加如下代碼:
function _image($fileld,$default){//$type= ‘type=hidden‘;//$width=($field[‘width‘] ? $field[‘width‘] : ‘570‘) .‘px;‘;//$height=($field[‘height‘] ? $field[‘height‘] : ‘415‘) .‘px;‘;return ;}
4.在content_form.php和category_content_form.php中增加判斷代碼:
<?php if($v[‘type‘]=="image"):?><div id="fileQueue"></div><input type="file" id="uploadify" name="Filedata" /><div><a href="javascript(‘#uploadify‘).uploadify(‘upload‘,‘*‘)">上傳</a><a href="javascript(‘#uploadify‘).uploadify(‘cancel‘)">取消上傳</a></div><input type="text" style="" name="<?php echo $v[‘name‘]?>" id="<?php echo $v[‘name‘]."_id"?>" value=""/><input type="hidden" style="" name="poster" id="<?php echo $v[‘name‘]."_id_1"?>" value=""/><link href="<?php echo $this->config->item("base_url");?>asset/js/uploadify/uploadify.css" type="text/css" rel="stylesheet" /><script type="text/javascript" src="<?php echo $this->config->item("base_url");?>asset/js/uploadify/jquery.uploadify-3.1.min.js"></script><script type="text/javascript">$(document).ready(function(){ $("#uploadify").uploadify({ ‘debug‘:false, ‘swf‘: ‘<?php echo $this->config->item("base_url");?>asset/js/uploadify/uploadify.swf‘, ‘uploader‘: ‘<?php echo $this->config->item("base_url");?>photo/savephoto‘, ‘method‘:"post", ‘height‘:30, ‘width‘:120, ‘queueID‘: ‘fileQueue‘, ‘auto‘: false, ‘multi‘: false, ‘fileTypeDesc‘:‘只允許上傳jpg格式圖片‘, ‘fileTypeExt‘:‘*.jpg‘, ‘fileSizeLimit‘ : ‘10MB‘, ‘formData‘ : { ‘<?php echo session_name();?>‘ : ‘<?php echo $this->session->userdata(‘session_id‘);?>‘ }, ‘onUploadError‘ : function (file,errorCode,errorMsg,errorString) { alert(‘The file ‘ + file.name + ‘ could not be uploaded: ‘ + errorString); }, ‘onUploadSuccess‘:function(file,data,response){ alert(data); $("#<?php echo $v[‘name‘]."_id"?>").val(data); $("#<?php echo $v[‘name‘]."_id_1"?>").val(data); } });})</script><?php endif;?>
6. 建立photo.php檔案:
<?phpif ( ! defined(‘BASEPATH‘)) exit(‘No direct script access allowed‘);date_default_timezone_set(‘Asia/Shanghai‘);class Photo extends Front_Controller{ public function __construct() { parent::__construct(); } function _getFilePath() { $path="attachments/".date("Y")."/"; if(!file_exists($path)){ mkdir($path,‘777‘); } $path.=date("m")."/"; if(!file_exists($path)){ mkdir($path,‘777‘); } return $path; } function _creat_photothumbs($FileName,$setW){ $IMGInfo= getimagesize($FileName); if(!$IMGInfo) return false; if($IMGInfo[‘mime‘]== "image/pjpeg" || $IMGInfo[‘mime‘]=="image/jpeg"){ $ThisPhoto= imagecreatefromjpeg($FileName); }elseif($IMGInfo[‘mime‘]== "image/x-png" || $IMGInfo[‘mime‘]== "image/png"){ $ThisPhoto= imagecreatefrompng($FileName); }elseif($IMGInfo[‘mime‘]== "image/gif"){ $ThisPhoto=imagecreatefromgif($FileName); } $width=$IMGInfo[‘0‘]; $height=$IMGInfo[‘1‘]; $scale=$height/$width; $nw=$setW; $nh=$nw*$scale; $NewPhoto=imagecreatetruecolor($nw,$nh); imagecopyresampled($NewPhoto,$ThisPhoto,0,0,0,0,$nw,$nh,$width,$height); ImageJpeg ($NewPhoto,$FileName); return true; } function _savephoto_post(){ $dest_dir =$this->_getFilePath(); if (!empty($_FILES)) { $date = date(‘Ymd‘); //$dest_dir =$this->_getFilePath(); $photoname = $_FILES["Filedata"][‘name‘]; $phototype = $_FILES[‘Filedata‘][‘type‘]; $photosize = $_FILES[‘Filedata‘][‘size‘]; $fileInfo=pathinfo($photoname); $extension=$fileInfo[‘extension‘]; $newphotoname = date("YmdHis").mt_rand(10000,99999).‘.‘.$extension; $dest=$dest_dir.$newphotoname; //move_uploaded_file($_FILES[‘Filedata‘][‘tmp_name‘], iconv("UTF-8","gb2312",$dest)); move_uploaded_file($_FILES[‘Filedata‘][‘tmp_name‘], $dest); //chmod($dest, 0755); //如果寬度大於600則要進行裁剪 $arr=getimagesize($dest); if($arr[0]>600){ $thumb_w=600; $this->_creat_photothumbs($dest,$thumb_w); } //產生縮圖 $config2[‘image_library‘] = ‘gd2‘; $config2[‘source_image‘] = $dest; $config2[‘create_thumb‘] = TRUE; $config2[‘maintain_ratio‘] = TRUE; $config2[‘width‘] = 170; $config2[‘height‘] = 150; $config2[‘master_dim‘]="width"; $config2[‘thumb_marker‘]="_1"; $this->load->library(‘image_lib‘, $config2); $this->image_lib->resize(); echo $dest; } }}?>
好了,可以測試了。
在php中使用jquery uploadify進行多圖片上傳