一個PHP無重新整理上傳檔案程式碼

來源:互聯網
上載者:User

一個最原始最簡單的iframe上傳例子:

前台上傳頁面index.html,主要是一個表單與一個js回呼函數。上傳檔案時,form表單的method、 enctype屬性必須和下面代碼一樣。然後將target的值設為iframe的name,這樣就可以實現無重新整理上傳檔案。

 代碼如下 複製代碼

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd"> 
<html xmlns="http://www.w3.org/1999/xhtml"> 
<head> 
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" /> 
<title>上傳檔案</title> 
</head> 
 
<body> 
<script> 
function CallbackFunction(str){ 
alert("上傳成功"); 

</script> 
<form action="uploadfile.php" enctype="multipart/form-data" method="post" target="iframeUpload"> 
 <iframe name="iframeUpload" src="" width="350" height="35" frameborder=0  SCROLLING="no" style="display:NONE"></iframe> 
 <input id="test_file" name="test_file" type="file"> 
 <input value="上傳檔案" type="submit"> 
</form> 
</body> 
</html>

後台上傳處理頁面uploadfile.php,這段代碼為簡單的上傳代碼,沒有進行錯誤及異常處理。上傳代碼執行完成後,要告訴父頁面已經上傳完畢了,因此,在這個頁面裡調用父頁面的回呼函數CallbackFunction,這個函數可以有參數,形式自己定義。

 代碼如下 複製代碼

<?php 
set_time_limit(0); 
if($_SERVER['REQUEST_METHOD']=='POST') { 
  move_uploaded_file($_FILES["test_file"]["tmp_name"], 
  dirname($_SERVER['SCRIPT_FILENAME'])."/UploadTemp/" . $_FILES["test_file"]["name"]); 
  echo "<script>window.parent.CallbackFunction();</script>"; 

?>

以上是用iframe實現無重新整理上傳檔案的簡單方法,想要健壯的程式的話需要再細化

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.