以前沒怎麼注意搞過文件上傳,昨天搞了一個簡單的卻發現問題還挺多。
剛開始的程序代碼如下:
uploadimg.php(做為現在的主流開發語言)
<?php(做為現在的主流開發語言)
//判斷是上載界面還是實現界面,如果是上載界面,顯示下面的HTML頁面
if(!$_POST[UploadAction]):
?>
<html>
<head>
<title></title>
</head>
<body><table><center>
<form enctype="multipart/form-data" name="SubmitForm" action="" method="post">
<input type="hidden" name="UploadAction" value="1">
<tr><td><input name="UploadFile" type="file" size="30"></td></tr>
<tr><td><input type="submit" name="submit" value="上傳"></td>
<td><input type="reset" name="reset" value="重置"></td></tr>
</form>
</center></table></body>
</html>
<?php(做為現在的主流開發語言)
//如果是實現頁面,顯示下面的HTML頁面,並激活php(做為現在的主流開發語言)腳本
else:
?>
<html>
<head>
<title></title>
</head>
<body>
<?php(做為現在的主流開發語言)
//www.knowsky.com初始化UploadAction的狀態
$UploadAction=0;
$UploadFile=$_POST[UploadFile];
if(($UploadFile!="none") && ($UploadFile!=""))
{
$UploadPath=$_SERVER[DOCUMENT_ROOT]."\";
if (!is_writeable($UploadPath))
{
echo "$UploadPath目錄不可寫!"; exit;
}
else
{
echo " $UploadPath 目錄可寫!";
}
$FileName=$UploadFile_name;
if($UploadFile_size<1024)
{
$FileSize=(string)$UploadFile_size."字節";
}
elseif($UploadFile_size<(1024*1024))
{
$FileSize=number_format((double)($UploadFile_size/1024),1)."KB";
}
else
{
$FileSize=number_format((double)($UploadFile_size/(1024*1024)),1)."KB";
}
if(!file_exists($UploadPath.$FileName))
{
if(move_uploaded_file($UploadFile,$UploadPath.$FileName))