[code=php] /** * @name getFileName * @desc This function checks if a file already exists, if so it adds _# at the back. * * @author Koen Van den Wijngaert * @version 1.0 * @param string $sUploadDir The directory the file is located * @param string $sName The filename * @param bool[optional] $bReplace Determines whether the file should be overwritten or not. [Default: false] * * @return The name of the image after the check. */ function getFileName($sUploadDir, $sName, $bReplace = false) { if($bReplace === true) { return $sName; } if(!file_exists($sUploadDir . $sName)) { return $sName; } $aFile = explode('.', $sName); $sExt = array_pop($aFile); $sFileName = implode('.', $aFile); for($i = 1; $i <= 255; $i++) { if(!file_exists($sUploadDir . $sFileName . '_'.$i.'.' . $sExt)) { $sPath = $sFileName . '_'.$i.'.' . $sExt; break; } } return $sPath; }