刚开始的程序代码如下: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)) { echo "文件 $UploadFile_name ($FileSize) 上传成功!"; } else { echo "文件 $UploadFile_name ($FileSize) 上传失败!"; } unlink($UploadPath.$FileName); } else { echo "文件 $UploadFile_name 已经存在!"; }}else{ echo "你没有选择任何文件上传,或者上传的文件超出了 $MAX_FILE_SIZE !";}
?>
<br><a href="uploadimg.php">返回</a></body></html>
<?php
endif
?>
运行发现上传一点效果都没有,连实现页面都进入不了,搜索询问之后才晓得上传文件获得要用$_FILES
于是做了一些变化:
$UploadFile=$_FILES['UploadFile']['tmp_name'];$UploadFile_size=$_FILES['UploadFile']['size'];$UploadFile_name=$_FILES['UploadFile']['name'];$FileName=strtolower($UploadFile_name);
本来以为这下一定搞定,结果出现错误信息为:
Warning: move_uploaded_file(F:\Server\www\): failed to open stream: Permission denied in F:\Server\www\eoool\finish\uploadimg.php on line 76Warning: move_uploaded_file(): Unable to move 'E:\WINDOWS\TEMP\php34.tmp' to 'F:\\Server\\www' in F:\Server\www\eoool\finish\uploadimg.php on line 76文件 *****(****字节) 上传失败!
而我是在WIN系统上进行调试,应该不存在文件权限问题,自己看了又看,想了又想,却仍然不得其法,在落伍和喜悦都发帖求救,却没有得到正确的答案,仅仅一个简单的文件上传费了我1天多的时间仍然搞不定,让我很是丧气,网上又搜索不到与我类似的错误。最终没办法,我想到为何用sablog调试的时候上传附件可以成功呢?于是仔细看了下sablog的附件上传的原代码,参照其代码又做了如下变化:
function getextension($filename) { return substr(strrchr($filename, "."), 1);}
$extension=getextension($FileName);
if(move_uploaded_file($UploadFile,$UploadPath."temp.".$extension))
//unlink($UploadFile); unlink($UploadPath."temp.".$extension);
结果马上收到效果,上传成功!我这里用到unlink($UploadPath."temp.".$extension);是为了马上删除掉上传到空间的文件,节省空间。