如何将文件重命名为随机名称,然后将文件名和扩展名存储在数据库中。我会用它来改变个人资料图片。 我在这里有这个代码: Валидниформати:“jpeg”,“jpg”,“png”';
if(isset($_POST["submit"])) {
$target_dir = "avatars/";
$target_file = $target_dir . basename($_FILES["file"]["name"]);
$uploadOk = 1;
$allowedFiles = array('gif','png' ,'jpg', 'jpeg', 'GIF', 'PNG', 'JPG', 'JPEG');
$imageFileType = pathinfo($target_file,PATHINFO_EXTENSION);
$check = getimagesize($_FILES["file"]["tmp_name"]);
if($check !== false) {
$imageError = "File is an image - " . $check["mime"] . ".";
$uploadOk = 1;
} else {
$imageError = "<span style='color: red;'>Това не е снимка</span>";
$uploadOk = 0;
}
if (file_exists($target_file)) {
$imageError = "<span style='color: red;'>Снимката съществува</span>";
$uploadOk = 0;
}
if ($_FILES["file"]["size"] > 500000) {
$imageError = "<span style='color: red;'>Твърде голям файл</span>";
$uploadOk = 0;
}
/*
if ($imageFileType != "jpg" && $imageFileType != "png" && $imageFileType != "jpeg" && $imageFileType != "gif") {
$imageError = "<span style='color: red;'>Допустими формати: JPG, JPEG, PNG, GIF</span>";
$uploadOk = 0;
}
*/
if (!in_array($imageFileType, $allowedFiles)) {
$imageError = "<span style='color: red;'>Допустими формати: JPG, JPEG, PNG, GIF</span>";
$uploadOk = 0;
}
if ($uploadOk == 1) {
if (move_uploaded_file($_FILES["file"]["tmp_name"], $target_file)) {
$imageError = "<span style='color: red;'>Файлът е качен успешно</span>";
} else {
$imageError = "<span style='color: red;'>Грешка при качване</span>";
}
}
}
?>