<form enctype="multipart/form-data" action="__URL__" method="POST">
<!-- MAX_FILE_SIZE must precede the file input field -->
<input type="hidden" name="MAX_FILE_SIZE" value="30000" />
<!-- Name of input element determines name in $_FILES array -->
Send this file: <input name="userfile" type="file" />
<input type="submit" value="Send File" />
</form>
接收參數陣列 檔案名稱:
$_FILES['userfile']['name']
檔案格式:
$_FILES['userfile']['type']
檔案大小:
$_FILES['userfile']['size']
檔案暫存的位置(伺服器上暫存的位置,必須要移動到正確的位置):
$_FILES['userfile']['tmp_name']
錯誤訊息:
$_FILES['userfile']['error']
語法例:
<?php
// In PHP versions earlier than 4.1.0, $HTTP_POST_FILES should be used instead
// of $_FILES.
$uploaddir = '/var/www/uploads/';
$uploadfile = $uploaddir . basename($_FILES['userfile']['name']);
echo '<pre>';
if (move_uploaded_file($_FILES['userfile']['tmp_name'], $uploadfile)) {
echo "File is valid, and was successfully uploaded.\\\\n";
} else {
echo "Possible file upload attack!\\\\n";
}
echo 'Here is some more debugging info:';
print_r($_FILES);
print "</pre>";
?>
以上介紹了建立上傳檔案用的表單及存取陣列,以下便建立上傳檔案的程式。首先需要建立一個目錄作儲存檔案,並需要確定 web server 有寫入的權限,一般設成 777 就可以,假設使用以上的上傳表單,以及上傳目錄是 "/var/www/uploads/",以下是程式碼範例會顯示上傳檔案資訊以及上傳檔案: upload.php