add folders uploading
This commit is contained in:
39
upload.php
39
upload.php
@@ -49,10 +49,45 @@ foreach ($_FILES["file"]["name"] as $index => $fileName) {
|
||||
exit;
|
||||
}
|
||||
|
||||
// --- Minimal Folder/Subfolder Logic ---
|
||||
// Check if a relativePath was provided (from a folder upload)
|
||||
$relativePath = '';
|
||||
if (isset($_POST['relativePath'])) {
|
||||
// In case of multiple files, relativePath may be an array.
|
||||
if (is_array($_POST['relativePath'])) {
|
||||
$relativePath = $_POST['relativePath'][$index] ?? '';
|
||||
} else {
|
||||
$relativePath = $_POST['relativePath'];
|
||||
}
|
||||
}
|
||||
if (!empty($relativePath)) {
|
||||
// Extract the directory part from the relative path.
|
||||
$subDir = dirname($relativePath);
|
||||
if ($subDir !== '.' && $subDir !== '') {
|
||||
// If uploading to root, don't add the "root" folder in the path.
|
||||
if ($folder === 'root') {
|
||||
$uploadDir = rtrim(UPLOAD_DIR, '/\\') . DIRECTORY_SEPARATOR . $subDir . DIRECTORY_SEPARATOR;
|
||||
} else {
|
||||
$uploadDir = rtrim(UPLOAD_DIR, '/\\') . DIRECTORY_SEPARATOR . $folder . DIRECTORY_SEPARATOR . $subDir . DIRECTORY_SEPARATOR;
|
||||
}
|
||||
if (!is_dir($uploadDir)) {
|
||||
mkdir($uploadDir, 0775, true);
|
||||
}
|
||||
// Use the basename from the relative path.
|
||||
$safeFileName = basename($relativePath);
|
||||
}
|
||||
}
|
||||
// --- End Minimal Folder/Subfolder Logic ---
|
||||
|
||||
$targetPath = $uploadDir . $safeFileName;
|
||||
|
||||
if (move_uploaded_file($_FILES["file"]["tmp_name"][$index], $targetPath)) {
|
||||
// Build the metadata key, including the folder if not in root.
|
||||
$metaKey = ($folder !== 'root') ? $folder . "/" . $safeFileName : $safeFileName;
|
||||
// Build the metadata key.
|
||||
if (!empty($relativePath)) {
|
||||
$metaKey = ($folder !== 'root') ? $folder . "/" . $relativePath : $relativePath;
|
||||
} else {
|
||||
$metaKey = ($folder !== 'root') ? $folder . "/" . $safeFileName : $safeFileName;
|
||||
}
|
||||
if (!isset($metadata[$metaKey])) {
|
||||
$uploadedDate = date(DATE_TIME_FORMAT);
|
||||
$uploader = $_SESSION['username'] ?? "Unknown";
|
||||
|
||||
Reference in New Issue
Block a user