PDA

View Full Version : append filename



Tabo
05-16-2007, 05:54 PM
Hey, i have a page where users upload images, everything fine. However if a file with the same name is uploaded i just


echo 'An image with that name already exists, please change the name and upload it again';

However i would prefer it if the name appends, for example if FILE.gif exists it renames the image that is being uploaded to FILE1.gif and so on.

Thanks.

Twey
05-16-2007, 06:44 PM
Something like:
if(file_exists($imagename)) {
while(file_exists($imagename . @++$i));
$imagename .= $i;
}

mburt
05-16-2007, 07:05 PM
Of course, adapting that would include something like:

fwrite(fopen($file,$handle),$filename . $imagename);
Depending on how you write the files.

Twey
05-16-2007, 07:13 PM
No it wouldn't... I'm assuming $imagename is the name of the file to write to. It's a cut-and-paste job.

mburt
05-16-2007, 07:19 PM
Wouldn't $imagename return the number?

Twey
05-16-2007, 07:42 PM
$imagename .= $i;Note the dot.