How to upload file with unique name in php

This is the example to make unique file name with the help of random characters and upload the file............


<?php
//****** Function for generate random character *****************************/
function randomGenerator($strlength)
{
$str="ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789";
$strshuf=str_shuffle($str);
$start=rand(0,strlen($str) - $strlength);
return substr(str_shuffle($strshuf),$start,$strlength);
}
$path_thumbs = "x:/projectname/uploads/"; //Upload Path
$uploadfilename = "Attachfile"; //File name submitted by form
$code = randomGenerator(5); //Generate random character
$fileParts = pathinfo($_FILES[$uploadfilename]['name']);
$file_name_only = stripslashes($fileParts['filename']);
$extension = $fileParts['extension'];
$UploadFileName = $file_name_only."_".$code.".$extension"; //Make a new file name
move_uploaded_file($_FILES[$formname]["tmp_name"],$path_thumbs.$UploadFileName); //upload file
?>

Comments

Popular posts from this blog

Login with facebook using coldfusion

Create CSV file in Coldfusion Using CFFile

Get Previous One Day Data in Sql Server