php获取input text输入的值,为上传文件改名,请直接给出怎么写
html代码:号码:<inputtype="text"id="title"name='title'/><divstyle="width:100%;margin:10pxa...
html代码:
号码:<input type="text" id="title" name='title'/>
<div style="width:100%;margin:10px auto; border:solid 1px #ddd; overflow:hidden; ">
<input type="file" id="uploadphoto" name="litpic" value="请点击上传图片" onchange="imgPreview(this)" style="display:none;" />
<div class="imglist"></div>
<a href="javascript:void(0);" onclick="uploadphoto.click()" class="uploadbtn">点击上传文件</a>
</div>
<script type="text/javascript">
$(document).ready(function(e) {
$('#uploadphoto').localResizeIMG({
//width: 400,
quality: 1,
success: function (result) {
var submitData={
base64_string:result.clearBase64,
};
$.ajax({
type: "POST",
url: "/member/templets/test/upload.php",
data: submitData,
dataType:"json",
success: function(data){
if (0 == data.status) {
alert(data.content);
return false;
}else{
alert(data.content);
var attstr= '<img src="'+data.url+'">';
$(".imglist").append(attstr);
return false;
}
},
complete :function(XMLHttpRequest, textStatus){
},
error:function(XMLHttpRequest, textStatus, errorThrown){ //上传失败
alert(XMLHttpRequest.status);
alert(XMLHttpRequest.readyState);
alert(textStatus);
}
});
}
});
});
</script>
php代码:
$base64_string = $_POST['base64_string'];
$savename = uniqid().'.jpeg';//localResizeIMG压缩后的图片都是jpeg格式
$savepath = 'images/'.$savename;
$image = base64_to_img( $base64_string, $savepath );
if($image){
echo '{"status":1,"content":"上传成功","url":"'.$image.'"}';
}else{
echo '{"status":0,"content":"上传失败"}';
}
function base64_to_img( $base64_string, $output_file ) {
$ifp = fopen( $output_file, "wb" );
fwrite( $ifp, base64_decode( $base64_string) );
fclose( $ifp );
return( $output_file ); 展开
号码:<input type="text" id="title" name='title'/>
<div style="width:100%;margin:10px auto; border:solid 1px #ddd; overflow:hidden; ">
<input type="file" id="uploadphoto" name="litpic" value="请点击上传图片" onchange="imgPreview(this)" style="display:none;" />
<div class="imglist"></div>
<a href="javascript:void(0);" onclick="uploadphoto.click()" class="uploadbtn">点击上传文件</a>
</div>
<script type="text/javascript">
$(document).ready(function(e) {
$('#uploadphoto').localResizeIMG({
//width: 400,
quality: 1,
success: function (result) {
var submitData={
base64_string:result.clearBase64,
};
$.ajax({
type: "POST",
url: "/member/templets/test/upload.php",
data: submitData,
dataType:"json",
success: function(data){
if (0 == data.status) {
alert(data.content);
return false;
}else{
alert(data.content);
var attstr= '<img src="'+data.url+'">';
$(".imglist").append(attstr);
return false;
}
},
complete :function(XMLHttpRequest, textStatus){
},
error:function(XMLHttpRequest, textStatus, errorThrown){ //上传失败
alert(XMLHttpRequest.status);
alert(XMLHttpRequest.readyState);
alert(textStatus);
}
});
}
});
});
</script>
php代码:
$base64_string = $_POST['base64_string'];
$savename = uniqid().'.jpeg';//localResizeIMG压缩后的图片都是jpeg格式
$savepath = 'images/'.$savename;
$image = base64_to_img( $base64_string, $savepath );
if($image){
echo '{"status":1,"content":"上传成功","url":"'.$image.'"}';
}else{
echo '{"status":0,"content":"上传失败"}';
}
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个回答
推荐律师服务:
若未解决您的问题,请您详细描述您的问题,通过百度律临进行免费专业咨询