主题:文件上传代码
wet123
[专家分:0] 发布于 2006-11-30 10:33:00
新手,求教个php文件上传的代码?
能够判断文件大小和文件类型的,最好给点注释!
回复列表 (共3个回复)
沙发
5ibc [专家分:150] 发布于 2006-11-30 12:05:00
http://blog.programfan.com/blog.asp?blogid=3028
板凳
longlong16 [专家分:10670] 发布于 2006-11-30 17:47:00
假如提交页面有
<input type="file" name="imageurl"/>
提交处理页面:
if($imageurl!=""){
//判断图片的类型,这里允许jpg bmp gif png类型
if(($imageurl_type=="image/gif") || ($imageurl_type=="image/pjpeg")
|| ($imageurl_type=="image/x-png") || ($imageurl_type=="image/bmp")){
//判断图片大小,不超过1m
if($imageurl_size<=1000000){
//重新生成路径
$dirurl="upload"."\\";
$datetime = date("YmdHis_");
$newname=$dirurl.$datetime.$imageurl_name;
copy($imageurl,$newname);
}
}
}
上面的newname是文件的路径可以保存到数据库里
3 楼
metome [专家分:0] 发布于 2006-12-07 14:43:00
<?php
session_start();
$picdir = 'upload/';
?>
<?php
if($_POST['upload']){
$MAX_SIZE = 20000000;
$FILE_MIMES = array('image/jpeg','image/jpg','image/gif','image/png','application/msword');
$FILE_EXTS = array('.zip','.jpg','.png','.gif','.wma','.rm','.wmv','.doc','.mpeg','.mp3','.avi');
$DELETABLE = true;
$site_name = $_SERVER['HTTP_HOST'];
$url_dir = "http://".$_SERVER['HTTP_HOST'].dirname($_SERVER['PHP_SELF']);
$url_this = "http://".$_SERVER['HTTP_HOST'].$_SERVER['PHP_SELF'];
$upload_dir = $picdir;
$upload_url = $url_dir."/".$picdir;
$message ="";
function do_upload($upload_dir, $upload_url) {
global $picdir;
$temp_name = $_FILES['userfile']['tmp_name'];
$file_name = $_FILES['userfile']['name'];
$file_name = str_replace("\\","",$file_name);
$file_name = str_replace("'","",$file_name);
$file_path = $upload_dir.$file_name;
$tm=date('Ymdhis');
$tn=strstr($file_name,".");
$tt=$tm.$tn;
if ( $file_name =="") {
$message = "文件名无效";
return $message;
}
$result = move_uploaded_file($temp_name, $file_path);
$message = ($result)?"$file_name 上传成功." :
"上传文件中有错误.";
rename($file_path,$picdir.$tt);
$_SESSION['pic']=$tt;
return $message;
}
if ($_FILES['userfile']) {
$resource = fopen("log.txt","a");
fwrite($resource,date("Ymd h:i:s")."UPLOAD - $_SERVER[REMOTE_ADDR]"
.$_FILES['userfile']['name']." "
.$_FILES['userfile']['type']."\n");
fclose($resource);
$file_type = $_FILES['userfile']['type'];
$file_name = $_FILES['userfile']['name'];
$file_ext = substr($file_name,strrpos($file_name,"."));
if ( $_FILES['userfile']['size'] > $MAX_SIZE)
$message = "文件超过 2MB.";
else if (!in_array($file_type, $FILE_MIMES)
&& !in_array($file_ext, $FILE_EXTS) )
$message = "对不起, $file_name($file_type) 不允许上传.";
else
{
if($_SESSION['pic']){
unlink($picdir.$_SESSION['pic']);
}
$message = do_upload($upload_dir, $upload_url);
}
}
else if (!$_FILES['userfile']);
else
$message = "指定文件无效.";
}
?>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=gb2312" />
<title>无标题文档</title>
<link href="css/css.css" rel="stylesheet" type="text/css" />
<link href="admin/manage.css" rel="stylesheet" type="text/css" />
<style type="text/css">
<!--
body {
margin-left: 0px;
margin-top: 0px;
margin-right: 0px;
margin-bottom: 0px;
}
-->
</style></head>
<body>
<table border="0" align="left" cellpadding="0" cellspacing="0" class="RightTable">
<form name="upload" ENCTYPE="multipart/form-data" method="post"><tr>
<td align="left" bgcolor="#FFFFFF" class="black_bold_b">上传文件:</td>
<td align="left" bgcolor="#FFFFFF"> <input type="file" name="userfile">
<input type="submit" name="upload" value="上传"></td>
<td align="left" bgcolor="#FFFFFF"><?php
if($picl){
?>
<img src="<?=$picdir.$_SESSION['pic']?>" height="100" width="100" />
<?php
}
?></td>
</tr></form>
</table>
</body>
</html>
我来回复