简体   繁体   中英

How to get unique file name on every http request in php

I have to create new file on every http request and I want file name should be unique so that no file overwrite other files

first you need to create function to generate an unique value. here is the code for that function.

function gen_uuid() {
    return sprintf( '%04x%04x-%04x-%04x-%04x-%04x%04x%04x',
        mt_rand( 0, 0xffff ), mt_rand( 0, 0xffff ),
        mt_rand( 0, 0xffff ),
        mt_rand( 0, 0x0fff ) | 0x4000,
        mt_rand( 0, 0x3fff ) | 0x8000,
        mt_rand( 0, 0xffff ), mt_rand( 0, 0xffff ), mt_rand( 0, 0xffff )
    );
}

Now call the above function for unique name. suppose your file extension should be .txt . then-

$ext = '.txt';
$fileName = gen_uuid().$ext;

that will gives you an unique name every time. you can also use the same function for generating UUID .

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM