Friday, July 25, 2008

coding php to extract or get filename and extension

it's not hard to code but not easy to solve
easyiest way is use explode function but this is advance :)

 
  $url = "htpp://www.example.com/filename.txt
  $file = substr(strrchr($url, '/'), 1); //get file
  $ext = substr(strrchr($file, '.'), 1); //get extension
  $filename = substr($file,0,strrpos($file,'.')); // get file name
and another code is function to remove file in folder and/or empty folder
<?php
// ggarciaa at gmail dot com (04-July-2007 01:57)
// I needed to empty a directory, but keeping it
// so I slightly modified the contribution from
// stefano at takys dot it (28-Dec-2005 11:57)
// A short but powerfull recursive function
// that works also if the dirs contain hidden files
// $dir = the target directory
// $DeleteMe = if true delete also $dir, if false leave it alone
function SureRemoveDir($dir, $DeleteMe) {
  if(!$dh = @opendir($dir)) return;
  while (false !== ($obj = readdir($dh))) {
    if($obj=='.' || $obj=='..') continue;
    if (!@unlink($dir.'/'.$obj)) SureRemoveDir($dir.'/'.$obj, true);
  }
  closedir($dh);
  if($DeleteMe){
    @rmdir($dir);
  }
}
//SureRemoveDir('EmptyMe', false);
//SureRemoveDir('RemoveMe', true);
?>
link: http://th.php.net/unlink link2: