note 88607 modified in function.file-exists by danbrown
| From: | danbrown@php.net | Date: | Sat, 31 Jan 2009 18:06:43 +0000 |
| Subject: | note 88607 modified in function.file-exists by danbrown | ||
| References: | 1 | Groups: | php.notes |
| Request: | Send a blank email to php-notes+get-150039@lists.php.net to get a copy of this message | ||
The code can be used to t a filename that can be used to create a new filename.
<?php
function generateRandomString($length = 8)
{
$string = "";
//character that can be used
$possible = "0123456789bcdfghjkmnpqrstvwxyz";
for($i=0;$i < $length;$i++)
{
$char = substr($possible, rand(0, strlen($possible)-1), 1);
if (!strstr($string, $char))
{
$string .= $char;
}
}
return $string;
}
function randomFile($folder = '', $extension = '')
{
$folder = trim($folder);
$folder = ($folder == '') ? './' : $folder;
//check if directory exist
if (!is_dir($folder)){ die('invalid folder given!'); }
//generate a filepath
$filepath = $folder . "/" . generateRandomString(128) . $extension;
//check if that filepath already exist, if it exist if generates again
//till if gets one that doesn't exist
while(file_exists($filepath))
{
$filepath = $folder . "/" . generateRandomString(128) . $extension;
}
return $filepath;
}
?>
--was--
The code can be used to t a filename that can be used to create a new filename.
function generateRandomString($length = 8)
{
$string = "";
//character that can be used
$possible = "0123456789bcdfghjkmnpqrstvwxyz";
for($i=0;$i < $length;$i++)
{
$char = substr($possible, rand(0, strlen($possible)-1), 1);
if (!strstr($string, $char))
{
$string .= $char;
}
}
return $string;
}
function randomFile($folder = '', $extension = '')
{
$folder = trim($folder);
$folder = ($folder == '') ? './' : $folder;
//check if directory exist
if (!is_dir($folder)){ die('invalid folder given!'); }
//generate a filepath
$filepath = $folder . "/" . generateRandomString(128) . $extension;
//check if that filepath already exist, if it exist if generates again
//till if gets one that doesn't exist
while(file_exists($filepath))
{
$filepath = $folder . "/" . generateRandomString(128) . $extension;
}
return $filepath;
}
http://php.net/manual/en/function.file-exists.php