How can multiple uploaded files be given unique names using PHP?
When multiple files are uploaded using PHP, it is important to ensure that each file has a unique name to prevent overwriting. One way to achieve this is by appending a timestamp or a unique identifier to the file name before saving it.
<?php
// Loop through each uploaded file
foreach ($_FILES['file']['name'] as $key => $name) {
$unique_name = time() . '_' . $name; // Append timestamp to the original file name
$target_path = "uploads/" . $unique_name; // Specify the target directory
move_uploaded_file($_FILES['file']['tmp_name'][$key], $target_path); // Move the uploaded file to the target directory
}
?>
Keywords
Related Questions
- How can one ensure that the HTTP_REFERER value is not spoofed or manipulated by users?
- What are potential size restrictions for POST requests in PHP and how can data be efficiently compressed, such as with XML and zip?
- What are the common pitfalls to avoid when populating a dropdown menu in HTML with dynamic data from a MySQL database using PHP?