PHP Ferrari
PHP Ferrari

Reputation: 15646

PHP: Find total number of images having same name in a folder

I am creating code to upload images in a folder using PHP, & before upload an image I check that if any image with same name already exists in the folder by using below syntax:

if (file_exists('profilephoto/' . 'frame.gif')) {
}

But actually I don't want to restrict the user to upload more images with same name & obviously it is impossible to save two images with same name , but there is a way to just find total number of images with same name & upload the next image with appending total number + 1 with image name.

Now I just want to know that using PHP how could we find total number of images having same name in a folder?

Upvotes: 0

Views: 301

Answers (1)

Matthew
Matthew

Reputation: 48304

<?php
$i = '';
while ( file_exists($filename = ('profilephoto/' . "frame$i.gif")) )
  ++$i;

echo $filename;
?>

Obviously it's subject to "race conditions," but that's probably the least of your concerns.

Upvotes: 1

Related Questions