PHP上傳實例
程序员文章站
2022-05-24 14:35:09
...
上傳檔案的網頁
<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']
檔案格式(image/jpeg)
$_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>";
?>
--------------------------------------------------------------------------------
多檔同時傳送陣列實例<form action="" method="post" enctype="multipart/form-data"> <p>Pictures: <input type="file" name="pictures[]" /> <input type="file" name="pictures[]" /> <input type="file" name="pictures[]" /> <input type="submit" value="Send" /> </p> </form><?php
foreach ($_FILES["pictures"]["error"] as $key => $error) {
if ($error == UPLOAD_ERR_OK) {
$tmp_name = $_FILES["pictures"]["tmp_name"][$key];
$name = $_FILES["pictures"]["name"][$key];
move_uploaded_file($tmp_name, "data/$name");
}
}
?>