php

php : utility functions


1 Getting the file name from full address.
  $filesample = ‘http://example.com/path/file.php’;
//This gets the file extension
echo $ext = strrchr($filesample,’/’),'<br>’;
echo substr(strrchr($filesample,’/’), 1 );
   
2 Reading everything within folder
 
  1. $dir = “/etc/php5/*”;
  2. // Open a known directory, and proceed to read its contents
  3. foreach(glob($dir) as $file)
  4. {
  5.     echo “filename: $file : filetype: ” . filetype($file) . “<br />”;
  6. }

 

3 Reading file property
  <?php
$path_parts = pathinfo(‘/www/htdocs/inc/lib.inc.php’);

echo $path_parts[‘dirname’], “\n”;
echo $path_parts[‘basename’], “\n”;
echo $path_parts[‘extension’], “\n”;
echo $path_parts[‘filename’], “\n”; // since PHP 5.2.0
?>

4 Reading the directory name
  <?php

//before PHP 4.3.0
dirname(‘c:/’); // returned ‘.’

//after PHP 4.3.0
dirname(‘c:/x’); // returns ‘c:\’
dirname(‘c:/Temp/x’); // returns ‘c:/Temp’
dirname(‘/x’); // returns ‘\’

?>

 

Advertisements

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s