How can timestamps be used in PHP to prevent file name conflicts during uploads?
When multiple users upload files simultaneously, there is a possibility of file name conflicts if they upload files with the same name. One way to prevent this is by using timestamps in the file names. By appending a timestamp to the file name before uploading, each file will have a unique name based on the time it was uploaded, thus avoiding conflicts.
// Generate a unique file name using timestamp
$timestamp = time();
$file_name = $timestamp . '_' . $_FILES['file']['name'];
// Upload the file with the unique name
move_uploaded_file($_FILES['file']['tmp_name'], 'uploads/' . $file_name);
Keywords
Related Questions
- What are potential pitfalls when using multiple OR conditions in a MySQL query within PHP?
- How can the "headers already sent" error be resolved when including the DOCTYPE declaration in a PHP file?
- Why does the function chunk_split(base64_encode($Dateiinhalt)) need to be included in the code for sending email attachments in PHP?