note 109129 deleted from function.filetype by danbrown
| From: | danbrown@php.net | Date: | Tue, 26 Jun 2012 13:58:16 +0000 |
| Subject: | note 109129 deleted from function.filetype by danbrown | ||
| References: | 1 | Groups: | php.notes |
| Request: | Send a blank email to php-notes+get-188836@lists.php.net to get a copy of this message | ||
Note Submitter: Si
----
In response to holdoffhunger, there is a neater way to append a string to a file name in front of
the extension using the pathinfo() function.
pathinfo() returns an associative array:
$pathSTR = '/home/mysite/mydir/my.file.php'
$pathARR = pathinfo($pathSTR);
$dirname = $pathARR['dirname']; // '/home/mysite/mydir'
$basename = $pathARR['basename']; // 'my.file.php'
$extension = $pathARR['extension']; // 'php'
$filename = $pathARR['filename']; // 'my.file' (since PHP 5.2.0)
So, to reconstruct a file name:
$pathSTR = '/home/mysite/mydir/my.file.php'
$pathARR = pathinfo($pathSTR);
$newPathSTR = "$pathARR[dirname]/$pathARR[filename]_BACKUP-1.$pathARR[extension]"; //
'/home/mysite/mydir/my.file_BACKUP-1.php'
Note that this also works for relative paths:
$pathSTR2 = '../../mydir/my.file.php';
$pathARR2 = pathinfo($pathSTR2);
$newPathSTR2 = "$pathARR2[dirname]/$pathARR2[filename]_BACKUP-2.$pathARR2[extension]"; //
'../../mydir/my.file_BACKUP-2.php'
If you are using a version prior to PHP 5.2.0, you can get $filename from:
$filename = substr($basename, 0, strrpos($basename, '.'));