What are best practices for preserving the original file name and extension during file uploads in PHP?
When uploading files in PHP, it is important to preserve the original file name and extension to maintain the integrity of the uploaded file. To achieve this, you can use the $_FILES superglobal array to access the original file name and extension, and then use move_uploaded_file() function to save the file with the original name.
$originalFileName = $_FILES['file']['name'];
$extension = pathinfo($originalFileName, PATHINFO_EXTENSION);
$uploadDirectory = 'uploads/';
$newFileName = $uploadDirectory . $originalFileName;
move_uploaded_file($_FILES['file']['tmp_name'], $newFileName);
Related Questions
- What are the potential pitfalls of attempting to update specific rows in a table using the LIMIT command in MySQL?
- What are the best practices for encoding and decoding URL parameters in PHP to avoid issues with spaces and special characters in titles?
- What are some potential challenges when using cURL for logging in to an external website using PHP?