HTML页面代码
<!DOCTYPE HTML>
<html>
<head>
<meta charset="UTF-8">
<title>demo</title>
<meta name="description" content="" />
<meta name="viewport" content="width=device-width , initial-scale=1.0 , user-scalable=0 , minimum-scale=1.0 , maximum-scale=1.0" />
<script type='text/javascript' src='/Uploads/Upload/js/jquery-2.0.3.min.js'></script>
<script type='text/javascript' src='/Uploads/Upload/js/jquery.form.js'></script>
<link href="/Uploads/Upload/css/style.css" type="text/css" rel="stylesheet"/>
</head>
<body>
<div style="width:100%;height:238;margin:10px auto; border:solid 1px #ddd; overflow:hidden; ">
<form id='myupload' action='/Uploads/Upload/upload.php' method='post' enctype='multipart/form-data'>
<input type="file" id="uploadphoto" name="uploadfile" value="请点击上传图片" style="display:none;" />
</form>
<div class="imglist"> </div>
<p class="res"></p>
<div class="progress">
<div class="progress-bar progress-bar-striped" ><span class="percent">50%</span></div>
</div>
<a href="javascript:void(0);" onclick="uploadphoto.click()" class="uploadbtn">点击上传</a>
<script type="text/javascript">
$(document).ready(function(e) {
var progress = $(".progress");
var progress_bar = $(".progress-bar");
var percent = $('.percent');
$("#uploadphoto").change(function(){
$("#myupload").ajaxSubmit({
dataType: 'json', //数据格式为json
beforeSend: function() { //开始上传
progress.show();
var percentVal = '0%';
progress_bar.width(percentVal);
percent.html(percentVal);
},
uploadProgress: function(event, position, total, percentComplete) {
var percentVal = percentComplete + '%'; //获得进度
progress_bar.width(percentVal); //上传进度条宽度变宽
percent.html(percentVal); //显示上传进度百分比
},
success: function(data) {
if(data.status == 1){
var src = data.url;
var attstr= '<img src="'+src+'" width=100%; height=218;>';
$(".imglist").append(attstr);
$(".res").html();
}else{
$(".res").html(data.content);
}
progress.hide();
},
error:function(xhr){ //上传失败
alert("上传失败");
progress.hide();
}
});
});
});
</script>
</body>
</html>PHP页面代码<?php
$picname = $_FILES['uploadfile']['name'];
$picsize = $_FILES['uploadfile']['size'];
if ($picname != "") {
if ($picsize > 10240000) { //限制上传大小
echo '{"status":0,"content":"图片大小不能超过2M"}';
exit;
}
$type = strstr($picname, '.'); //限制上传格式
if ($type != ".gif" && $type != ".jpg" && $type != "png") {
echo '{"status":2,"content":"图片格式不对!"}';
exit;
}
$rand = rand(100, 999);
$pics = uniqid() . $type; //命名图片名称
//上传路径
$pic_path = "../Upimg/". $pics;
move_uploaded_file($_FILES['uploadfile']['tmp_name'], $pic_path);
}
$size = round($picsize/1024,2); //转换成kb
echo '{"status":1,"name":"'.$picname.'","url":"'.$pic_path.'","size":"'.$size.'","content":"上传成功"}';
?>这个上传图片的代码,放在用TP开发的站里面无法上传。比如我的站点某页面地址是http://tp5.cn/index.php/home/info/123.html
用http://tp5.cn/info/123.html 也可以访问(index.php和home我隐藏了)
这个上传图片的页面实际地址http://tp5.cn/Uploads/Upload/index.html
访问http://tp5.cn/Uploads/Upload/index.html上传图片是没有问题的。
我用将这个页面引用到123.html页面后,点击上传没有反应,请教大神需要如何修改
引用代码
<?php include("/Uploads/Upload/index.html"); ?> 最佳答案