当前位置: 移动技术网 > IT编程>开发语言>PHP > PHP无刷新上传文件实现代码

PHP无刷新上传文件实现代码

2019年04月19日  | 移动技术网IT编程  | 我要评论

复制代码 代码如下:

<html>
<head>
<title>无刷新上传文件</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>

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>

如对本文有疑问, 点击进行留言回复!!

相关文章:

验证码:
移动技术网