简体   繁体   English

如何从具有任何扩展名的路径获取文件扩展名? 解决了

[英]How to get a file extension from path with any extension? Solved

Solved.解决了。 "../pictures/uploads/profile" I changed to "./pictures/uploads/profile", My mistake, sorry! “../pictures/uploads/profile”我改成了“./pictures/uploads/profile”,我错了,对不起! And I used answer from @Kunal Raut我使用了@Kunal Raut 的回答

I want to get extension of file in PHP from path.我想从路径中获取 PHP 中的文件扩展名。 I have this code:我有这个代码:

$fileName = "../pictures/uploads/profile".$id.".*";
$ext = pathinfo($fileName, PATHINFO_EXTENSION);

$sourceImg = "../pictures/uploads/profile".$id.".".$ext."?".mt_rand();

I have folder with pictures.我有图片文件夹。 They can be in png or jpg or jpeg.它们可以是 png 或 jpg 或 jpeg。 And php file is in another folder. php 文件位于另一个文件夹中。 So how do that?那么怎么做呢?

$fileName = "../pictures/uploads/profile".$id.".*"; $fileName = "../pictures/uploads/profile".$id.".*"; // <---- this is not a file,put a file name $ext = pathinfo($fileName, PATHINFO_EXTENSION); // <---- 这不是文件,放一个文件名 $ext = pathinfo($fileName, PATHINFO_EXTENSION);

Try this:尝试这个:

$files = scandir("../pictures/uploads/");
foreach ($files as $fileName) {
   $ext = pathinfo($fileName, PATHINFO_EXTENSION); 
}

Just use the DirectoryIterator class.只需使用 DirectoryIterator class。

foreach (new DirectoryIterator('../pictures/uploads') as $fileInfo) {
    if ($fileInfo->isDot()) continue;

    if (stripos($fileInfo->getFilename(), 'profile' . $id) !== false) {
        var_dump($fileInfo->getExtension());
    }
}

The DirectoryIterator class is available since PHP 5.3.6. DirectoryIterator class 自 PHP 5.3.6 起可用。

A more detailed example could be a DirectoryIterator instance used by a FilterIterator instance to just output the desired files.更详细的示例可能是 FilterIterator 实例使用的 DirectoryIterator 实例,仅用于 output 所需的文件。 Th given example requires a minimum of PHP 7.4.给出的示例至少需要 PHP 7.4。 If you 're using a PHP version smaller than 7.4 remove the type hints for the class properties.如果您使用的是小于 7.4 的 PHP 版本,请删除 class 属性的类型提示。

class MyFileFilter extends FilterIterator
{
    protected string $filename;
    public function __construct(Iterator $iterator, $filename)
    {
        $this->filename = $filename;
        parent::__construct($iterator);
    }

    public function accept(): bool
    {
        $info = $this->getInnerIterator();
        return stripos($info->getFilename(), $this->filename) !== false;
    }
}

The FilterIterator class is available since PHP 5.1. FilterIterator class 自 PHP 5.1 起可用。 The above shown extension takes an Iterator and searches for a given filename and returns the SplFileInfo object, if the filename matches.上面显示的扩展采用一个迭代器并搜索给定的文件名,如果文件名匹配,则返回 SplFileInfo object。

How to use it:如何使用它:

$directory = new DirectoryIterator('../pictures/uploads');
$filename = 'picture' . $id;
$filter = new MyFileFilter($directory, $filename);

foreach ($filter as $file) {
    var_dump($file->getExtension());
}

The foreach loop is basically the filter, that only returns files, that match the given filename. foreach 循环基本上是过滤器,它只返回与给定文件名匹配的文件。 Every returned file is a SplFileInfo instance.每个返回的文件都是一个 SplFileInfo 实例。 With this you can use the SplFileInfo::getExtension() method to get the file extension.有了这个,您可以使用 SplFileInfo::getExtension() 方法来获取文件扩展名。

Last but not least comes the GlobIterator class which is available since PHP 5.3.最后但并非最不重要的是GlobIterator class 自 PHP 5.3 起可用。 It 's the easiest way to iteratate over a given path with placeholders.这是使用占位符遍历给定路径的最简单方法。

$filesystem = new GlobIterator('../pictures/uploads/profile' . $id . '.*');
foreach ($filesystem as $file) {
    var_dump($file->getExtension());
}

You can get all the extensions in the directory by using the function scandir() as您可以使用 function scandir()作为目录中的所有扩展名

$fileName = scandir("../pictures/uploads/");
$ext = "";
foreach($fileName as $files) {
if($files !== '.' && $files !== '..') {
$ext = pathinfo($files, PATHINFO_EXTENSION);
 }
}
 echo $ext .'<br>';

Usually scandir() returns the first two values in the array as .通常scandir()将数组中的前两个值返回为. and .. and by if condition mentioned in the answer you can delete these unwanted values and get the answer in the pure form...如果答案中提到的条件,您可以删除这些不需要的值并以纯形式获得答案。

Note: scandir() returns the values in the form of array.注意: scandir()以数组的形式返回值。

i got it working see below我得到了它的工作见下文

在此处输入图像描述

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM