您可以使用数组符号获取完整文件路径的一部分,如
echo $path_parts['dirname'], "'n";
echo $path_parts['basename'], "'n";
echo $path_parts['extension'], "'n";
echo $path_parts['filename'], "'n"; // since PHP 5.2.0
但是除了"basename"命名不正确(因为它返回整个文件名),'basename'和'filename'版本只返回文件名的第一个字符。如果文件名是Archive.zip,我执行以下操作:
echo "Here's the zip filename: " . $zipFilename . "<br>";
echo "Here's the basename: " . $zipFilename['basename'] . "<br>";
echo "Here's the 'filename': " . $zipFilename['filename'];
我:
Here's the zip filename: Archive.zip
Here's the basename: A
Here's the 'filename': A
如果我在完整路径上尝试,上载/Archive.zip,我得到:
Here's the zip filename: uploads/Archive.zip
Here's the basename: u
Here's the 'filename': u
知道为什么吗?谢谢。
原来这里缺少一个步骤:调用pathinfo
将路径字符串转换为数组。Pramod的评论指出了这个问题,尽管它不完全正确:pathinfo没有创建一个文件数组。它只是将字符串解析为数组。
谢谢!