PHP Directories Splitting a Filename into Its Component Parts - Supercoders | Web Development and Design | Tutorial for Java, PHP, HTML, Javascript PHP Directories Splitting a Filename into Its Component Parts - Supercoders | Web Development and Design | Tutorial for Java, PHP, HTML, Javascript

Breaking

Post Top Ad

Post Top Ad

Monday, July 15, 2019

PHP Directories Splitting a Filename into Its Component Parts

PHP Directories


Splitting a Filename into Its Component Parts

Problem

You want to find a file’s path and filename; for example, you want to create a file in the same directory as an existing file.

Solution

Use basename() to get the filename and dirname() to get the path:

       $full_name = '/usr/local/php/php.ini';
       $base = basename($full_name); // $base is "php.ini"
       $dir = dirname($full_name); // $dir is "/usr/local/php"

Use pathinfo() to get the directory name, base name, and extension in an associative array:

       $info = pathinfo('/usr/local/php/php.ini');
       // $info['dirname'] is "/usr/local/php"

       // $info['basename'] is "php.ini"
       // $info['extension'] is "ini"

Discussion

To create a temporary file in the same directory as an existing file, use dirname() to find the directory, and pass that directory to tempnam(). For instance:

       $dir = dirname($existing_file);
       $temp = tempnam($dir,'temp');
       $temp_fh = fopen($temp,'w');

The dirname() function is particularly useful in combination with the special constant __FILE__, which contains the full pathname of the current file. This is not the same as the currently executing PHP script. If /usr/local/alice.php includes /usr/local/bob.php, then __FILE__ in bob.php is /usr/local/bob.php.

This makes __FILE__ useful when you want to include or require scripts in the same directory as a particular file, but you don’t know what that directory is and it isn’t necessarily in the include path. For example:

       $currentDir = dirname(__FILE__);
       include $currentDir . '/functions.php';
       include $currentDir . '/classes.php';

If this code is in the /usr/local directory, then it includes /usr/local/functions.php and /usr/local/classes.php. This technique is particularly useful when you’re distributing code for others to use. With it, you don’t have to require any configuration or include path modification for your code to work properly. As of PHP 5.3, you use the constant __DIR__ instead of dirname(__FILE__).

Using functions such as basename(), dirname(), and pathinfo() is more portable than just splitting up full filenames on the / character because the functions use an operating system–appropriate separator. On Windows, these functions treat both / and \ as file and directory separators. On other platforms, only / is used.

There’s no built-in PHP function to combine the parts produced by basename(), dirname(), and pathinfo() back into a full filename. To do this, combine the parts with . and the built-in DIRECTORY_SEPARATOR constant, which is / on Unix and \ on Windows.

No comments:

Post a Comment

Post Top Ad