以前没怎么注意搞过文件上传,昨天搞了一个简单的却发现问题还挺多。
刚开始的程序代码如下:
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
(本文来源于图老师网站,更多请访问http://m.tulaoshi.com/php/)//如果是实现页面,显示下面的HTML页面,并激活PHP脚本
else:
?
<html
<head
<title</title
</head
<body
<?php
(本文来源于图老师网站,更多请访问http://m.tulaoshi.com/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))
{
echo "文件 $UploadFile_name ($FileSize) 上传成功!";
}
else
{
echo "文件 $UploadFile_name ($FileSize) 上传失败!";