0

I adopted code from https://stackoverflow.com/a/44553006/8719001

but can't figure out why when uploading the same file "test.jpg" several times it only counts up once, creating "test-1.jpg" but not more ie. test-2.jpg, test-3.jpg.

Can anybody spot the issue and help please?

 $keepFilesSeperator = "-";
 $keepFilesNumberStart = 1;

if (isset($_FILES['upload'])) {
        // Be careful about all the data that it's sent!!!
        // Check that the user is authenticated, that the file isn't too big,
        // that it matches the kind of allowed resources...
        $name = $_FILES['upload']['name'];
        //If overwriteFiles is true, files will be overwritten automatically.
        if(!$overwriteFiles)
        {
    $ext = ".".pathinfo($name, PATHINFO_EXTENSION);
            // Check if file exists, if it does loop through numbers until it doesn't.
            // reassign name at the end, if it does exist.
    if(file_exists($basePath.$name))
            {
                    $operator = $keepFilesNumberStart;

                //loop until file does not exist, every loop changes the operator to a different value.
            while(file_exists($basePath.$name.$keepFilesSeperator.$operator))
                {
                        $operator++;
               }
               $name = rtrim($name, $ext).$keepFilesSeperator.$operator.$ext;
            }
        }
        move_uploaded_file($_FILES["upload"]["tmp_name"], $basePath . $name);
    }
Markus
  • 458
  • 4
  • 16
  • don't you redefine the variable $operator to 1 for every file? I mean, can you upload only 1 file or 2 (test-1.jpg and test-2.jpg)? – I-V Mar 20 '18 at 19:43
  • `file_exists($basePath.$name.$keepFilesSeperator.$operator)` should probably be `file_exists($basePath.rtrim($name, $ext).$keepFilesSeperator.$operator.$ext)` since that's where you're moving them to. – Jonnix Mar 20 '18 at 20:01
  • @I-V operator starts with 1 and counts up with ++ within the while loop. The logic check is for each individual upload. – Markus Mar 21 '18 at 08:48
  • @JonStirling thanks for posting the actual code, which worked! I suspected it's it's not being triggered but wasn't sure why, since I had the rtrim after the loop but realised it's still needed there for actually writing out the new file name :). – Markus Mar 21 '18 at 08:48

1 Answers1

1

your while loop condition has a problem

while( file_exists( $basePath.$name.$keepFilesSeperator.$operator ) )

the $name variable still contains the full name of file, in this case test.jpg, you're testing a value like /home/test.jpg-1 so finally the while loop is never executed as the file test.jpg-1 never exists, that's why you always get the test-1.jpg on disk and not a ...-2.jpg or ...-3.jpg

Jonnathan Q
  • 312
  • 1
  • 8
  • Thanks Jonnathan for pointing out the issue, I suspected it was never triggered but didn't realise i needed rtrim in the loop as well. Jon posted the code above in the comments which worked :) – Markus Mar 21 '18 at 08:50