In PHP, how can the naming convention for uploaded files be customized based on user input, such as specifying different names for each file upload field?
When uploading files in PHP, the naming convention for the uploaded files is usually determined by the server or a default setting. However, if you want to customize the naming convention based on user input, such as specifying different names for each file upload field, you can achieve this by using the $_FILES array and renaming the uploaded file before moving it to the desired location.
// Get the user input for the file names
$fileNames = $_POST['file_names'];
// Loop through each uploaded file
foreach ($_FILES['file_upload']['name'] as $key => $name) {
$newFileName = $fileNames[$key]; // Get the custom file name from user input
$tempFile = $_FILES['file_upload']['tmp_name'][$key];
$targetFile = 'uploads/' . $newFileName;
// Move the uploaded file to the desired location with the custom name
move_uploaded_file($tempFile, $targetFile);
}