上傳檔案判斷檔案名稱是否重複
來源:互聯網
上載者:User
上傳檔案判斷檔案名稱是否重複請指教
上傳檔案時候如何判斷 檔案名稱重複?
上傳檔案我知道
$_FILES["表單名字"]
$_FILES["表單名字"]["name"]名字
如何擷取上傳路徑文檔裡面的圖片名字?
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(){
//如何判斷上傳的檔案名稱與已上傳的檔案名稱重複
}
}
?>
include("UpFile.php");
$upfile = new UpFile($_FILES["upfile"]);
?>
------解決方案--------------------
$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()........