PHP Files and Directories
Include a file
- Include local files using the
include()
orrequire()
methods. Both methods are the same except for the fact that require() produces a fatal error if it fails while include() only produces a warning.
include('../my_file.php');
Get the current file path
// Get the absolute path to the directory containing the current working file.
$dir_path = __DIR__; // Recommended fastest way, but only available for PHP >= 5.3.
$dir_path = dirname(__FILE__);
Get parts of a file path
$file_path = '/home/mydir/myfile.pdf';
// Get all the path parts in an array.
$file_path_parts = pathinfo( $file_path );
// Get each part separately
echo pathinfo( $file_path, PATHINFO_DIRNAME); // Returns "/home/mydir"
echo pathinfo( $file_path, PATHINFO_BASENAME); // Returns "myfile.pdf"
echo pathinfo( $file_path, PATHINFO_EXTENSION); // Returns "pdf"
echo pathinfo( $file_path, PATHINFO_FILENAME); // Returns "myfile"
Get a file extension
This is faster than using pathinfo()
or getting the value from array if you just need the ext.
$ext = substr(strrchr($filename, "."), 1);
Get the headers of a remote file
Get the headers from a file hosted at a remote server without downloading the whole file.
Headers contain content-type and file size info.
$media_file_headers = array_change_key_case(get_headers($file_url, TRUE));
Check if a local file exists
If you want to check exists and:
- Is only a file: Use
is_file()
- Is only a dir: Use
is_dir()
- Is file or dir:
Use file_exists()
If you want to check exists and is readable:
- And is only file: Use
is_file()
andis_readable()
- And is only dir: Use
is_dir()
andis_readable()
- And is file or dir: Use
is_readable()
You don’t need to use file_exists()
and is_readable()
together because is_readable()
also checks if file_exists()
.
Pairing is_readable()
with either is_file()
or is_dir()
is only to check if is readable specifically on a file only or specifically on a directory only.
$filename = $_SERVER['DOCUMENT_ROOT'] . 'images/myImage.png';
if (file_exists($filename)) {
echo "The file $filename exists";
} else {
echo "The file $filename does not exist";
}
Parsing Directories
/**
* Converts a nested directory tree into a multidimensional array
* structured to represent the nesting hierarchy of all
* files and directories defined by '$startpath'.
*/
function get_recursive_dir_file_listings( $startpath )
{
$listings_array = array();
$raw_listings = new RecursiveIteratorIterator(
new RecursiveDirectoryIterator( $startpath ),
RecursiveIteratorIterator::CHILD_FIRST
);
foreach ($raw_listings as $splFileInfo)
{
if( $splFileInfo->isDir() )
{
$path = array( $splFileInfo->getFilename() => array() );
}
else
{
$path = array( $splFileInfo->getFilename() );
}
for( $depth = $raw_listings->getDepth() - 1; $depth >= 0; $depth-- )
{
$path = array( $raw_listings->getSubIterator( $depth )->current()->getFilename() => $path );
}
$listings_array = array_merge_recursive( $listings_array, $path );
}
return $listings_array;
}
// Example:
// This will print an array containing the contents of
// the current directory and everything under it.
print_r( get_recursive_dir_file_listings( __DIR__ ) ) ;