Image

Imageholographic wrote in Imagephp

ok, it's late and the answer to this is probably stupidly obvious, but I'm asking anyway

I'm fiddling around with a file upload script, and I want it to rename files rather than overwrite them.
so if I upload 'file.txt' and that file already exists, the new file should be called 'file1.txt', and so on.

function new_filename($file_name) {
	$name = explode(".", $file_name, 2);
	$i = 1;
	while(file_exists($name[0].$i.".".$name[1])) {
		$i++;
	}
	$newname = $name[0].$i.".".$name[1];
	return $newname;

}


that's working ok to a point. However, if both file.txt and file1.txt already exist, rather than name the new file 'file2.txt' it overwrites file1.txt

So yeah, I can't figure out why it's not working properly. suggestions? apart from that I either need sleep or more caffiene right now...