Something like this will work:
<?php
$file = $_FILES['file']['name'];
$a = pathinfo($file);
$basename = $a['basename'];
$filename = $a['filename'];
$extension = $a['extension'];
$path = $_SERVER['DOCUMENT_ROOT'].'/up/images/'; # set upload directory
$dest = $path.$basename;
if(file_exists($dest))
{
$b = count(glob($path.$filename.'*.'.$extension))+1; # all matches + 1
for($i = 1; $i < $b; $i++)
{
if(!file_exists($path.$filename.$i.'.'.$extension))
{
move_uploaded_file($_FILES['file']['tmp_name'],$path.$filename.$i.'.'.$extension);
}
}
}
else
{
move_uploaded_file($_FILES['file']['tmp_name'],$dest);
}
?>
If the file exists then the script does a glob() in the destination directory in search of the filename (without the number) and counts all the matches, we add 1 to the result and go to loop until a free combination is find. This is just an interpretation of a function used in CodeIgniter framework that you can find in System/Libraries/Upload.php at line 390~ the difference is that in CI that function is limitated to 100 loops. Bye.
cereal
Veteran Poster
1,146 posts since Aug 2007
Reputation Points: 344
Solved Threads: 223
Skill Endorsements: 22
You're welcome, if your problem is solved please mark the thread as solved, bye :)
cereal
Veteran Poster
1,146 posts since Aug 2007
Reputation Points: 344
Solved Threads: 223
Skill Endorsements: 22
Question Answered as of 1 Year Ago by
cereal