What are some alternative methods or approaches to implementing a file upload feature with user-selected storage locations in PHP?

When implementing a file upload feature in PHP, allowing users to select their own storage locations can be a security risk as it may expose sensitive server directories. To address this, one approach is to limit the storage locations to a predefined set of directories that are safe for file uploads.

<?php
// Define safe storage locations
$allowed_storage_locations = array('/uploads/folder1', '/uploads/folder2');

// Check if the selected storage location is allowed
if (in_array($_POST['storage_location'], $allowed_storage_locations)) {
    $upload_path = $_POST['storage_location'] . '/' . $_FILES['file']['name'];
    
    // Move the uploaded file to the selected storage location
    move_uploaded_file($_FILES['file']['tmp_name'], $upload_path);
    
    echo 'File uploaded successfully to ' . $_POST['storage_location'];
} else {
    echo 'Selected storage location is not allowed';
}
?>