轉自 http://blog.raienet.com/410
http://php5.idv.tw/html.php?mod=article&do=show&shid=6
最近常常碰到檔案上傳,乾脆整理一下並且弄清楚,免得之後要用都要再找。
--
PHP上传实例
上传档案的网页
<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><?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");
}
}
?>
--
一、前置作業
PHP 檔案上傳運作原理
PHP 上傳檔案時會先將檔案移到暫存目錄,上傳成功後再將檔案送往上傳目錄並自動清除暫存檔
php.ini 檔案上傳相關設定
file_uploads = On
; Temporary directory for HTTP uploaded files (will use system default if not
; specified). PHP 暫存目錄設定,預設為系統暫存目錄,若需設定請將前置分號移除
;upload_tmp_dir =
; Maximum allowed size for uploaded files. PHP 預設上傳限制為 2M
upload_max_filesize = 2M
二、檔案上傳示範 (big5)
file.htm
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=big5">
<title>檔案上傳</title>
</head>
<body>
<form action="file_ok.php" method="post" enctype="multipart/form-data">
<input type="hidden" name="max_file_size" value="1024000">
<input type="file" name="myfile">
<input type="submit" value="上傳">
</form>
</body>
</html>
1. 範例檔案為 big5 編碼,因 utf-8 編碼有中文檔案名上傳問題,之後再討論。2. 上傳檔案時,form 的 enctype 屬性要設定為 multipart/form-data
3. <input type="hidden" name="max_file_size" value="1024">
可限定上傳檔案大小(1k = 1024),要寫在 <input type="file" ... > 之前
file_ok.php
<?php
$uploaddir = '';
$uploadfile = $uploaddir.basename($_FILES['myfile']['name']);
echo "<pre>";
if (move_uploaded_file($_FILES['myfile']['tmp_name'], $uploadfile)) {
echo "Upload OK \n";
} else {
echo "Upload failed \n";
}
print_r($_FILES);
echo "</pre>";
?>
1. PHP 4.1 之前版本,要用 $HTTP_POST_FILES 取代 $_FILES2. $uploaddir 為上傳目錄設定。
3. 移除檔案可使用 unlink() 函數,例 unlink($uploaddir.$uploadfile);
三、注意事項
UTF-8 的中文檔案名上傳問題
utf-8 編碼網頁無法在 big5 系統正確處理中文檔名,原因在於 move_uploaded_file() 不能處理 utf-8 中文編碼,需利用 iconv() 函數作轉碼,解決方法如下:
請將
改為
四、檔案上傳示範 (utf-8)
file.htm
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8">
<title>檔案上傳</title>
</head>
<body>
<form action="file_ok.php" method="post" enctype="multipart/form-data">
<input type="hidden" name="max_file_size" value="102400000">
<input type="file" name="myfile">
<input type="submit" value="上傳">
</form>
</body>
</html>
file_ok.php
<meta http-equiv="Content-Type" content="text/html; charset=utf-8">
<?php
$uploaddir = '';
$uploadfile = $uploaddir.basename($_FILES['myfile']['name']);
echo "<pre>";
if (move_uploaded_file($_FILES['myfile']['tmp_name'], iconv("utf-8", "big5", $uploadfile))) {
echo "Upload OK \n";
} else {
echo "Upload failed \n";
}
print_r($_FILES);
echo "</pre>";
?>
--
留言列表