How do you generate random names for files in php7? random_bytes
us returning symbols like _шИ ¶.png
What other function can I use?
Edit: I guess I could use uniqueid() but isn't it a bit old?
How do you generate random names for files in php7? random_bytes
us returning symbols like _шИ ¶.png
What other function can I use?
Edit: I guess I could use uniqueid() but isn't it a bit old?
You can modify random_bytes
's output to limit it to the printable character range.
Example:
// Ascii 126=~, 32=[space], so the lower ASCII printable block
define('RANDOMIZVI_RANGE_DEFAULT', 126-32);
function randomizvi(int $length, $range = RANDOMIZVI_RANGE_DEFAULT){
$bytes = random_bytes($length);
for($i = 0;$i < $length;$i++){
$bytes[$i] = chr((ord($bytes[$i]) % $range) + 32);
}
return $bytes;
}
It's kind of a crude solution, but it works. Note that this particular implementation will be biased slightly towards certain characters.
If you don't really need random filenames, it might be simpler to just number them, which avoids possibilities of collisions.