上载文件的表单网页(upload.htm)代码如下:
<HTML>
<HEAD>
<TITLE>Upload Your File</TITLE>
</HEAD>
<BODY>
<FORM ACTION="receiver.php" ENCTYPE="multipart/form-data" METHOD=POST>
<INPUT TYPE="HIDDEN" NAME="MAX_FILE_SIZE" VALUE="2000000">
<INPUT TYPE="FILE" NAME="uploadfile" SIZE="24" MAXLENGTH="80">
<BR><BR>
<INPUT TYPE="SUBMIT" VALUE="Upload File!" NAME="sendit">
<INPUT TYPE="SUBMIT" VALUE="Cancel" NAME="cancelit">
<BR>
</FORM>
</BODY>
</HTML>
处理上载文件的PHP文件(receiver.php)如下:
<?
function do_upload ()
{
global $uploadfile, $uploadfile_size;
global $local_file, $error_msg;
if ( $uploadfile == "none" )
{
$error_msg = "对不起,你没有选定任何文件上传!";
return;
}
if ( $uploadfile_size > 2000000 )
{
$error_msg = "对不起,你要上传的文件太大了!";
return;
}
$the_time = time ();
// 在这里指定你用来存放上传文件的目录,你需要对以下目录有写权限
// 同时,我们也可以给上传文件指定另外的目录,如:$upload_dir = "/local/uploads";
$upload_dir = "d:/usr/www/upload/upload";
$local_file = "$upload_dir/$the_time";
if ( file_exists ( '$local_file' ) )
{
$seq = 1;
while ( file_exists ( "$upload_dir/$the_time$seq" ) ) { $seq++; }
$local_file = "$upload_dir/$the_time$seq";
}
rename ( $uploadfile, $local_file );
display_page ();
}
function display_page ()
{
// 这里是你的页面内容
}
?>
<HTML>
<HEAD>
<TITLE>php Receiving Script</TITLE>
</HEAD>
<BODY>
<?
if ( $error_msg ) { echo "<B>$error_msg</B><BR><BR>"; }
if ( $_POST["sendit"])
{
do_upload ();
echo "文件上载成功!";
}
elseif ( $cancelit )
{
header ( "Location: $some_other_script" );
echo "文件上载失败!";
exit;
}
else
{
some_other_func ();
}
function some_other_func()
{
echo"上传有误!";
}
?>
</BODY>
</HTML>
出现错误如下:Warning: rename(,d:/usr/www/upload/upload/1172545799): Permission denied in d:\usr\www\upload\receiver.php on line 29
文件上载成功!
但是上传文件夹里没有任何文件。请各位大虾不吝赐教,万分感谢!