PHP無刷新上傳文件實(shí)現(xiàn)代碼
更新時間:2011年09月19日 23:57:09 作者:
PHP無刷新上傳文件實(shí)現(xiàn)代碼,使用php的朋友可以參考下。
index.html
<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>
復(fù)制代碼 代碼如下:
<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
復(fù)制代碼 代碼如下:
<?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>
相關(guān)文章
PHP flush 函數(shù)使用注意事項(xiàng)
ob_flush/flush在手冊中的描述, 都是刷新輸出緩沖區(qū), 并且還需要配套使用, 所以會導(dǎo)致很多人迷惑…其實(shí), 他們倆的操作對象不同, 有些情況下, flush根本不做什么事情2016-08-08
PHP實(shí)現(xiàn)根據(jù)設(shè)備類型自動跳轉(zhuǎn)相應(yīng)頁面的方法
這篇文章主要介紹了PHP實(shí)現(xiàn)根據(jù)設(shè)備類型自動跳轉(zhuǎn)相應(yīng)頁面的方法,是非常實(shí)用的功能,需要的朋友可以參考下2014-07-07
PHP基于Closure類創(chuàng)建匿名函數(shù)的方法詳解
這篇文章主要介紹了PHP基于Closure類創(chuàng)建匿名函數(shù)的方法,結(jié)合實(shí)例形式詳細(xì)分析了Closure 類的功能、常用函數(shù)使用技巧及相關(guān)注意事項(xiàng),需要的朋友可以參考下2017-08-08
PHP以指定字段為索引返回?cái)?shù)據(jù)庫所取的數(shù)據(jù)數(shù)組
本文與大家分享幾個使用得PHP編程技巧,有些技巧是在看別人代碼的時候?qū)W來的,有些是自己總結(jié)的,下面為大家介紹下以特定字段為索引,返回?cái)?shù)據(jù)庫取的數(shù)據(jù)數(shù)組,感興趣的朋友可以了解下哈2013-06-06
如何取得中文字符串中出現(xiàn)次數(shù)最多的子串
以下是對取得中文字符串中出現(xiàn)次數(shù)最多的子串的實(shí)現(xiàn)代碼進(jìn)行了詳細(xì)的分析介紹,需要的朋友可以過來參考下2013-08-08
PHP使用Redis實(shí)現(xiàn)防止大并發(fā)下二次寫入的方法
這篇文章主要介紹了PHP使用Redis實(shí)現(xiàn)防止大并發(fā)下二次寫入的方法,結(jié)合實(shí)例形式分析了php使用鎖機(jī)制實(shí)現(xiàn)并發(fā)讀寫redis情況下的讀寫錯誤,需要的朋友可以參考下2017-10-10
PHP number_format函數(shù)原理及實(shí)例解析
這篇文章主要介紹了PHP number_format函數(shù)原理及實(shí)例解析,文中通過示例代碼介紹的非常詳細(xì),對大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價值,需要的朋友可以參考下2020-07-07
php 注冊時輸入信息驗(yàn)證器的實(shí)現(xiàn)詳解
本篇文章是對php中注冊時輸入信息驗(yàn)證器的實(shí)現(xiàn)方法進(jìn)行了詳細(xì)的分析介紹,需要的朋友參考下2013-07-07

