当我想上传图片并创建不存在的新目录时遇到问题,它不会创建文件夹 $gallery_name,它将图片存储在 uploads/galerije/$username 而不是 uploads/galerije/$username/$gallery_name 中。谁能帮我?
$username = $_SESSION['username'];
$gallery_name = $_POST['gallery_name'];
if (isset($_FILES['files'])) {
$errors = array();
foreach ( $_FILES['files']['tmp_name'] as $key => $tmp_name ) {
$file_name = $key . $_FILES['files']['name'][$key];
$file_size = $_FILES['files']['size'][$key];
$file_tmp = $_FILES['files']['tmp_name'][$key];
$file_type = $_FILES['files']['type'][$key];
if ($file_size > 2097152) {
$errors[] = 'File size must be less than 2 MB';
}
$desired_dir = "uploads/galerije/" . $username . "/" . $gallery_name;
if (empty($errors)) {
if (!is_dir($desired_dir)) {
mkdir($desired_dir, 0777); // Create directory if it does not exist
}
if (!is_dir("$desired_dir/" . $file_name) == false) {
move_uploaded_file($file_tmp, "$desired_dir/" . $file_name);
header("location: index.php");
}
}
else {
print_r($errors);
}
}
if (empty($error)) {
echo "Success";
}
}