I am creating profile form where I am allowing users to store their profile photos. To make sure the names are different and won't be overwritten, I am using microtome function to rename the uploaded file:
$temp = explode(".",$_FILES["fileToUpload"]["name"]);
$newfilename = round(microtime(true)) . '.'. end($temp);
move_uploaded_file($_FILES["fileToUpload"]["tmp_name"],"subsites/uploads/". $newfilename
...but later when I call $newfilename
variable in the same form (to save file path in the database) it values is changed.
<input type="hidden" name="FormPicName" id="FormFileName"
value=<?php echo $_FILES["fileToUpload"]["tmp_name"],"subsites/uploads/". $newfilename?>>
I tried storing $newfilename as variable or session variable, but it still generates different values.
Is there a way to save microtome based variable as constant?
This is a great solution (I use this for my projects)
// get file extension
$ext = pathinfo($_FILES["fileToUpload"]["name"], PATHINFO_EXTENSION);
// make new filename
$newfilename = uniqid('profilepic_').'.'.$ext;
$newfilename will have something like profilepic_4b3403665fea6.jpg