PHP File Related Functions

1. How to determine the extension of file  ?


We can easily determine the extension of file using any of these  simple Function
1.
// argument : file name ($_FILE[name of file][name])
// Return extention including -  (.ext)
 function  extension($basename)
{
    $pos=strrpos($basename,".");
    return substr($basename,$pos);
}
2. 
 // Return extention -  (ext)
  function extension($basename)
{
    $filename = strtolower($basename) ;
    $exts = split("[/\\.]", $basename) ;
     $n = count($exts)-1;
    $exts = $exts[$n];
    return $exts; 
}


 3.
// Return extention -  (ext) 
function extension($basename)
  {
     $arr=explode(".",$basename);
     $arr_element=sizeof($arr) ;
     return  $arr[$arr_element-1];


  }


* * Note : Please verify file type , before uploading; to prevent malicious file upload . Please use mime_content_type($filename.ext)  , it will return the mime-type of file like image/gif,text/plain etc ...

2. Unlink all files and folder in a particular folder

Basic unlink is to delete a file physically  . The syntax is unlink(file path including file name) .
But if you want  to delete all files and folder of a perticular folder then please use these code 

// arguments : The target directory  , delete directory or leave it blank (true/false) 
// return : none
function removeAll ($dir, $delDir) {
    if(!$dh = @opendir($dir)) return;
    while (false !== ($obj = readdir($dh))) {
        if($obj=='.' || $obj=='..') continue;
        if (!@unlink($dir.'/'.$obj))
removeAll ($dir.'/'.$obj, true);
    }
    if ($
delDir){
        closedir($dh);
        @rmdir($dir);
    }
}
** Please change folder permission (0777) before excuting the code 

Archive

Enter your email address:

Delivered by FeedBurner

Stat Counter