php无刷新上传文件的代码举例

php无刷新上传文件,用到两个文件index.html与upload.php。

1,文件:index.html
<html>
<head>
<title>无刷新上传文件_脚本学堂_www.jbxue.com</title>
<meta Content-type="text/html" charset="utf-8" />
<script type="text/javascript">
function startUpload() {
document.getElementById('processing').innerHTML = 'loding...';
return true;
}

function stopUpload(rel){
var msg;
switch (rel) {
case 0:
msg = "上传成功";
break;
case 1:
msg = "上传的文件超过限制";
break;
case 2:
msg = "只能上传图片文件";
break;
default:
msg = "上传文件失败";
}
document.getElementById('processing').innerHTML = msg;
}
</script>
</head>
<body>
<div style="text-align:center">
<div id="processing"></div>
<form action="upload.php" method="post" enctype="multipart/form-data" target="form-target" onsubmit="startUpload();">
<input type="hidden" name="MAX_FILE_SIZE" value="1000000" />
<input type="file" name="myfile" />
<input type="submit" name="sub" value="upload" />
</form>
<iframe style="width:0; height:0; border:0;" name="form-target"></iframe>
</div>
</body>
</html>


2,文件: upload.php
<?php
sleep(2);

$fileTypes = array('jpg','png','gif','bmp');
$result = null;
$uploadDir = './upfiles';
$maxSize = 1 * pow(2,20);

if ($_SERVER['REQUEST_METHOD'] == 'POST' && isset($_POST['sub'])) {
$myfile = $_FILES['myfile'];
$myfileType = substr($myfile['name'], strrpos($myfile['name'], ".") + 1);

if ($myfile['size'] > $maxSize) {
$result = 1;
} else if (!in_array($myfileType, $fileTypes)) {
$result = 2;
} elseif (is_uploaded_file($myfile['tmp_name'])) {
$toFile = $uploadDir . '/' . $myfile['name'];
if (@move_uploaded_file($myfile['tmp_name'], $toFile)) {
$result = 0;
} else {
$result = -1;
}
} else {
$result = 1;
}
}
?>
<script type="text/javascript">
window.top.window.stopUpload(<?php echo $result; ?>);
</script>

本文原始链接:[url=http://www.jbxue.com/article/1437.html]http://www.jbxue.com/article/1437.html[/url]
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值