InfinityQuest - Programming Code Tutorials and Examples with Python, C++, Java, PHP, C#, JavaScript, Swift and more

Menu
  • Home
  • Sitemap

Python Programming Language Best Tutorials and Code Examples

Learn Python Right Now!
Home
PHP
Splitting a Filename into Its Component Parts in PHP
PHP

Splitting a Filename into Its Component Parts in PHP

InfinityCoder December 26, 2016

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.

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

1
2
3
$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:

1
2
3
4
$info = pathinfo('/usr/local/php/php.ini');
// $info['dirname'] is "/usr/local/php"
// $info['basename'] is "php.ini"
// $info['extension'] is "ini"

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:

1
2
3
$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:

1
2
3
$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(), dir name(), 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.

Share
Tweet
Email
Prev Article
Next Article

Related Articles

Converting an Epoch Timestamp to Time and Date Parts in PHP
You want the set of time and date parts that …

Converting an Epoch Timestamp to Time and Date Parts in PHP

Sorting Multiple Arrays in PHP
You want to sort multiple arrays or an array with …

Sorting Multiple Arrays in PHP

About The Author

InfinityCoder
InfinityCoder

Leave a Reply

Cancel reply

Recent Tutorials InfinityQuest

  • Adding New Features to bash Using Loadable Built-ins in bash
    Adding New Features to bash Using Loadable …
    June 27, 2017 0
  • Getting to the Bottom of Things in bash
    Getting to the Bottom of Things in …
    June 27, 2017 0

Recent Comments

  • fer on Turning a Dictionary into XML in Python
  • mahesh on Turning a Dictionary into XML in Python

Categories

  • Bash
  • PHP
  • Python
  • Uncategorized

InfinityQuest - Programming Code Tutorials and Examples with Python, C++, Java, PHP, C#, JavaScript, Swift and more

About Us

Start learning your desired programming language with InfinityQuest.com.

On our website you can access any tutorial that you want with video and code examples.

We are very happy and honored that InfinityQuest.com has been listed as a recommended learning website for students.

Popular Tags

binary data python CIDR convert string into datetime python create xml from dict python dictionary into xml python how to create xml with dict in Python how to write binary data in Python IP Address read binary data python tutorial string as date object python string to datetime python

Archives

  • June 2017
  • April 2017
  • February 2017
  • January 2017
  • December 2016
  • November 2016
Copyright © 2021 InfinityQuest - Programming Code Tutorials and Examples with Python, C++, Java, PHP, C#, JavaScript, Swift and more
Programming Tutorials | Sitemap