uploadify 上传插件的使用(视频和图片都可以上传)

第一步:引入jquery.js

第二步:引入jquery.uploadify.js

第三步:引入uploadify.css

第四步:在html加标签<input id="upload-gooods-video" type="file" name="goods_video">

第五步:添加js代码(到了这一步已经把视频传到了页面上了)

<script type="text/javascript">

$(function(){

//将upload-goods-video 替换成一个上传插件

$("#upload-goods-video").uploadify({

"height":30,

"width":120,  //定义上传插件的宽和高

"swf":"<?php echo RESOURCE_SITE_URL;?>/js/uploadify/uploadify.swf",  //引入上传插件的flash

"fileObjName":"goods_video",   //以什么名字上传到服务器上.. 保存到$_FILES. 默认的名字为 'Filedata'

"buttonText":"上传视频",

"uploader":"<?php echo SHOP_SITE_URL; ?>/index.php?act=store_goods_add&op=video_upload", //上传给哪个url地址

'removeTimeout'

: 3,  //上传后的提示在几秒后消失

'multi': false,//是否执行多文件上传

'debug': false, //是否开启调试模式

'formData': {name:'goods_video',pic_url:''}, //上传图片的同时以POST的形式向服务器参数,  指定上传到又拍云的哪个空间

'fileTypeExts'

: '*.jpg; *.png; *.gif; *.bmp; *.mp4;',  //指定上传的类型,  它可以帮我们验证..

'onUploadSuccess' : function(file, data, response) {  //上传成功之后执行该方法

var obj = $.parseJSON(data);

// console.log(data);

$("#video_path").val(obj.name);

//显示预览图片的div

// $('.upload-pre-item img').attr('src','__BRAND__/'+obj.path+'!s'); //将上传后的路径设置给img的src中

// $('.upload-img-box').show();

},

'onFallback' : function() {

alert('未检测到兼容版本的Flash.');

}

});

});

</script>

第六步:php功能代码(到了这一步已经把文件保存到文件或者upyun上面了)

//上传视频

    public function video_uploadOp(){

        $uploader = new UploadFile();

        $uploader->set('default_dir', ATTACH_GOODS . DS . $_SESSION ['store_id'] . DS . $uploader->getSysSetPath());

        $uploader->set('max_size', '10240');

        $uploader->set('fprefix', $_SESSION['store_id']);

        $uploader->set('allow_type', array('gif', 'jpg', 'jpeg', 'png', 'mp4'));

        $result = $uploader->upload($_POST['name'],C('target'));

        if (!$result) {

            if (strtoupper(CHARSET) == 'GBK') {

                $uploader->error = Language::getUTF8($uploader->error);

            }

            $output = array();

            $output['error'] = $uploader->error;

            $output = json_encode($output);

            exit($output);

        }

        $video_name = $uploader->getSysSetPath() . $uploader->file_name;

        $data = array ();

        $data ['name'] = $video_name;

//        if(C('target')=='upyun'){

//            $data ['thumb_name']=UPYUN_URL.$uploader->img_url.'!240';

//        }else{

//            $data ['thumb_name'] = cthumb($uploader->getSysSetPath() . $uploader->thumb_image, 240, $_SESSION['store_id']);

//        }

        // 整理为json格式

        $output = json_encode($data);

        echo $output;

        exit();

    }

第七步:写的底层代码

/**

* 上传视频操作

*

* @param string $field

* @return bool

*/

public function upload($field,$target=''){

//上传文件

$this->upload_file = $_FILES[$field];

if ($this->upload_file['tmp_name'] == ""){

$this->setError(Language::get('cant_find_temporary_files'));

return false;

}

//对上传文件错误码进行验证

$error = $this->fileInputError();

if (!$error){

return false;

}

//验证是否是合法的上传文件

if(!is_uploaded_file($this->upload_file['tmp_name'])){

$this->setError(Language::get('upload_file_attack'));

return false;

}

//验证文件大小

if ($this->upload_file['size']==0){

$error = Language::get('upload_file_size_none');

$this->setError($error);

return false;

}

if($this->upload_file['size'] > $this->max_size*1024){

$error = Language::get('upload_file_size_cant_over').$this->max_size.'KB';

$this->setError($error);

return false;

}

//文件后缀名

$tmp_ext = explode(".", $this->upload_file['name']);

$tmp_ext = $tmp_ext[count($tmp_ext) - 1];

$this->ext = strtolower($tmp_ext);

//验证文件格式是否为系统允许

if(!in_array($this->ext,$this->allow_type)){

$error = Language::get('image_allow_ext_is').implode(',',$this->allow_type);

$this->setError($error);

return false;

}

//设置路径

$this->save_path = $this->setPath();

//设置文件名称

if(empty($this->file_name)){

$this->setFileName();

}

//是否立即弹出错误

if($this->if_show_error){

echo "<script type='text/javascript'>alert('". ($this->if_show_error_one ? $error : $this->error) ."');history.back();</script>";

die();

}

if ($this->error != '') return false;

//上传本地或者upyun

// if($target=='upyun'){

// require_once(BASE_DATA_PATH.'/resource/upyun/upyun.class.php');

// $upyun=new Upyun(C('bucket'),C('user_name'),C('pwd'));

// $opts = array(

// UpYun::X_GMKERL_THUMBNAIL => 'thumbtype'

// );

// $this->img_url=DS.$this->save_path.DS.$this->file_name;

// $fh = fopen($this->upload_file['tmp_name'], 'rb');

// $result = $upyun->writeFile($this->img_url,$fh, True, $opts);   // 上传图片,自动创建目录

// fclose($fh);

//            $this->assertTrue(is_array($result));

// return 1;

// }else{

if(@move_uploaded_file($this->upload_file['tmp_name'],BASE_UPLOAD_PATH.DS.$this->save_path.DS.$this->file_name)){

//删除原图

if ($this->ifremove && is_file(BASE_UPLOAD_PATH.DS.$this->save_path.DS.$this->file_name)) {

@unlink(BASE_UPLOAD_PATH.DS.$this->save_path.DS.$this->file_name);

}

return true;

}else {

$this->setError(Language::get('upload_file_fail'));

return false;

}

// }

// $this->setErrorFileName($this->upload_file['tmp_name']);

return $this->error;

}