How can the $_FILES array be utilized to assign specific names to individual uploaded files in PHP?
When uploading multiple files using PHP, the $_FILES array contains information about each file, including its temporary location and original name. To assign specific names to individual uploaded files, you can loop through the $_FILES array and move each file to a new location with the desired name.
foreach ($_FILES['upload']['tmp_name'] as $key => $tmp_name) {
$new_name = 'file_' . $key . '.' . pathinfo($_FILES['upload']['name'][$key], PATHINFO_EXTENSION);
$new_location = 'uploads/' . $new_name;
move_uploaded_file($tmp_name, $new_location);
}
Keywords
Related Questions
- Are there any best practices or guidelines to follow when implementing ID-based page access in PHP?
- What are some best practices for securely handling user input in PHP when interacting with a database?
- What potential issue can arise when using multiple database connections in PHP, as seen in the provided code snippets?