HTML 代码
<!-- 图片上传CSS --->
<link rel="stylesheet" href="__PUBLIC__/admin/uploadify/uploadify.css">
<label>封面图片<b>★</b></label><br />
<input id="file_upload" name="file_upload" type="file" multiple="true" />
<img src="" id="upload_org_code_img" style="width: 180px; height: 100px; display: none;" />
<!-- jQuery 2.2.3 -->
<script src="__PUBLIC__/admin/plugins/jQuery/jquery-2.2.3.min.js"></script>
<!-- 图片上传插件 --->
<script src="__PUBLIC__/admin/uploadify/uploadify.js"></script>
JQUERY 代码
<script>
var SCOPE = {
'upload_image_url' : "{:U('Image/imageUpload')}",
'upload_swf' : "__ROOT__/Public/admin/uploadify/uploadify.swf",
};
$(function() {
$('#file_upload').uploadify({
'swf' : SCOPE.upload_swf,
'uploader' : SCOPE.upload_image_url,
'buttonText': '上传图片',
// 允许上传的图片格式
'fileTypeExts': '*.gif; *.jpg; *.png; *.jpeg',
'onUploadSuccess' : function(file,data,response) {
var data = JSON.parse(data);
if(data.flag){
alert(data.flag);
return false;
}else{
$("#upload_org_code_img").attr("src","__ROOT__/"+data['url']);
$("#upload_org_code_img").show();
$('.left_img').attr('src',"__ROOT__/"+data['url']);
$(".left_img").show();
}
}
});
});
</script>
PHP 代码
// 图片上传
public function imageUpload(){
$upload = new \Think\Upload(); // 实例化上传类
$upload->maxSize = 1024*1024*2; // 设置附件上传大小 2M
$upload->exts = array('jpg', 'gif', 'png', 'jpeg');// 设置附件上传类型
$upload->rootPath = './upload/'; // 设置附件上传根目录
$upload->savePath = ''; // 设置附件上传(子)目录
$upload->subName = array('date','Ymd'); // 子目录命名形式
// 上传文件
$info = $upload->upload();
if($info){
// 文件上传成功后的名称
$fileName = $info['Filedata']['savename'];
// 文件上传成功后的路径
$fileUrl = 'upload/'.$info['Filedata']['savepath'].$fileName;
echo json_encode(array('url'=>$fileUrl));
exit;
}else{
$error = $upload->getError();
echo json_encode(array('flag'=>$error));
exit;
}
uploadify文件包下载:https://pan.baidu.com/s/1i4KDi6L