我创建了一个脚本,允许管理员将徽标上传到选定的目录。我只想在这个目录中存在一个图像,因为它是网站品牌部分的标志。我怎样才能这样做,每次上传图像时它首先清理目录或替换已经存在的图像?
这是我的代码:
<?php
if (!file_exists("". $_SERVER['DOCUMENT_ROOT'] ."/uploads/". $_GET['distributor'] ."/Logo/")) {
mkdir("". $_SERVER['DOCUMENT_ROOT'] ."/uploads/". $_GET['distributor'] ."/Logo/", 0777, true);
}
$target_dir = "". $_SERVER['DOCUMENT_ROOT'] ."/uploads/". $_GET['distributor'] ."/Logo/";
$target_file = $target_dir . basename($_FILES["fileToUpload"]["name"]);
$uploadOk = 1;
$imageFileType = pathinfo($target_file,PATHINFO_EXTENSION);
if(isset($_POST["submit"])) {
$check = getimagesize($_FILES["fileToUpload"]["tmp_name"]);
if($check !== false) {
$uploadOk = 1;
}
else {
$uploadOk = 0;
}
}
if (file_exists($target_file)) {
$uploadOk = 0;
}
if($imageFileType != "jpg" && $imageFileType != "png" && $imageFileType != "jpeg" && $imageFileType != "gif") {
$uploadOk = 0;
}
else {
if (move_uploaded_file($_FILES["fileToUpload"]["tmp_name"], $target_file)) {
header('location: ../edit-distributor.php?distributor='. $_GET['distributor'] .'&id='. $_GET['id'] .'');
exit();
}
}
?>