-1

Solved! "../pictures/uploads/profile" I changed to "./pictures/uploads/profile". My mistake, sorry! And I used answer from @Kunal Raut

I want to get extension of file in PHP from path. 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. And php file is in another folder. So how do that?

UnexoN
  • 1
  • 4

3 Answers3

0

$fileName = "../pictures/uploads/profile".$id.".*"; // <---- this is not a file,put a file name $ext = pathinfo($fileName, PATHINFO_EXTENSION);

Try this:

$files = scandir("../pictures/uploads/");
foreach ($files as $fileName) {
   $ext = pathinfo($fileName, PATHINFO_EXTENSION); 
}
Curlas
  • 879
  • 5
  • 14
0

Just use the 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.

A more detailed example could be a DirectoryIterator instance used by a FilterIterator instance to just output the desired files. Th given example requires a minimum of PHP 7.4. If you 're using a PHP version smaller than 7.4 remove the type hints for the class properties.

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. The above shown extension takes an Iterator and searches for a given filename and returns the SplFileInfo object, if the filename matches.

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. Every returned file is a SplFileInfo instance. With this you can use the SplFileInfo::getExtension() method to get the file extension.

Last but not least comes the GlobIterator class which is available since 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());
}
Marcel
  • 4,854
  • 1
  • 14
  • 24
  • I edited my question. Maybe now it's easier to understand – UnexoN Apr 28 '20 at 10:04
  • Well, if I understand your changes, you are searching for all files with a given filename in a specific path to get the file extension. That 's exactly what my given code examples do. – Marcel Apr 28 '20 at 10:05
  • "Parse error: syntax error, unexpected 'string' (T_STRING), expecting function (T_FUNCTION) or const (T_CONST)" on line with "protected string" – UnexoN Apr 28 '20 at 10:13
  • Well, that sounds like a PHP version < 7.4. What php version are you running? Try `echo PHP_VERSION;`. – Marcel Apr 28 '20 at 10:14
  • It's 7.2.30 version – UnexoN Apr 28 '20 at 10:19
  • I 've edited my answer and gave you an example for the GlobIterator class, which is available since PHP 5.3. This should work. – Marcel Apr 28 '20 at 10:21
  • Still don't get extension – UnexoN Apr 28 '20 at 10:24
  • What do you get instead? What did you try from the above? Are you using the right directory? Does a filename with the given `$id` even exist in the directory? Show me some code. – Marcel Apr 28 '20 at 10:27
  • Well ... you should not just copy code from stack overflow! Try to understand, what the code does and transfer that to your application. The line `$ext = var_dump($file->getExtension);` can not work! Change it into `$ext = $file->getExtension();`. – Marcel Apr 28 '20 at 10:33
  • I changed that but still can't got extension – UnexoN Apr 28 '20 at 10:36
  • Solved! "../pictures/uploads/profile" I changed to "./pictures/uploads/profile". My mistake, sorry! And I used answer from @Kunal Raut – UnexoN Apr 28 '20 at 12:06
-1

You can get all the extensions in the directory by using the function scandir() as

$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 . 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.

i got it working see below

enter image description here

Kunal Raut
  • 2,495
  • 2
  • 9
  • 25