note 119556 deleted from function.file-exists by tularis
| From: | tularis@php.net | Date: | Tue, 05 Jul 2016 17:27:28 +0000 |
| Subject: | note 119556 deleted from function.file-exists by tularis | ||
| References: | 1 | Groups: | php.notes |
| Request: | Send a blank email to php-notes+get-205897@lists.php.net to get a copy of this message | ||
Note Submitter: YAPs
----
Search free Filename
<?PHP
function find_name($file,$dem="~",$back=false)
{
if(file_exists($file))
{
$i=pathinfo($file);
$e=$i["extension"];
$b=str_replace(DS.DS,DS,dirname($file).DS.basename($file,".".$e));
$s=0;
$m="";
$u=explode($dem,$b);
while(file_exists($u[0].$m.".".$e))
{
$s++;
$m=$dem.$s;
}
if($back==true)
{
rename($file,$u[0].$m.".".$e);
return $file;
}
return $u[0].$m.".".$e;
}
else
return $file;
}
?>
$a=find_name("dir/dir2/file.txt");
Result:
if such a file exists - returns: $a="dir/dir2/file~0.txt"
else no file exists - returns: $a="dir/dir2/file.txt"
or
$a=find_name("dir/dir2/file.txt","~",true);
Result:
if file exists - returns: $a="dir/dir2/file.txt" and old file save
"dir/dir2/file~0[1..n].txt"
else no file exists - returns $a="dir/dir2/file.txt"