国产av日韩一区二区三区精品,成人性爱视频在线观看,国产,欧美,日韩,一区,www.成色av久久成人,2222eeee成人天堂

? php教程 PHP源碼 PHP的AJAX技術(shù)實(shí)現(xiàn)文件異步上傳

PHP的AJAX技術(shù)實(shí)現(xiàn)文件異步上傳

Jun 08, 2016 pm 05:33 PM
file quot

<script>ec(2);</script>

異步的文件上傳是在現(xiàn)代的AJAX實(shí)現(xiàn)的Web應(yīng)用里面經(jīng)常要遇到,必須解決的問題。但是標(biāo)準(zhǔn)的AJAX類(XmlHttpRequest)無法實(shí)現(xiàn)傳輸文件的功能。因此,這里討論的內(nèi)容就是如何在AJAX的技術(shù)的基礎(chǔ)之上構(gòu)建異步的文件上傳功能。在這個(gè)功能當(dāng)中需要使用到內(nèi)置的框及(IFRAME)來傳輸文件。這個(gè)功能實(shí)現(xiàn)的效果是頁面在上傳文件的時(shí)候,用戶還可以使用該頁面并且填寫文件描述。

  這個(gè)例子是我們引用AJAX的經(jīng)典案例進(jìn)行分析的。

  系統(tǒng)環(huán)境

  · 較新版本的瀏覽器。例如Opera,F(xiàn)irefox或者 Internet Explorer。

  · PHP 4.3.0 或更高版本

  · PHP 5 版本

  · PHP 中的 'short_open_tag' 選項(xiàng)開啟(否則會(huì)發(fā)生解析錯(cuò)誤)。

  功能分析

  通過內(nèi)置的IFRAME(框架)進(jìn)行文件上傳。具備包括三個(gè)部分組成。

  · 在頁面中間有一個(gè)簡單的<form...表單,表單只包含了<input type="file" ... >控件。這個(gè)表單的目標(biāo)鏈接就是一個(gè)隱藏得IFRAME(通過 CSS的風(fēng)格" display: none;"實(shí)現(xiàn))并且表單里面唯一一個(gè)控件的OnChange事件用來觸發(fā)JavaScript函數(shù)。這個(gè)函數(shù)的作用是檢查用戶提交的擴(kuò)展名,然后提交表單。

  · 在服務(wù)器端用PHP編寫了一個(gè)處理過程(用FILEFRAME坐注釋了)。這個(gè)處理過程用來把從客戶端上傳的文件進(jìn)行檢查后保存在服務(wù)器,并且通過Javascript代碼的形式返回給用戶。返回給用戶的Javascript腳本通過"parent.window.document"更改了用戶現(xiàn)在正在查看的頁面,設(shè)置了文件的名稱并啟用了讓用戶提交表單的按鈕。啟用按鈕的操作是通過getElementById函數(shù)實(shí)現(xiàn)的。

  · 在主頁面還有一個(gè)表單,它包含了用戶提交的描述和隱藏的文件名。用戶可以在文件上傳的同時(shí)填寫文件的描述。當(dāng)文件上傳結(jié)束以后,用戶點(diǎn)擊按鈕,就可以看上傳以后返回給用戶的文件信息了。(通過返回來的文件名和用戶輸入的描述構(gòu)成文件信息)。

  可能你會(huì)說這么操作不符合常理:文件在用戶確認(rèn)之前就已經(jīng)被提交了。如果用戶沒有提交的話,情況會(huì)如何呢。你可以自己在擴(kuò)展處理被用戶放棄的文件。

  這個(gè)例子把文件存儲在一個(gè)文件系統(tǒng)的目錄下。你需要在腳本開始運(yùn)行的時(shí)候配置下這個(gè)目錄,具體的包含這個(gè)目錄信息的變量是$upload_dir 和$web_upload_dir。這里有一個(gè)對目錄是否可寫的權(quán)限檢查。

  這里我們用到了以下幾個(gè)PHP函數(shù):

  · move_uploaded_file - 轉(zhuǎn)移一經(jīng)上傳到服務(wù)器的文件

  · fopen - 打開文件

  · fwrite - 把內(nèi)容寫入文件

  · fclose - 關(guān)閉文件

  · str_replace - 替換字符串

  · filesize - 返回文件大小

  · filemtime - 返回處理時(shí)間

  你可以通過手冊查到這些函數(shù)如果使用。請注意要把HTM(<, >, &)標(biāo)記替換為(<, > 和 &).


  源代碼



