I have a small piece of code that will get the request input file from the form and will move it to a folder. Here it is:
$destinationPath = 'uploads';
$filename = $file->getClientOriginalName();
$upload_success = $file->move($destinationPath, $filename);
Yes, the code above works but then what I wanted to do is each time I upload an image, it will have a unique name so it wouldn't overwrite any picture in the folder. For now this is what I did:
function generateRandomString($length = 8) {
$characters = '0123456789abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ';
$charactersLength = strlen($characters);
$randomString = '';
for ($i = 0; $i < $length; $i++) {
$randomString .= $characters[rand(0, $charactersLength - 1)];
}
return $randomString;
}
$destinationPath = 'uploads';
$rand = md5(generateRandomString());
$filename = $rand."_".$file->getClientOriginalName();
$upload_success = $file->move($destinationPath, $filename);
This will output something like 7c724458520a11c68747793c86554127_Jellyfish.jpg
but it looks untidy. Is there a trick for this? Thank you.