19.5. Splitting a Filename into Its Component Parts
$full_name = '/usr/local/php/php.ini'; $base = basename($full_name); // $base is php.ini $dir = dirname($full_name); // $dir is /usr/local/php
$info = pathinfo('/usr/local/php/php.ini');
$dir = dirname($existing_file); $temp = tempnam($dir,'temp'); $temp_fh = fopen($temp,'w');
$info = pathinfo('/usr/local/php/php.ini'); print_r($info); Array ( [dirname] => /usr/local/php [basename] => php.ini [extension] => ini )
You can also pass basename( ) an optional suffix to remove it from the filename. This sets $base to php:
$base = basename('/usr/local/php/php.ini','.ini');
Using functions such as basename( ), dirname( ), and pathinfo( ) is more portable than just separating a full filename on / because they use an operating-system appropriate separator. On Windows, these functions treat both / and \ as file and directory separators. On other platforms, only / is used.
$dirname = '/usr/local/php'; $basename = 'php'; $extension = 'ini'; $full_name = $dirname . '/' . $basename . '.' . $extension;
You can pass a full filename produced like this to other PHP file functions on Windows, because PHP accepts / as a directory separator on Windows.
19.5.4. See Also
Copyright © 2003 O'Reilly & Associates. All rights reserved.