close

PHP,檔案上傳

; Whether to allow HTTP file uploads. 檔案上傳,預設為開啟
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 取代 $_FILES
2. $uploaddir 為上傳目錄設定。
3. 移除檔案可使用 unlink() 函數,例 unlink($uploaddir.$uploadfile);

 

move_uploaded_file($_FILES['myfile']['tmp_name'], $uploadfile)

四、檔案上傳示範 (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>";
?>

 

 

arrow
arrow
    全站熱搜
    創作者介紹
    創作者 BlackHawk 的頭像
    BlackHawk

    鐵人

    BlackHawk 發表在 痞客邦 留言(0) 人氣()