html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=gb2312">
<title>PHP上传文件例子</title>
</head>
<body>
<table width="566" border="0" cellpadding="0" cellspacing="0">
<!--DWLayoutTable-->
<tr>
<td width="566" height="216" align="center" valign="middle"><form action="" method="post" enctype="multipart/form-data" name="form1">
<p>
<input type="file" name="file1" id="file1">
</p>
<p>
<input type="submit" name="Submit" value="提交">
</p>
</form></td>
</tr>
</table>
</body>
</html>
<?php
$ufile=$_FILES['file1']['tmp_name'];
$ufile_name=$_FILES['file1']['name'];
if($ufile){ //用move_uploaded_file()方法上传
if (!move_uploaded_file($ufile,"Apic/".$ufile_name)) {echo "file:".$ufile_name." upload file fail!";exit();}
chmod ("Apic/".$ufile_name, 0777);//作用将图片属性变为任何权限都可以读写和执行命令.
echo "<script>window.alert('文件上传完成!')</script>";
}
?>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=gb2312">
<title>PHP上传文件例子</title>
</head>
<body>
<table width="566" border="0" cellpadding="0" cellspacing="0">
<!--DWLayoutTable-->
<tr>
<td width="566" height="216" align="center" valign="middle"><form action="" method="post" enctype="multipart/form-data" name="form1">
<p>
<input type="file" name="file1" id="file1">
</p>
<p>
<input type="submit" name="Submit" value="提交">
</p>
</form></td>
</tr>
</table>
</body>
</html>
<?php
$ufile=$_FILES['file1']['tmp_name'];
$ufile_name=$_FILES['file1']['name'];
if($ufile){ //用move_uploaded_file()方法上传
if (!move_uploaded_file($ufile,"Apic/".$ufile_name)) {echo "file:".$ufile_name." upload file fail!";exit();}
chmod ("Apic/".$ufile_name, 0777);//作用将图片属性变为任何权限都可以读写和执行命令.
echo "<script>window.alert('文件上传完成!')</script>";
}
?>
<input type="file" name="upfile">
<input type="submit" value="提 交" name="B1"> <input type="reset" value="重填" name="B2">
</form>
处理上传文件的代码guboinsertfile.php如下:<?php//如果上传的文件名为空,则退出
if ($upfile_name=="")
{
echo "错误:没有提交文件!";
echo "<a href=guboupfile.php>返回</a>";
exit;
}
//上传文件的尺寸小于等于0
if ($upfile_size<=0)
{
echo "错误:没有提交文件!";
echo "<a href=guboupfile.php>返回</a>";
exit;
} $file_size_max = 10000000;// 限制所上传的文件最大尺寸为10000k(bytes)
$store_dir = "sucai/";//上传文件的存储位置
$accept_overwrite = true;//允许读写文件
if ($upfile_size> $file_size_max) // 检查文件大小
{
echo "错误:文件尺寸太大,不能超过10000K";
echo "<a href=guboupfile.php>返回</a>";
exit;
}
//计算文件名的总长度
$filename_long=strlen($upfile_name);//计算从左到右,扩展名点号的位置,在第几个字符后面
$point_address=strpos($upfile_name,".");
//取文件名后面的点号扩展名
//从右到左取的字符个数
$rightToleft=$filename_long-$point_address;//点号带扩展名是什么
$pointAndextend=substr("$upfile_name",-$rightToleft);//提取当前日期,并形成如此格式20050508135736
$createdate=date("YmdHis");//重命名文件的名称
$newupfilename=$createdate.$pointAndextend;if (file_exists($store_dir.$newupfilename) or !$accept_overwrite)//检查读写文件
{
echo "错误:已经有了这个文件名或者此目录不允许写";
echo "<a href=guboupfile.php>返回</a>";
exit;
}
if (! @ copy($upfile,$store_dir.$newupfilename))//复制文件到指定目录
{
echo "错误:复制失败";
echo "<a href=guboupfile.php>返回</a>";
exit;
}
echo "恭喜!上传成功!上传的文件在服务器上的网址是:<br>";
echo "/".$store_dir.$newupfilename;?>