View Single Post
Old 10-25-2007, 06:22 PM   #2 (permalink)
Wildhoney
La Vida es Sueño
Advanced Programmer Top Contributor 
 
Wildhoney's Avatar
 
Join Date: Sep 2007
Location: Oldham
Posts: 2,280
Thanks: 90
Wildhoney is on a distinguished road
Default

This function will count up until it finds an empty slot. Good if you want to keep your images in order, else just generate a random file name.

Basically:

Input image: myLogo.jpg
If myLogo.jpg exists change to myLogo.2.jpg
If myLogo.2.jpg doesn't exist, return myLogo.2.jpg

PHP Code:
define('DIRECTORY''./images/');

function 
getImageName($szImage)
{
    
$iIndex 2;
    
    if(!
file_exists(DIRECTORY $szImage))
    {
        return 
$szImage;
    }
    
    
$aParts explode('.'$szImage);
    
    if(
count($aParts) <= 1)
    {
        return 
$szImage;
    }
    
    
$szExt end($aParts);
    
$iCount count($aParts);
    
    
$aParts[$iCount 1] = $iIndex;
    
$aParts[$iCount] = $szExt;
    
    do { 
$aParts[count($aParts) - 2] = $iIndex++; }
    while(
file_exists(DIRECTORY implode('.'$aParts)));
    
    return 
implode('.'$aParts);

__________________
The man who comes back through the Door in the Wall will never be quite the same as the man who went out.
Send a message via AIM to Wildhoney Send a message via MSN to Wildhoney Send a message via Yahoo to Wildhoney
Wildhoney is offline  
Reply With Quote