Generate random file names File upload

Information for PHP. Hypertext Preprocessor
Post Reply
dzcadii
Posts: 94
Joined: Sun Mar 01, 2015 4:36 am

Generate random file names File upload

Post by dzcadii » Sun Mar 01, 2015 6:29 pm

Here is a little snippet if you want to generate random file names from move_uploaded_file();

Code: Select all

<?
// Generate a 32 bit file name with md5();
$File_Name = $_FILES['file_to_upload']['name'];
$Ext = substr($File_name,0,strlen($File_Name)-3);

$new_file_name = md5($File_Name . time()) . $Ext;
?>

Code: Select all

<?
// Generate random file name and leave X amount of characters from the original file name in it;
$File_Name = $_FILES['file_to_upload']['name'];
$Ext = substr($File_name,0,strlen($File_Name)-3);

$X = 5;
$new_file_name = substr($File_Name,0,$X) . md5($File_Name . time()) . $Ext;
?>

Post Reply