When uploading files to a directory using PHP's move_uploaded_file() function, the file's name remains the same as its original name. This can be undesirable when dealing with multiple files with similar names or if you wish to enforce a specific naming convention.
To rename an uploaded file before saving it, modify the second parameter of move_uploaded_file(). This parameter specifies the destination path and filename. Here's how you can change it:
$newfilename = round(microtime(true)) . '.' . end($temp); move_uploaded_file($_FILES["file"]["tmp_name"], "../img/imageDirectory/" . $newfilename);
In this example:
This approach ensures that uploaded files receive a unique random name while retaining their original extension.
The above is the detailed content of How Can I Rename Uploaded Files in PHP Before Saving Them?. For more information, please follow other related articles on the PHP Chinese website!