Pergunta

I want to use the output of a SHA hash to generate a filename. Any recommended way to do that? I tried Base64 encoding, but for some input that results in the filename containing forward slashes. Obviously I would prefer a method whose output will never contain characters reserved by file systems. Converting each byte to a two-digit hex number would work, but something that produces shorter output would be preferable.

Foi útil?

Solução 2

You can use this

    function getfilename(){

   mt_srand((double)microtime()*10000);
   $token=mt_rand(1, mt_getrandmax());
   $uid=uniqid(md5($token),true);   
   if($uid!=false && $uid!='' && $uid !=NULL){ return $filename =sha1($uid);}   
    }

    //create file name
    $filename=getfilename();
    $filename = substr($filename, 0, 10);

The above code uses the current system time to generate the File Name and also uses mt_rand and MD5 so as to create a unique file name every time the code is run. The final filename is 10 characters, and you can adjust it to whatever number of characters you want.

Outras dicas

You could use Base32

It only uses alpha and numeric characters.

Licenciado em: CC-BY-SA com atribuição
Não afiliado a StackOverflow
scroll top