سؤال

i need to change variable data if the 'data name' already exists..

i have this

if (file_exists('../../images/produtos/'.$name)){
        $name = '1_'.$name;
    }

it is working, if i have a file with the name that is saved in the variable $name, it create a new one with a 1_ before, but i need to check if there is a 1_.$name, 2_$name ...... until there is no file with the name saved in variable $name.

If i upload a file with name things.png i need to check if there is already a file called things.png, if there is then change a name to 1_things.png, but if there is a file called 1_things.png change the name to 2_things.png etc etc until there is no file with the same name.

PS: I dont want overwrite obv..

Im sorry for my bad english, but i really dont know how to explain this better, hope you guys understand.

Thanks in advance

هل كانت مفيدة؟

المحلول

Use a loop:

if(file_exists('../../images/produtos/'.$name)){
$i = 1;
    while(file_exists('../../images/produtos/'.$i."_".$name)){
    $i++;
    }
$name = $i."_".$name;
}
مرخصة بموجب: CC-BY-SA مع الإسناد
لا تنتمي إلى StackOverflow
scroll top