文件上傳我們需要用到HTML裡面表單的type=file類型
也許每一個站點都可能會對上傳文件有許多的限制
當然出錯的預處理也是我們不容忽視的!如果再深一步的討論我們還可以對文件的操作起用事件日志的記錄
下面我們通過一段程序來實現這些功能
首先是我們預設的變量值
$MAX_SIZE =
$FILE_MIMES = array(
$FILE_EXTS = array(
$DELETABLE = true;
下一部就是設置浏覽器訪問變量及目錄訪問變量
$site_name = $_SERVER[
$url_dir = http://
$url_this = http://
$upload_dir = files/;
$upload_url = $url_dir
$message =;
建立上傳目錄並相應改變權限
if (!mkdir($upload_dir))
die (upload_files directory doesn
if (!chmod($upload_dir
die (change permission to
}
用戶請求的處理
if ($_REQUEST[del] && $DELETABLE) {
$resource = fopen(log
fwrite($resource
fclose($resource);
if (strpos($_REQUEST[del]
else if (strpos($_REQUEST[del]
else if (substr($_REQUEST[del]
unlink($_REQUEST[del]);
print ﹤script﹥window
}
}
else if ($_FILES[
$resource = fopen(log
fwrite($resource
fclose($resource);
$file_type = $_FILES[
$file_name = $_FILES[
$file_ext = strtolower(substr($file_name
//文件大小的檢查
if ( $_FILES[
$message = The file size is over
//File Type/Extension Check
else if (!in_array($file_type
&& !in_array($file_ext
$message = Sorry
else
$message = do_upload($upload_dir
print ﹤script﹥window
}
else if (!$_FILES[
else
$message = Invalid File Specified
列出我們上傳的文件
$handle=opendir($upload_dir);
$filelist = ;
while ($file = readdir($handle)) {
if(!is_dir($file) && !is_link($file)) {
$filelist
if ($DELETABLE)
$filelist
$filelist
$filelist
}
}
function do_upload($upload_dir
$temp_name = $_FILES[
$file_name = $_FILES[
$file_name = str_replace(\\\\
$file_name = str_replace(
$file_path = $upload_dir
//File Name Check
if ( $file_name ==) {
$message = Invalid File Name Specified;
return $message;
}
$result = move_uploaded_file($temp_name
if (!chmod($file_path
$message = change permission to
else
$message = ($result)?$file_name uploaded successfully
Somthing is wrong with uploading a file
return $message;
}
?﹥
﹤center﹥
﹤font color=red﹥﹤?=$_REQUEST[message]?﹥﹤/font﹥
﹤br﹥
﹤form name=upload id=upload ENCTYPE=multipart/form
Upload File ﹤input type=file id=userfile name=userfile﹥
﹤input type=submit name=upload value=Upload﹥
﹤/form﹥
﹤br﹥﹤b﹥My Files﹤/b﹥
﹤hr width=
﹤?=$filelist?﹥
﹤hr width=
﹤small﹥﹤sup﹥Developed By
﹤a style=text
﹤/sup﹥﹤/small﹥
﹤/center﹥
From:http://tw.wingwit.com/Article/program/PHP/201311/21506.html