How can the md5() function be used in the context of uploading images in PHP?
When uploading images in PHP, it's important to ensure that each image has a unique filename to prevent overwriting existing files. One way to achieve this is by using the md5() function to generate a unique hash based on the image file's content. This hash can then be used as part of the filename to guarantee uniqueness.
// Get the uploaded file
$uploadedFile = $_FILES['image']['tmp_name'];
// Generate a unique filename using md5 hash
$uniqueFilename = md5_file($uploadedFile) . '.jpg';
// Move the uploaded file to a designated directory with the unique filename
move_uploaded_file($uploadedFile, 'uploads/' . $uniqueFilename);