What is the best practice for organizing uploaded files in PHP to mimic URLs like Mediafire.com?

When organizing uploaded files in PHP to mimic URLs like Mediafire.com, it is best to create a unique identifier for each file and store the files in a structured directory hierarchy. This unique identifier can be generated using functions like md5 or uniqid to ensure uniqueness. By organizing files in this way, you can easily retrieve and serve files based on their unique identifiers, similar to how Mediafire.com handles file storage.

// Generate a unique identifier for the uploaded file
$unique_id = md5(uniqid());

// Define the directory structure for storing files
$first_level = substr($unique_id, 0, 2);
$second_level = substr($unique_id, 2, 2);

// Create the directory structure if it doesn't exist
$directory = "uploads/$first_level/$second_level/";
if (!file_exists($directory)) {
    mkdir($directory, 0777, true);
}

// Move the uploaded file to the generated directory
move_uploaded_file($_FILES["file"]["tmp_name"], $directory . $unique_id . "_" . $_FILES["file"]["name"]);

// Now you can access the file using its unique identifier
$file_url = "https://example.com/uploads/$first_level/$second_level/$unique_id_" . $_FILES["file"]["name"];