How can PHP be used to prevent files from being overwritten during upload?
To prevent files from being overwritten during upload in PHP, one approach is to generate a unique filename for each uploaded file. This can be achieved by appending a timestamp or a random string to the original filename before saving it to the server.
// Generate a unique filename by appending a timestamp to the original filename
$uploadedFile = $_FILES['file']['name'];
$extension = pathinfo($uploadedFile, PATHINFO_EXTENSION);
$uniqueFilename = time() . '_' . uniqid() . '.' . $extension;
// Move the uploaded file to a specified directory with the unique filename
move_uploaded_file($_FILES['file']['tmp_name'], 'uploads/' . $uniqueFilename);
Related Questions
- What are some best practices for comparing and evaluating different PHP frameworks for a specific project, such as a browser game?
- What are the alternative methods to SOAP for website communication in PHP, such as fopen, file_get_contents, or cURL?
- What is the significance of the error "Use of undefined constant name" in PHP?