熱點推薦:
您现在的位置: 電腦知識網 >> 編程 >> PHP編程 >> 正文

PHP無刷新上傳文件實現代碼

2022-06-13   來源: PHP編程 

  indexhtml

   <html>
<head>
<title>無刷新上傳文件</title>
<meta Contenttype="text/html" charset="utf" />
<script type="text/javascript">
function startUpload() {
documentgetElementById(processing)innerHTML = loding;
return true;
}
function stopUpload(rel){
var msg;
switch (rel) {
case :
msg = "上傳成功";
break;
case :
msg = "上傳的文件超過限制";
break;
case :
msg = "只能上傳圖片文件";
break;
default:
msg = "上傳文件失敗";
}
documentgetElementById(processing)innerHTML = msg;
}
</script>
</head>
<body>
<div style="textalign:center">
<div id="processing"></div>
<form action="uploadphp" method="post" enctype="multipart/formdata" target="formtarget" onsubmit="startUpload();">
<input type="hidden" name="MAX_FILE_SIZE" value="" />
<input type="file" name="myfile" />
<input type="submit" name="sub" value="upload" />
</form>
<iframe style="width:; height:; border:;" name="formtarget"></iframe>
</div>
</body>
</html> uploadphp

   <?php
sleep();
$fileTypes = array(jpgpnggifbmp);
$result = null;
$uploadDir = /upfiles;
$maxSize = * pow();
if ($_SERVER[REQUEST_METHOD] == POST && isset($_POST[sub])) {
$myfile = $_FILES[myfile];
$myfileType = substr($myfile[name] strrpos($myfile[name] "") + );
if ($myfile[size] > $maxSize) {
$result = ;
} else if (!in_array($myfileType $fileTypes)) {
$result = ;
} elseif (is_uploaded_file($myfile[tmp_name])) {
$toFile = $uploadDir / $myfile[name];
if (@move_uploaded_file($myfile[tmp_name] $toFile)) {
$result = ;
} else {
$result = ;
}
} else {
$result = ;
}
}
?>
<script type="text/javascript">
windowtopwindowstopUpload(<?php echo $result; ?>);


From:http://tw.wingwit.com/Article/program/PHP/201311/20855.html
    推薦文章
    Copyright © 2005-2022 電腦知識網 Computer Knowledge   All rights reserved.