3

is there any pretty solution in PHP which allows me to expand filename with an auto-increment number if the filename already exists? I dont want to rename the uploaded files in some unreadable stuff. So i thought it would be nice like this: (all image files are allowed.)

Cover.png
Cover (1).png
Cover (2).png
…
4
  • using file_exists() function you can achieve this very simply Commented Dec 29, 2012 at 12:05
  • But file_exists delivers me only true or false… Commented Dec 29, 2012 at 12:08
  • 1
    ya,you need to take care of further stuff,write simple algorithm first then implement it in coding,it makes easier to you Commented Dec 29, 2012 at 12:10
  • "simple algorithm" ? ja, basically i know what do you mean but ive no idea how to resolve that. :( Commented Dec 29, 2012 at 12:13

4 Answers 4

6

First, let's separate extension and filename:

$file=pathinfo(<your file>);

For easier file check and appending, save filename into new variable:

$filename=$file['filename'];

Then, let's check if file already exists and save new filename until it doesn't:

$i=1;
while(file_exists($filename.".".$file['extension'])){
 $filename=$file['filename']." ($i)";
 $i++;
}

Here you go, you have a original file with your <append something> that doesn't exist yet.

EDIT: Added auto increment number.

Sign up to request clarification or add additional context in comments.

3 Comments

pretty easy!!! :) but the auto increment solution is better i think. But thanks!!!
@Salman A: In what way? I double checked and I don't see anything wrong.
I mean what does $filename." ($i)"; do? I don't see any assignment.
2

Got it:

if (preg_match('/(^.*?)+(?:\((\d+)\))?(\.(?:\w){0,3}$)/si', $FILE_NAME, $regs)) {
        $filename = $regs[1];
        $copies = (int)$regs[2];
        $fileext = $regs[3];

        $fullfile = $FILE_DIRECTORY.$FILE_NAME;
        while(file_exists($fullfile) && !is_dir($fullfile))
        {
                $copies = $copies+1;
                $FILE_NAME = $filename."(".$copies.")".$fileext;
                $fullfile = $FILE_DIRECTORY.$FILE_NAME;
        }
}
return $FILE_NAME;

1 Comment

You could use pathinfo instead of regex; much simpler this way.
0

You can use this function below to get unique name for uploading

function get_unique_file_name($path, $filename) {
    $file_parts = explode(".", $filename);
    $ext = array_pop($file_parts);
    $name = implode(".", $file_parts);

    $i = 1;
    while (file_exists($path . $filename)) {
        $filename = $name . '-' . ($i++) . '.' . $ext;
    }
    return $filename;
}

Use that function as

$path = __DIR__ . '/tmp/';
$fileInput = 'userfile';

$filename = $path . 
    get_unique_file_name($path, basename($_FILES[$fileInput]['name']));

if (move_uploaded_file($_FILES[$fileInput]['tmp_name'], $filename)) {
    return $filename;
}

You can get working script here at github page

Comments

-2

Use file_exists() function and rename() function to achieve what you're trying to do!

1 Comment

Do i need any regex for that? Sure i can check if the original filename already exist but what if i am trying to upload another Cover.png. How can i recognize how many are there already?

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.