Add random characters to end of file path
I have this
$resizeObj -> saveImage('images/'.$pic, 90);
and I want to append random characters at the end of file name in order to prevent duplicate names 开发者_StackOverflow社区using somethign like this:
function genRandomString() {
$length = 10;
$characters = ’0123456789abcdefghijklmnopqrstuvwxyz’;
$string = ”;
for ($p = 0; $p < $length; $p++) {
$string .= $characters[mt_rand(0, strlen($characters))];
}
return $string;
}
Whta is the best way to incorporate this into $resizeObj
$pathinfo = pathinfo($pic);
$pic = sprintf("%s%s.%s", $pathinfo['basename'], getRandomString(), $pathinfo['extension']);
$resizeObj -> saveImage('images/'.$pic, 90);
You should not save random string. Just append a unique string (eg. uniqid()) when you present the image.
精彩评论