先記下來
出處:http://blog.xuite.net/dizzy03/murmur/49555435-%5BPHP%5D%5B%E8%BD%89%5D+PHP%E6%AA%94%E6%A1%88%E4%B8%8A%E5%82%B3
---------------------------------------------------------------------------------------------------------------------------
上傳檔案的網頁<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>p.s. 重點* form要加上 enctype="multipart/form-data"才能上傳檔案* 上傳檔案的input type要設為file接收參數陣列
檔案名稱
$_FILES['userfile']['name']檔案格式(image/jpeg)
$_FILES['userfile']['type']
檔案大小
$_FILES['userfile']['size']
檔案暫存的位置(伺服器上暫存的位置,必須要移動到正確的位置)
$_FILES['userfile']['tmp_name']
錯誤訊息
$_FILES['userfile']['error']P.s. $_FILE['xxxxx']裡頭的xxxxx取決於你form裡頭選擇檔案input的"name"使用到PHP 的 move_uploaded_file 函式bool move_uploaded_file(source file, target file);即是將source file搬移到target file(名稱可自訂), 搬移完成後再將原檔案刪除.如windows中的剪下貼上.如搬移成功則回傳true, 失敗則回傳false語法例:
<?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><?phpforeach ($_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");
}
}?>
0 意見:
張貼留言