上载文件表单网页:updatefile.htm
<html>
<head>
<title>上载文件表单</title>
</head>
<body>
<form enctype="multipart/form-data" action="updatefile.php3" method="post">
<input type="hidden" name="max_file_size" value="1000">
<div align="center"><center> 请选取文件:
<input name="userfile" type="file">
<input type="submit" value="send file">
</center></div>
</form>
</body>
</html>
----------------------------------------------------------------
处理上载文件网页:updatefile.php3
<html>
<head>
<meta http-equiv="content-type" content="text/html;
charset=gb2312">
<title>处理上载文件</title>
</head>
<body>
<?
copy($userfile, "newfilename");
echo $userfile." - 用户上传到服务器上的文件临时存放的名称<br>";
echo $userfile_name." - 在用户机器上该文件的原始名称<br>";
echo $userfile_size." - 上传文件的实际字节数<br>";
echo $userfile_type." - 如果用户的浏览器提供了这个信息的话,
它表示mime的类型。例如
image/gif<br>";
?>
</body>
</html>
----------------------------------------------------------------
注意:文件上载后必须拷贝到新的地方或重新更名,
否则当进程执行完后,上载样本将被删除
如何用php3实现文件上载(实例2)
php3是一种非常强大的cgi脚本语言,就其语言特点基本上源于c,
就其实现方式而言更象perl,而其对数据库的内在支持更使之成为asp
的强大对手。
首先在前台必须有一个界面
<html>
<head>
<title>文件上载界面</title>
</head>
<body><table><center>
<form enctype = "multipart/form-data" name = "submitform"
action = "upload.php3" method = "post">
<input type = "hidden" name = "max_file_size" value ="1000000">
<tr>
<td><input name = "uploadfile" type = "file" size = "30"></td>
</tr>
<tr>
<td><input name = "submit" value = "提交" type = "submit"></td>
<td><input name = "reset" value = "重置" type = "reset"></td>
</tr>
</form></center></table></body>
</html>
值得注意的是一个 max_file_size的隐藏值域,通过设置其value可
以限制上载文件的大小。
然后在后台实现文件上载操作。如果只是最基本的上载,寥寥几行
就可以搞定
<?
if($uploadfile != "none")
{
copy($uploadfile,"$uploadfile_name");
unlink($uploadfile);
}
else
{
echo "你没有选择任何文件上载!";
}
?>
因为form传递过来的值自动赋值给同名变量,所以直接通过
$uploadfile可以访问上载的文件,但由于这是一个保存文件的变量,因此
文件名字必须通过另外一个$uploadfile_
name变量取得。在拷贝文件后删去临时文件(unlink)。
上面的代码完全可以工作,但实际应用中漏洞百出,让我们逐步来完善之
首先,上载的文件必须有一个固定的目录保存,我们在这里用一个
$uploadpath变量保存之,如
$uploadpath = "/home/flier/upload/";
或复杂一点的自动定位,如
$uploadpath = addslashes(dirname
($path_translated))."\\upload\\";
$path_translated顾名思义是当前传送目录,
我们假定以其一个名为upload
的子目录来保存上载的文件。dirname函数返回其目录名,
然后加上子目录名
然后用一个变量$filename保存完整的上载后文件名和路径
$filename = $uploadpath.$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) . " mb";
}
number_format函数起到格式化输出的作用,具体用法请参照手册
下一步我们必须考虑到文件已经存在和拷贝操作失败的情况,并提
供相应的提示信息
if(!file_exists($filename))
{
if(copy($uploadfile,$filename))
{
echo "文件 $uploadfile_name
($filesize)上载成功!";
}
else
{
echo "文件 $uploadfile_name上载失败!";
}
unlink($uploadfile);
}
else
{
echo "文件 $uploadfile_name已经存在!";
}
然后我们应该考虑到大文件上载时容易出现超时的情况,可以用
set_time_limit($timelimit);加大超时限制时间。
最后,把截面和实现代码综合到一个单独的文件中,为了实现这
个想法,我们通过在 form中添加一个隐含值
<input type = "hidden" name = "uploadaction" value = "1">指出
当前的状态(界面或实现),以便区分对待完成代码如下
(文件名upload.php3)
<?
if(!$uploadaction):
?>
<html>
<head>
<title>文件上载界面</title>
</head>
<body><table><center>
<form enctype = "multipart/form-data" name = "submitform"
action = "upload.php3" method = "post">
<input type = "hidden" name = "max_file_size" value ="1000000">
<input type = "hidden" name = "uploadaction" value = "1">
<tr>
<td><input name = "uploadfile" type = "file" size = "30"></td>
</tr>
<tr>
<td><input name = "submit" value = "提交" type = "submit"></td>
<td><input name = "reset" value = "重置" type = "reset"></td>
</tr>
</form></center></table></body>
</html>
<?
else:
?>
<html>
<head>
<title>文件上载代码</title>
</head>
<body>
<?
$uploadaction=0;
$timelimit=60; /*设置超时限制时间
缺省时间为 30秒
设置为0时为不限时 */
set_time_limit($timelimit);
if(($uploadfile != "none")&&
($uploadfile != ""))
{
$uploadpath = addslashes(dirname($path_translated))."\\upload\\";
//上载文件存放路径
$filename = $uploadpath.$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)."mb";
}
if(!file_exists($filename))
{
if(copy($uploadfile,$filename))
{
echo "文件 $uploadfile_name ($filesize)上载成功!";
}
else
{
echo "文件 $uploadfile_name上载失败!";
}
unlink($uploadfile);
}
else
{
echo "文件 $uploadfile_name已经存在!";
}
}
else
{
echo "你没有选择任何文件上载!";
}
set_time_limit(30); //恢复缺省超时设置
?>
<br><a href = "upload.php3">返回</a>
</body>
</html>
<?
endif;
?>
Java Asp PHP .Net XML C/C++ CGI VB Jsp J2ee J2se J2me EJB Servlet Tomcat Resin Struts Weblogic Eclipse ANT GUI JMS Web servise IDEA Webphere Hibernate Spring Jboss Applet Swing Socket Javamail Perl Ajax P2P 安全 模式 框架 测试 开源 游戏
Windows XP Windows 2000 Windows 2003 Windows Me Windows 9.x Linux UNIX 注册表 操作系统 服务器 应用服务器