generating a unique file name

PHP programming forum. Ask questions or help people concerning PHP code. Don't understand a function? Need help implementing a class? Don't understand a class? Here is where to ask. Remember to do your homework!

Moderator: General Moderators

Post Reply
hame22
Forum Contributor
Posts: 214
Joined: Wed May 11, 2005 5:50 am

generating a unique file name

Post by hame22 »

Hi

My site allows users to upload a number of images to the web server. What I would like to do is generate a unique filename for each file uploaded so as I don't have the problem of 2 or more users uploading an image with the same name.

Is there a function to achieve this?

Thanks in advance
User avatar
dibyendrah
Forum Contributor
Posts: 491
Joined: Wed Oct 19, 2005 5:14 am
Location: Nepal
Contact:

Post by dibyendrah »

Simple solution

Code: Select all

$unique_output_file_name = md5(microtime()) . ".jpg";
jmut
Forum Regular
Posts: 945
Joined: Tue Jul 05, 2005 3:54 am
Location: Sofia, Bulgaria
Contact:

Post by jmut »

dibyendrah wrote:Simple solution

Code: Select all

$unique_output_file_name = md5(microtime()) . ".jpg";

Code: Select all

$unique_output_file_name = md5(microtime()) . ".jpg";
//just in case 
while (file_exists($unique_output_file_name)) {
    $unique_output_file_name = md5(microtime()) . ".jpg";
}
echo $unique_output_file_name;
Post Reply