Sunday, June 7, 2009

Rename file if exists in PHP

function rename_if_exists($dir, $filename) {
    $ext = strrchr($filename, '.');
    $prefix = substr($filename, 0, -strlen($ext));
    $i = 0;
    while(file_exists($dir . $filename)) { // If file exists, add a number to it.
        $filename = $prefix . ++$i . $ext;
    }

    return $filename;
}

No comments:

Post a Comment