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............
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?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
Post a Comment