1.推荐上传
需要的js和含标签名称的页面的资源
处理的代码
$base64_string = $_POST['base64_string'];
$side = $_POST['side'];
$savename = uniqid() . '.jpeg';//localResizeIMG压缩后的图片都是jpeg格式
$month=date("n", time());
$year =date("y", time());
$savepath = ROOT_PATH.'data/attachment/'.$year.'/'.$month.'/' ;
$savepath_url = 'data/attachment/'.$year.'/'.$month.'/' ;
if (! is_dir ( $savepath )) {
mkdir ( $savepath, 0777, true );
}
$savepath = $savepath . $savename;
$savepath_url = $savepath_url . $savename; ;
$image = $this->base64_to_img($base64_string, $savepath);
if ($image){
echo json_encode(array(
'status' => 1,
'url'=> $savepath_url,
'side'=>$side
));
}
public function base64_to_img( $base64_string, $output_file ) {
$ifp = fopen( $output_file, "wb" );
fwrite( $ifp, base64_decode( $base64_string) );
fclose( $ifp );
return( $output_file );
}
2.原始上传(不推荐)
$image = $_FILES['wx_receive_code'];
if ($image['error'] > 0) {
$error = "上传失败了,原因是";
switch ($image['error']) {
case 1:
$error .= "大小超过了服务器设置的限制!";
break;
case 2:
$error .= "文件大小超过了表单的限制!";
break;
case 3:
$error .= "文件只有部分被上传!";
break;
case 4:
$error .= "没有文件被上传!";
break;
case 6:
$error .= "上传文件的临时目录不存在!";
break;
case 7:
$error .= "写入失败!";
break;
default:
$error .= "未知的错误!";
break;
}
//输出错误
exit($error);
}
else {
//截取文件后缀名
$type = strrchr($image['name'], ".");
$path = ROOT_PATH.'data/attachment/'. $image['name'];
//判断上传的文件是否为图片格式
if (strtolower($type) == '.png' || strtolower($type) == '.jpg' || strtolower($type) == '.bmp' || strtolower($type) == '.gif') {
dump($image['tmp_name']);
//将图片文件移到该目录下
move_uploaded_file($image['tmp_name'], $path);
}
}
3.插件上传(不推荐), 所需资源
<th>缩略图:</th>
<td>
<input type="text" size="50" name="art_thumb">
<input id="file_upload" name="file_upload" type="file" multiple="true">
<script src="{{asset('resources/org/uploadify/jquery.uploadify.min.js')}}" type="text/javascript"></script>
<link rel="stylesheet" type="text/css" href="{{asset('resources/org/uploadify/uploadify.css')}}">
<script type="text/javascript">
<?php $timestamp = time();?>
$(function() {
$('#file_upload').uploadify({
'buttonText' : '图片上传',
'formData' : {
'timestamp' : '<?php echo $timestamp;?>',
'_token' : "{{csrf_token()}}"
},
'swf' : "{{asset('resources/org/uploadify/uploadify.swf')}}",
'uploader' : "{{url('admin/upload')}}",
'onUploadSuccess' : function(file, data, response) {
$('input[name=art_thumb]').val(data);
$('#art_thumb_img').attr('src','/'+data);
// alert(data);
}
});
});
</script>
<style>
.uploadify{display:inline-block;}
.uploadify-button{border:none; border-radius:5px; margin-top:8px;}
table.add_tab tr td span.uploadify-button-text{color: #FFF; margin:0;}
</style>
</td>
</tr>
<tr>
<th></th>
<td>
<img src="" alt="" id="art_thumb_img" style="max-width: 350px; max-height:100px;">
</td>
</tr>
<tr>
//图片上传
public function upload()
{
$file = Input::file('Filedata');
if($file -> isValid()){
$entension = $file -> getClientOriginalExtension(); //上传文件的后缀.
$newName = date('YmdHis').mt_rand(100,999).'.'.$entension;
$path = $file -> move(base_path().'/uploads',$newName);
$filepath = 'uploads/'.$newName;
return $filepath;
}
}