日期:2014-05-17  浏览次数:20470 次

上传文件判断文件名是否重复请指教
上传文件时候如何判断 文件名重复?
上传文件我知道
$_FILES["表单名字"]
$_FILES["表单名字"]["name"]名字
如何获取上传路径文档里面的图片名字?


<?php
class UpFile{
private $name; //名字
private $type; //类型
private $size; //大小
private $tempDirectory; //服务器的临时路径
private $directory; //的实际路径 (临时 移动 实际)
private $allowType; //允许的文件类型
private $error;

function __construct($FILE){
$this->name = $FILE["name"];
$this->type = $FILE["type"];
$this->size = $FILE["size"];
$this->tempDirectory = $FILE["tmp_name"];
$this->directory = "../image/".$this->name;

$this->allowType = array(
'image/jpg',
  'image/png',
  'image/gif',
  'image/pjpeg'
);
$this->error = $FILE["error"];
  }
function isUpFile(){ //是否有文件 1或0 1代表有
return is_uploaded_file($this->tempDirectory);
}
function moveFile(){ //文件移动
move_uploaded_file($this->tempDirectory,$this->directory);
}
function iftype(){ //检查文件类型 unlink(路径);//文件删除方法
if(in_array($this->type,$this->allowType)){
return true;
}else{
return false;
}
}
function ifName(){
//如何判断上传的文件名与已上传的文件名重复
}
}
?>


<?php
include("UpFile.php");
$upfile = new UpFile($_FILES["upfile"]);

?>
  <form action="" method="post" enctype="multipart/form-data">
  <p>上传文件模块</p>
  <input type="file" name="upfile" size="20"/><br/>
  <input type="submit" value="提交"/>
  </form>



------解决方案--------------------
$file=$_FILES["表单名字"]["name"];
if(file_exists($file))
echo 'exist';
else
move_uploaded_file()........
------解决方案--------------------
if(file_exists($this->tempDirectory)) echo '文件已存在';
------解决方案--------------------
按照规则先生成绝对路径,然后就能知道该路径下的文件是否存在了呗...
不过这种做法很不安全,建议还是完全随机的文件名。
------解决方案--------------------
探讨
$file=$_FILES["表单名字"]["name"];
if(file_exists($file))
echo 'exist';
else
move_uploaded_file()........