Tuesday, October 18, 2011

Function for checking if file exists and if so returning a random file name

$filename = fileExists(FILE_PATH.$_FILES['photo_file']['name']);

function fileExists($file){
  
    if(!file_exists($file)){
        $path_parts = pathinfo($file);
        $filename = $path_parts['filename'].".".$path_parts['extension'];
        return $filename;
    }else{
        $path_parts = pathinfo($file);
        $filename = rand(100,1000).$path_parts['filename'].".".$path_parts['extension'];
        $file = $path_parts['dirname']."/".$filename;
        return fileExists($file);
    }
}