在线等....怎么修改PHP上传文件的名字?随便高手帮偶PP一个上传文件的类 1)不要依赖$_FILES["filename"]["name"]传过来的文件名,这是不可靠的,不但有非英文字符问题,还有重名问题,还可能被人利用。2)function __construct($filename) $filename没有被利用3)没有考虑多文件上传问题 解决方案 » 免费领取超大流量手机卡,每月29元包185G流量+100分钟通话, 中国电信官方发货 最后那个 function 这么写吧,方便以后扩展,function filetype(){ static $filetypes = array( 'image/gif' => "【.gif/.jpg图片类型】", 'image/pjpeg' =>"【.gif/.jpg图片类型】", "application/octet-stream" => "【.rar/.exe/格式文件】", "application/x-shockwave-flash" => ".swf【flash格式文件】", "text/plain" => ".html【网页文件格式】" ); if (isset($filetypes[$this->filetype])) { return $filetypes[$this->filetype]; } else { return null; }} function __construct($filename){ require_once("UPLOAD_Attribute.inc.php");//调用文件路径,文件大小等设置信息$this->uploaddir="$uploaddir";$this->filename=$_FILES["filename"]["name"];//读出上传文件的名$this->filesize=$_FILES["filename"]["size"];//接受上传文件的大小,并传给属性$this->filetype=$_FILES["filename"]["type"];//接受上传文件的类型并传给属性$this->fileload_tmp=$_FILES["filename"]["tmp_name"];//接收上传文件上传的临时目录 $this->Upload_size(); }======================================在一个类的方法直接引用外部变量$_files是非常坏的事情。注意封装的概念,不光是整合,还有封闭,就是对象与外界的分离。 function Upload_Success(){if(move_uploaded_file($this->fileload_tmp,$this->uploadfile))移动文件 的时候,使用自己确定的文件名不就可以做到了? 没有写太完整的一个类,可能对你有帮助<?phpclass Upload{ function Upload() { //构造函数 //最大上传文件大小 $this->Max_Size = 20000000; //设置Mine类型 $this->Flie_Mimes = array('image/jpeg','image/jpg','image/gif','image/png','application/msword'); //设置允许上传的文件类型,按照格式添加 $this->File_Postfix = array('.rar','.zip','.doc','.jpg','.gif','.wma','.rm','.wmv','.mpeg','.mp3','.avi'); /** //创建上传目录 if(!is_dir($this->upload_dir)) { if(!mkdir($this->upload_dir)) { die('文件没有创建成功!!'); } if (!chmod($this->upload_dir,0777)) { die ("改变权限失败."); } } **/ } function Up_load($Upload_Dir) { $Upload_Url = "http://".$_SERVER['HTTP_HOST'].dirname($_SERVER['PHP_SELF'])."/".$Upload_Dir; //上传文件处理 if($_FILES['userfile']) { //-----写日志------- //$resourse = fopen('log.txt','a'); //fwrite($resourse,date('Y-m-d H:i:s')."上传 - 上传操作的ip地址为: $_SERVER[REMOTE_ADDR]".$_FILES['userfile']['name']." ".$_FILES['userfile']['type']."\n\n"); //fclose($resourse); //------------------ $File_Name = $_FILES['userfile']['name']; //上传文件的名称 $File_Type = $_FILES['userfile']['type']; //上传文件的类型 $This_File_Postfix = substr($File_Name,strrpos($File_Name,".")); //获取上传文件的后缀 //文件大小检查 if($_FIFES['userfile']['size'] > $this->Max_Size) { echo '文件太大不能上传'; echo "<meta http-equiv=\"refresh\" content=\"1;url=test2.php\">"; exit; } elseif(!in_array($File_Type,$this->Flie_Mimes) && !in_array($This_File_Postfix,$this->File_Postfix)) { echo "对不起你所上传的文件类型不符合规定,不允许上传!!"; echo "<meta http-equiv=\"refresh\" content=\"1;url=test2.php\">"; exit; } else { //echo $Upload_Dir; //exit; $this->Do_Upload($Upload_Dir,$Upload_Url); } } } function Do_Upload($Upload_Dir,$Upload_Url) { $Temp_Name = $_FILES['userfile']['tmp_name']; //文件被上传后在服务端储存的临时文件名 //获取后缀名,并主合成新的名称 $File_Name = date("YmdHis").substr($_FILES['userfile']['name'],strrpos($_FILES['userfile']['name'],".")); $File_Path = $Upload_Dir.$File_Name; //文件名检查 if($File_Name = '') { echo "文件名无效!!!"; echo "<meta http-equiv=\"refresh\" content=\"1;url=test2.php\">"; exit; } //echo $Temp_Name; //exit; $result = move_uploaded_file($Temp_Name,$File_Path); if($result) { echo "上传成功!!"; echo "<meta http-equiv=\"refresh\" content=\"1;url=test2.php\">"; exit; } else { echo "上传失败!!"; echo "<meta http-equiv=\"refresh\" content=\"1;url=test2.php\">"; exit; } echo "end"; } function Del($FileName) { $Result = unlink($FileName); if(!$Result) { echo "删除操作失败,请重试"; echo "<meta http-equiv=\"refresh\" content=\"1;url=test2.php\">"; exit; } else { echo "删除操作成功!返回"; echo "<meta http-equiv=\"refresh\" content=\"1;url=test2.php\">"; exit; } }}$upload = new upload();if($_GET['action']=='upload'){ $upload->Up_load("sc/");}else if($_GET['action']=='del'){ $upload->Del($_GET['File']);}unset($upload);?><html><head><title>资料管理</title><meta http-equiv="Content-Type" content="text/html; charset=gb2312"></head><body><table width="100%" border="0" cellpadding="3" cellspacing="1" bgcolor="#CCCCCC"> <tr> <td height="16" colspan="2" bgcolor="#FFFFFF">资料上传</td> </tr> <tr> <td height="25" colspan="2" bgcolor="#FFFFFF"> <form name="upload" id="upload" enctype="multipart/form-data" method="post" action ="upload.php?action=upload"> 上传文件 <input type="file" id="userfile" name="userfile"> <input type="submit" name="upload" value="上传"> </form></td> </tr></table><br><table width="100%" border="0" cellpadding="3" cellspacing="1" bgcolor="#CCCCCC"> <tr> <td width="39%" align="center" bgcolor="#FFFFFF"><strong>文件名</strong></td> <td width="18%" align="center" bgcolor="#FFFFFF"><strong>大小</strong></td> <td width="25%" align="center" bgcolor="#FFFFFF"><strong>上传时间</strong></td> <td width="18%" align="center" bgcolor="#FFFFFF"><strong>操作</strong></td> </tr><?$upload_dir="sc/"; $handle = opendir($upload_dir);while($file = readdir($handle)){ if(!is_dir($file) && !is_link($file)) {?> <tr> <td align="center" bgcolor="#FFFFFF"><img src='<?=$upload_dir.$file?>'><br><?=$file?></td> <td align="center" bgcolor="#FFFFFF"><?=filesize($upload_dir.$file)?>byte</td> <td align="center" bgcolor="#FFFFFF"><?=date("Y-m-d H:i:s", filemtime($upload_dir.$file))?></td> <td align="center" bgcolor="#FFFFFF"><a href="test2.php?action=del&File=<?=$upload_dir.$file?>">删除</a></td> </tr><? }}?></table></body></html> php smarty 国际化问题 php为什么用phpmyadmin?而不用其他可视化工具呢? 404错误之后转到指定的页面 急 100分 求熟手写的php项目代码 主要拿来学习用 请大家帮忙 3Q! php看看我这个字符串应该如何添加 流动的变量 xmlhttp,submit,window.open 请问哪有Zend的Encode编译器下载,盗版的那种。HH 网上是否有类似这样的 免费PHP 管理系统? 招聘PHP及LINUX系统管理人员地点上海 请问各位大侠,这是什么加密方式?好奇怪,没见过,看得我眼花。 php文件上传 错误提示 3,晕了~~~ 我要执行另一个脚本怎么弄?
static $filetypes = array(
'image/gif' => "【.gif/.jpg图片类型】",
'image/pjpeg' =>"【.gif/.jpg图片类型】",
"application/octet-stream" => "【.rar/.exe/格式文件】",
"application/x-shockwave-flash" => ".swf【flash格式文件】",
"text/plain" => ".html【网页文件格式】"
);
if (isset($filetypes[$this->filetype])) {
return $filetypes[$this->filetype];
}
else {
return null;
}
}
require_once("UPLOAD_Attribute.inc.php");//调用文件路径,文件大小等设置信息
$this->uploaddir="$uploaddir";
$this->filename=$_FILES["filename"]["name"];//读出上传文件的名
$this->filesize=$_FILES["filename"]["size"];//接受上传文件的大小,并传给属性
$this->filetype=$_FILES["filename"]["type"];//接受上传文件的类型并传给属性
$this->fileload_tmp=$_FILES["filename"]["tmp_name"];//接收上传文件上传的临时目录
$this->Upload_size();
}
======================================
在一个类的方法直接引用外部变量$_files是非常坏的事情。注意封装的概念,不光是整合,还有封闭,就是对象与外界的分离。
if(move_uploaded_file($this->fileload_tmp,$this->uploadfile))移动文件 的时候,使用自己确定的文件名不就可以做到了?
<?php
class Upload
{
function Upload()
{
//构造函数
//最大上传文件大小
$this->Max_Size = 20000000;
//设置Mine类型
$this->Flie_Mimes = array('image/jpeg','image/jpg','image/gif','image/png','application/msword');
//设置允许上传的文件类型,按照格式添加
$this->File_Postfix = array('.rar','.zip','.doc','.jpg','.gif','.wma','.rm','.wmv','.mpeg','.mp3','.avi');
/**
//创建上传目录
if(!is_dir($this->upload_dir))
{
if(!mkdir($this->upload_dir))
{
die('文件没有创建成功!!');
}
if (!chmod($this->upload_dir,0777))
{
die ("改变权限失败.");
}
}
**/
}
function Up_load($Upload_Dir)
{
$Upload_Url = "http://".$_SERVER['HTTP_HOST'].dirname($_SERVER['PHP_SELF'])."/".$Upload_Dir;
//上传文件处理
if($_FILES['userfile'])
{
//-----写日志-------
//$resourse = fopen('log.txt','a');
//fwrite($resourse,date('Y-m-d H:i:s')."上传 - 上传操作的ip地址为: $_SERVER[REMOTE_ADDR]".$_FILES['userfile']['name']." ".$_FILES['userfile']['type']."\n\n");
//fclose($resourse);
//------------------
$File_Name = $_FILES['userfile']['name']; //上传文件的名称
$File_Type = $_FILES['userfile']['type']; //上传文件的类型
$This_File_Postfix = substr($File_Name,strrpos($File_Name,".")); //获取上传文件的后缀
//文件大小检查
if($_FIFES['userfile']['size'] > $this->Max_Size)
{
echo '文件太大不能上传';
echo "<meta http-equiv=\"refresh\" content=\"1;url=test2.php\">";
exit;
}
elseif(!in_array($File_Type,$this->Flie_Mimes) && !in_array($This_File_Postfix,$this->File_Postfix))
{
echo "对不起你所上传的文件类型不符合规定,不允许上传!!";
echo "<meta http-equiv=\"refresh\" content=\"1;url=test2.php\">";
exit;
}
else
{
//echo $Upload_Dir;
//exit;
$this->Do_Upload($Upload_Dir,$Upload_Url);
}
}
}
function Do_Upload($Upload_Dir,$Upload_Url)
{
$Temp_Name = $_FILES['userfile']['tmp_name']; //文件被上传后在服务端储存的临时文件名
//获取后缀名,并主合成新的名称
$File_Name = date("YmdHis").substr($_FILES['userfile']['name'],strrpos($_FILES['userfile']['name'],"."));
$File_Path = $Upload_Dir.$File_Name;
//文件名检查
if($File_Name = '')
{
echo "文件名无效!!!";
echo "<meta http-equiv=\"refresh\" content=\"1;url=test2.php\">";
exit;
}
//echo $Temp_Name;
//exit;
$result = move_uploaded_file($Temp_Name,$File_Path);
if($result)
{
echo "上传成功!!";
echo "<meta http-equiv=\"refresh\" content=\"1;url=test2.php\">";
exit;
}
else
{
echo "上传失败!!";
echo "<meta http-equiv=\"refresh\" content=\"1;url=test2.php\">";
exit;
}
echo "end";
}
function Del($FileName)
{
$Result = unlink($FileName);
if(!$Result)
{
echo "删除操作失败,请重试";
echo "<meta http-equiv=\"refresh\" content=\"1;url=test2.php\">";
exit;
}
else
{
echo "删除操作成功!返回";
echo "<meta http-equiv=\"refresh\" content=\"1;url=test2.php\">";
exit;
}
}
}$upload = new upload();
if($_GET['action']=='upload')
{
$upload->Up_load("sc/");
}
else if($_GET['action']=='del')
{
$upload->Del($_GET['File']);
}
unset($upload);
?>
<html>
<head>
<title>资料管理</title>
<meta http-equiv="Content-Type" content="text/html; charset=gb2312">
</head>
<body>
<table width="100%" border="0" cellpadding="3" cellspacing="1" bgcolor="#CCCCCC">
<tr>
<td height="16" colspan="2" bgcolor="#FFFFFF">资料上传</td>
</tr>
<tr>
<td height="25" colspan="2" bgcolor="#FFFFFF">
<form name="upload" id="upload" enctype="multipart/form-data" method="post" action ="upload.php?action=upload">
上传文件
<input type="file" id="userfile" name="userfile">
<input type="submit" name="upload" value="上传">
</form></td>
</tr>
</table>
<br>
<table width="100%" border="0" cellpadding="3" cellspacing="1" bgcolor="#CCCCCC">
<tr>
<td width="39%" align="center" bgcolor="#FFFFFF"><strong>文件名</strong></td>
<td width="18%" align="center" bgcolor="#FFFFFF"><strong>大小</strong></td>
<td width="25%" align="center" bgcolor="#FFFFFF"><strong>上传时间</strong></td>
<td width="18%" align="center" bgcolor="#FFFFFF"><strong>操作</strong></td>
</tr>
<?
$upload_dir="sc/";
$handle = opendir($upload_dir);
while($file = readdir($handle))
{
if(!is_dir($file) && !is_link($file))
{
?>
<tr>
<td align="center" bgcolor="#FFFFFF"><img src='<?=$upload_dir.$file?>'><br><?=$file?></td>
<td align="center" bgcolor="#FFFFFF"><?=filesize($upload_dir.$file)?>byte</td>
<td align="center" bgcolor="#FFFFFF"><?=date("Y-m-d H:i:s", filemtime($upload_dir.$file))?></td>
<td align="center" bgcolor="#FFFFFF"><a href="test2.php?action=del&File=<?=$upload_dir.$file?>">删除</a></td>
</tr>
<?
}
}
?>
</table>
</body>
</html>