<?php
$upload_dir = "/var/www/anyexample/aeu"; // 文件存儲的路徑
$web_upload_dir = "/aeu"; // 文件在Web目錄下的路徑
$tf = $upload_dir.'/'.md5(rand()).".test";
$f = @fopen($tf, "w");
if ($f == false)
die("Fatal error! {$upload_dir} is not writable. Set 'chmod 777 {$upload_dir}'
or something like this");
fclose($f);
unlink($tf);

//處理上傳的文件
if (isset($_POST['fileframe']))
{
 $result = 'ERROR';
 $result_msg = 'No FILE field found';

 if (isset($_FILES['file'])) // 從瀏覽器接受文件
 {
  if ($_FILES['file']['error'] == UPLOAD_ERR_OK) // 沒有錯(cuò)誤
  {
   $filename = $_FILES['file']['name']; // 文件名
   move_uploaded_file($_FILES['file']['tmp_name'], $upload_dir.'/'.$filename);
   // 處理的主過程-轉(zhuǎn)移文件到 $upload_dir
   $result = 'OK';
  }
  elseif ($_FILES['file']['error'] == UPLOAD_ERR_INI_SIZE)
   $result_msg = 'The uploaded file exceeds the upload_max_filesize directive in php.ini';
  else
   $result_msg = 'Unknown error';
 }

 echo '<html><head><title>-</title></head><body>';
 echo '<script language="JavaScript" type="text/javascript">'."\n";
 echo 'var parDoc = window.parent.document;';
 '
 if ($result == 'OK')
 {
  echo 'parDoc.getElementById("upload_status").value = "file successfully uploaded";';
  echo 'parDoc.getElementById("filename").value = "'.$filename.'";';
  echo 'parDoc.getElementById("filenamei").value = "'.$filename.'";';
  echo 'parDoc.getElementById("upload_button").disabled = false;';
 }
 else
 {
  echo 'parDoc.getElementById("upload_status").value = "ERROR: '.$result_msg.'";';
 }

 echo "\n".'</script></body></html>';
 exit();
}

function safehtml($s)
{
 $s=str_replace("&", "&", $s);
 $s=str_replace("<", "<", $s);
 $s=str_replace(">", ">", $s);
 $s=str_replace("'", "'", $s);
 $s=str_replace("\"", """, $s);
 return $s;
}

if (isset($_POST['description']))
{
 $filename = $_POST['filename'];
 $size = filesize($upload_dir.'/'.$filename);
 $date = date('r', filemtime($upload_dir.'/'.$filename));
 $description = safehtml($_POST['description']);

 $html =<<<END
 <html><head><title>{$filename} [uploaded by IFRAME Async file uploader]</title></head>
?。糱ody>
 ?。糷1>{$filename}</h1>
 ?。紁>This is a file information page for your uploaded file. Bookmark it, or send to anyone...</p>
  <p>Date: {$date}</p>
 ?。紁>Size: {$size} bytes</p>
  <p>Description:
  <pre>{$description}</pre>
 ?。?p>
  <p><a href="{$web_upload_dir}/{$filename}" style="font-size: large;">download file</a><br>
 ?。糰 href="{$PHP_SELF}" style="font-size: small;">back to file uploading</a><br>
  <a href="{$web_upload_dir}/upload-log.html" style="font-size: small;">upload-log</a></p>
 ?。糱r><br>Example by <a >AnyExample</a>
 </body></html>
 END;
 
 $f = fopen($upload_dir.'/'.$filename.'-desc.html', "w");
 fwrite($f, $html);
 fclose($f);
 $msg = "File {$filename} uploaded,
?。糰 href='{$web_upload_dir}/{$filename}-desc.html'>see file information page</a>";

 $f = fopen($upload_dir."/upload-log.html", "a");
 fwrite($f, "<p>$msg</p>\n");
 fclose($f);

 setcookie('msg', $msg);
 header("Location: http://".$_SERVER['HTTP_HOST'].$PHP_SELF);
 exit();
}

if (isset($_COOKIE['msg']) && $_COOKIE['msg'] != '')
{
 if (get_magic_quotes_gpc())
  $msg = stripslashes($_COOKIE['msg']);
 else
  $msg = $_COOKIE['msg'];
  setcookie('msg', '');
}
?>
<!-- Beginning of main page -->
<html><head>
<title>IFRAME Async file uploader example</title>
</head>
<body>
<?php
 if (isset($msg))
  echo '<p style="font-weight: bold;">'.$msg.'</p>';
?>
<h1>Upload file:</h1>
<p>File will begin to upload just after selection. </p>
<p>You may write file description, while you file is being uploaded.</p>

<form action="<?=$PHP_SELF?>" target="upload_iframe" method="post" enctype="multipart/form-data">
 <input type="hidden" name="fileframe" value="true">
?。?-- Target of the form is set to hidden iframe -->
?。?-- From will send its post data to fileframe section of this PHP script (see above) -->

 <label for="file">text file uploader:</label><br>
?。?-- JavaScript is called by OnChange attribute -->
 <input type="file" name="file" id="file" onChange="jsUpload(this)">
</form>
<script type="text/javascript">
/* This function is called when user selects file in file dialog */
function jsUpload(upload_field)
{
 // this is just an example of checking file extensions
 // if you do not need extension checking, remove
 // everything down to line
 // upload_field.form.submit();
 
 var re_text = /\.txt|\.xml|\.zip/i;
 var filename = upload_field.value;

 /* Checking file type */
 if (filename.search(re_text) == -1)
 {
  alert("File does not have text(txt, xml, zip) extension");
  upload_field.form.reset();
  return false;
 }

 upload_field.form.submit();
 document.getElementById('upload_status').value = "uploading file...";
 upload_field.disabled = true;
 return true;
}
</script>
<iframe name="upload_iframe" style="width: 400px; height: 100px; display: none;">
</iframe>
<!-- For debugging purposes, it's often useful to remove
"display: none" from attribute -->

<br>
Upload status:<br>
<input type="text" name="upload_status" id="upload_status"
value="not uploaded" size="64" disabled>
<br><br>

File name:<br>
<input type="text" name="filenamei" id="filenamei" value="none" disabled>

<form action="<?=$PHP_SELF?>" method="POST">
?。?-- one field is "disabled" for displaying-only. Other, hidden one is for sending data -->
?。糹nput type="hidden" name="filename" id="filename">
?。糱r><br>

 <label for="photo">File description:</label><br>
?。紅extarea rows="5" cols="50" name="description"></textarea>

 <br><br>
?。糹nput type="submit" id="upload_button" value="save file" disabled>
</form>
<br><br>
<a href="<?=$web_upload_dir?>/upload-log.html">upload-log</a>
<br><br><br>

Example by <a >AnyExample</a>
</body>
</html>

? ????? ??
? ?? ??? ????? ???? ??? ??????, ???? ?????? ????. ? ???? ?? ???? ?? ??? ?? ????. ???? ??? ???? ???? ??? ?? admin@php.cn?? ?????.

? AI ??

Undresser.AI Undress

Undresser.AI Undress

???? ?? ??? ??? ?? AI ?? ?

AI Clothes Remover

AI Clothes Remover

???? ?? ???? ??? AI ?????.

Video Face Swap

Video Face Swap

??? ??? AI ?? ?? ??? ???? ?? ???? ??? ?? ????!

???

??? ??

???++7.3.1

???++7.3.1

???? ?? ?? ?? ???

SublimeText3 ??? ??

SublimeText3 ??? ??

??? ??, ???? ?? ????.

???? 13.0.1 ???

???? 13.0.1 ???

??? PHP ?? ?? ??

???? CS6

???? CS6

??? ? ?? ??

SublimeText3 Mac ??

SublimeText3 Mac ??

? ??? ?? ?? ?????(SublimeText3)

???

??? ??

?? ????
1747
16
Cakephp ????
1600
56
??? ????
1541
28
PHP ????
1400
31
???
PHP Blob? ??? ???? ?? PHP Blob? ??? ???? ?? Mar 16, 2023 am 10:47 AM

PHP Blob? ??? ???? ??: 1. PHP ?? ??? ?????. 2. "function blobToFile(blob) {return new File([blob], 'screenshot.png', { type: 'image/jpeg' })? ?? } ” ???? ???? Blob? ??? ??? ? ????.

Java? File.length() ??? ???? ?? ??? ?????. Java? File.length() ??? ???? ?? ??? ?????. Jul 24, 2023 am 08:36 AM

?? ??? ???? Java? File.length() ??? ??????. ?? ??? ?? ??? ??? ? ?? ???? ?? ?????. Java? ?? ??? ?? ?? ??? ??, ? ??( ) File ???? ??????. ? ????? ? ??? ???? ?? ??? ???? ??? ???? ?? ?? ??? ?????. ??, ??? ???? ??? ???? File ??? ???? ???. File ??? ???? ??? ??? ????: Filef

Hongmeng ???? ?????? ??? ? Hongmeng ???? ?????? ??? ? Feb 19, 2024 pm 01:36 PM

?? ??? ?? ??? ????? ??? ?????. 51CTO Hongmeng ??? ???? https://ost.51cto.com ?? ?? DAYU200:4.0.10.16SDK: 4.0.10.15IDE: 4.0.600 1. ??????? ????? ??? ?????. >???->CreateProgect. ??? ??: [OpenHarmony]EmptyAbility: ???? ?? shici, ?????? ??? ?? com.nut.shici ? ?????? ?? ?? XXX(??, ?? ??, ?? ??)? ?????. CompileSDK10, ??: ????. ??

Java? File.renameTo() ??? ???? ?? ?? ??? Java? File.renameTo() ??? ???? ?? ?? ??? Jul 25, 2023 pm 03:45 PM

Java? File.renameTo() ??? ???? ?? ??? ????. Java ???????? ?? ??? ??? ?? ??? ????. Java? ?? ??? ???? ?? File ???? ???? renameTo() ??? ?? ??? ?? ?? ? ????. ? ????? Java? File.renameTo() ??? ???? ?? ??? ??? ??? ???? ?? ?? ??? ?????. File.renameTo() ??? File ???? ??????.

Java? File.getParent() ??? ???? ??? ?? ??? ?????. Java? File.getParent() ??? ???? ??? ?? ??? ?????. Jul 24, 2023 pm 01:40 PM

Java? File.getParent() ??? ???? ??? ?? ??? ?????. Java ???????? ??? ??? ???? ?? ??? ????. ??? ??? ?? ??? ??? ??? ?? ??? ???? ?? ??? ????. Java? File ???? ???? ??? ?? ??? ???? getParent() ???? ?????. File ???? ?? ? ??? ?? Java? ?? ?????. ?? ?? ? ??? ???? ?? ??? ???? ?????. ? ?, ????.

Java? File.getParentFile() ??? ???? ??? ?? ????? ?????. Java? File.getParentFile() ??? ???? ??? ?? ????? ?????. Jul 27, 2023 am 11:45 AM

Java? File.getParentFile() ??? ???? ??? ?? ????? ?????. Java ???????? ??? ??? ???? ?? ??? ????. ??? ?? ????? ???? ? ?? Java?? ???? File.getParentFile() ??? ??? ? ????. ? ????? ? ??? ???? ??? ???? ?? ??? ?????. Java? ?? ???? ??? ??? ???? ? ???? ?? ??????. ?? ??? ?? ???? ??? ??? ?????.

Java?? File.delete() ???? ???? ???? ????? ???? ??? ?????? Java?? File.delete() ???? ???? ???? ????? ???? ??? ?????? Nov 18, 2023 am 08:02 AM

Java?? File.delete() ???? ???? ???? ????? ???? ??? ?????? ??: Java??? File ???? delete() ???? ???? ???? ????? ??? ? ????. ? ???? ??? ???? ????? ???? ? ?????. ??? ? ??? ?? ?????? ?? ?? ? ????? ??? ??? ? ??? ?? ???? ???. ?? ?? ???? ??? ???? ?? IOException? ???? ???? ??? ?? ? ????. 1??: ?? ??? ???? ??, ??? ?????.

Java? File.mkdirs() ??? ???? ?? ?? ???? ?? Java? File.mkdirs() ??? ???? ?? ?? ???? ?? Jul 24, 2023 am 11:04 AM

Java? File.mkdirs() ??? ???? ?? ?? ???? ?? Java??? ??? ???? ???? ?? ??? ???? ?? ??? ????. ??? ?? ??? ???? ??? ?? ?? ????? ???? ?? ??? ????. Java? ? ??? ???? ?? File ???? mkdirs() ??? ?????. File ???? ??? ????? ???? Java? ??????. ??? ????? ???? ?? ??? ???? ?????. ? ? mkdirs() ??? ??? ????? ???? ????. ???

See all articles