HTML 本身并不能直接上傳文件到服務(wù)器(′?ω?`),何上它只是傳服一種用于創(chuàng )建網(wǎng)頁(yè)的標記語(yǔ)言,( ???)我??們可以通過(guò) HTML 表(biao)單和 JavaScript 來(lái)實(shí)現用戶(hù)選擇文件并提交到服務(wù)器的何上功能,以下是傳服一個(gè)簡(jiǎn)單的示例:
(圖片來(lái)源網(wǎng)絡(luò ),侵刪)1、何上我們需要創(chuàng )建一個(gè) HTML 表單,傳服包含一個(gè)文件輸入框和一個(gè)提交按鈕,何上用戶(hù)可以在這個(gè)輸入框中選擇要上傳的傳服文件,然后點(diǎn)擊提交??按鈕將文件發(fā)送到服務(wù)器。何上
<!DOCTYPE html><html lang="en"><head> <meta charset="UTF8"> <meta name=&quo??t;viewport" content="??width=(◎_◎;)devicewidth,傳服 initialscale=1.0"> <title>文件上傳</title></head><body> <form action="upload.p(′_ゝ`)hp" method="(′▽?zhuān)?p( ?ヮ?)ost" enctype="multipart??/formdata"> <input type="file" name="fileToUpload" id="fileToUpload"> <input type="submit" value="上傳文件" name="submit"> </form></body></ht??ml>
2、在上面的何上代碼中,acti(???)on 屬性指定了表單提交的傳服目標 URL(在這個(gè)例子中是 "upload.php"),method 屬性指定了表??單數據的何上傳輸方式(在這個(gè)例子中是 "post")。enctype 屬性指定了在發(fā)送表單數??據時(shí)要使用的傳服編碼類(lèi)型(在這個(gè)例子中是 "multipart/formdata",這是何上因為我們要上傳文件)。
3、input 標簽中的 type 屬性設置為 &q??uot;file",表示這是一個(gè)文件輸入框。name 屬性指定??了在服務(wù)器端處理表單數據時(shí)使用的名稱(chēng)(在這個(gè)例子中是 "fileToUpload")。id 屬性為這個(gè)輸入框設置了一個(gè)唯一的標識符(在這個(gè)(′?`*)例子中是 "fileToUpload")。
4、另一個(gè) input 標簽中的 type 屬性設置為 "submit",表示這是一個(gè)提交按鈕。value 屬性指定了按鈕上顯示的文本(在這個(gè)例子中是 "上(?_?;)傳文件")。nam??e 屬性指定了在服務(wù)器端處理表單數據時(shí)使用的(de)名稱(chēng)(在這個(gè)例子中是 "submit")。
5、現(╯°□°)╯在,我們需要創(chuàng )建一個(gè) PHP 腳本來(lái)處理用戶(hù)上傳的文件,在這個(gè)例子中,我??們將創(chuàng )建一個(gè)名為 "upload.php" 的腳本,以下是一個(gè)簡(jiǎn)單的 PHP 腳本示例:
<?php$target_dir = "(?⊿?);uploads/"??;$target_file = $target_dir . basename($_FILES["fileToUpload"]["name&qu??ot;]);$uploadOk = 1;$imageFileType = strtolower(pathinfo($target_file??,PATHINFO_EXTENSION));// Check if image file is a actual image or fake imageif(isset($_POST["submit"])) { $check = getimagesize($_FILES["fileToUpload"]["tmp_name"]); if($check !== false(′?`)) { echo "File is an image " . $check["mime"] . "."; $uploadOk = 1; } else { echo "File is not an image."; $uploadOk = 0;?? }}// Check if file already existsif (file_exists($target_file)) { echo "Sorry, fil(???)e already exists."; $uploadOk(???) = 0;}// Check file sizeif ($_FILES["fileToUpload"]["size"] > 50000(′?`*)0) { echo "Sorry, your file is too large."; $uploadOk = 0;}// Allow certain file formatsif($imageFileType != "jpg" && $imageFileType != "png" && $imageFileType != "jpeg"??&???& $imageFileType != "gif&??quot; ) { echo "Sorry, only JPG, JPEG, PNG &a(′▽?zhuān)?mp; GIF files are allowed."; $uploadOk = 0;}// Check if $uploadOk is seヾ(′ω`)?t to 0 by an errorif ($uploadOk == 0) { echo "Sorry, your file was not up??loaded.";// if everything is ok,(╬?益?) try to upload file} else { if (move_uploaded_file($_??FILES["fileToUpload??"]["tmp_nameヽ(′ー`)ノ"], $target_file)) { echo &quヾ(′?`)?ot;The file ". htmlspecialchars( basename( $_FILES["fileToUpload"]["name"])). " has been uploa??ded."; } else { echo "Sor??ry, there was an error uploading your file."; }}?>6、在上面的 PHP 腳本中,我們首先定義了一個(gè)目標文件夾(在這個(gè)例子中是 "uploads/")┐(′?`)┌和目標文件名(通過(guò)獲取用戶(hù)上傳的文件名并將其添加(jia)到目標文件夾路徑中),我們檢??查文件是否為實(shí)際圖像、是否已存在、大小是否超過(guò)限制以ヽ(′?`)ノ及文件格式是否允許,如果所有條??件都滿(mǎn)足,我們將嘗試將文件移動(dòng)到目標文件夾,如果成功,我們將顯示一條消息表示文件已成功上傳;否則,我們將顯示一條錯誤消(xiao)息。