php.net |  support |  documentation |  report a bug |  advanced search |  search howto |  statistics |  random bug |  login
Doc Bug #81079 pathinfo() unexpected path/filename separation with trailing slash
Submitted: 2021-05-24 14:57 UTC Modified: 2021-05-25 12:13 UTC
Votes:2
Avg. Score:4.5 ± 0.5
Reproduced:1 of 1 (100.0%)
Same Version:1 (100.0%)
Same OS:1 (100.0%)
From: info at kgsw dot de Assigned:
Status: Verified Package: *Directory/Filesystem functions
PHP Version: 7.3.28 OS: Linux Mint 20.1
Private report: No CVE-ID: None
Welcome back! If you're the original bug submitter, here's where you can edit the bug or add additional notes.
If you forgot your password, you can retrieve your password here.
Password:
Status:
Package:
Bug Type:
Summary:
From: info at kgsw dot de
New email:
PHP Version: OS:

 

 [2021-05-24 14:57 UTC] info at kgsw dot de
Description:
------------
using linux command line, sometimes trailing slash will make sure to identify a directory; 

my own, manual functions would first split path from filename at the _last_ slash;
 which would find NO filename, only path, in that example;

for me unexpected - so need documentation or is a bug? 

I could not find PHP 7.4.3 in the options dropbox to select using:
PHP 7.4.3 (cli) (built: Oct  6 2020 15:47:56) ( NTS )

once reported on Win2k - but discarded as "no URL function" - but I think, this is not the point - it is just unexpected
https://bugs.php.net/bug.php?id=41834

If you still insist on that IMHO strange behaviour, I would like to add a note to the manual

Test script:
---------------
php > var_dump( pathinfo('/home/user/backup.2021-05-24/'));
array(4) {
  ["dirname"]=>
  string(10) "/home/user"
  ["basename"]=>
  string(17) "backup.2021-05-24"
  ["extension"]=>
  string(10) "2021-05-24"
  ["filename"]=>
  string(6) "backup"
}

Expected result:
----------------
array(4) {
  ["dirname"]=>
  string(10) "/home/user/backup.2021-05-24"
  // no more set - cause no filename given
}


Patches

Pull Requests

History

AllCommentsChangesGit/SVN commitsRelated reports
 [2021-05-25 12:13 UTC] cmb@php.net
-Status: Open +Status: Verified
 [2021-05-25 12:13 UTC] cmb@php.net
Well, that behavior might be confusing, but it is long
standing[1], and consistent with basedir() and dirname(), so it
can't be changed. A note in the docs won't hurt.

[1] <https://3v4l.org/aXYbl>
[2] <https://3v4l.org/mlkXI>
 [2023-06-21 09:28 UTC] nguyenthuongzl633 at gmail dot com
In the Linux command line, whether or not a trailing slash is required to identify a directory depends on the specific command or tool you are using.
There are some exceptions where a trailing slash may be required or preferred.

cp -r /path/to/source/directory/ /path/to/destination/

If you omit the trailing slash after the source directory, the cp command will create a new directory inside the destination with the same name as the source directory and copy the contents into that new directory.

Regarding your question about splitting the path and filename in your own functions, if you are using PHP, the pathinfo() function can be helpful. It returns an associative array containing information about a file path, including the directory name and filename. Here's an example:


$path = '/path/to/directory/filename.txt';
$pathInfo = pathinfo($path);

$directory = $pathInfo['dirname']; // Contains '/path/to/directory'
$filename = $pathInfo['filename']; // Contains 'filename'

Regarding PHP 7.4.3, it seems that you are referring to the PHP version installed on your system. The version you mentioned, 7.4.3, is a specific release of PHP that was built on October 6, 2020. If you are unable to select PHP 7.4.3 from the options dropdown, it could indicate that the specific version is not available in the environment you are using. You may need to select an alternative PHP version that is supported in your current setup. (https://www.myaarpmedicares.us/)github.com

I recommend referring to the documentation or contacting the provider of the environment or platform you are using for further assistance in selecting the appropriate PHP version. They should be able to provide you with the available PHP options and help you choose the desired version.
 [2023-08-12 10:14 UTC] astro258shastri at gmail dot com
Great info! I recently came across your blog and have been reading along.
 [2023-08-12 10:15 UTC] punjabbestastrologer at gmail dot com
bestastrologernearyou.wordpress.com/
bestastrologerinamritsar.blogspot.com/
 
PHP Copyright © 2001-2024 The PHP Group
All rights reserved.
Last updated: Thu Nov 21 18:01:29 2024 UTC