Question

I'm making a class that lists files of a given extension inside a folder.

the trouble I'm having is that some files are images and I want to extract more information from those files and not for files that are not images.

I created this foreach that adds an entry to an array. later that array is returnd and contains all the files:

$file_array[] = array(
   "basename"=>$file->getBasename(),
   "size"=>$file->getSize(), //in bytes
   "extension"=>$extension,
   "file"=>$filename,
   "path"=>$file->getPath(),
   in_array(strtolower($extension), array("jpg","jpeg","png","gif")) ? array("info"=>getimagesize($filename)) : null
   );

As i said earlier, this works. the problem that I want to correct is the output:

Array
(
    [0] => Array
        (
            [basename] => 1.JPG
            [size] => 56533
            [extension] => jpg
            [file] => folder\1.JPG
            [path] => folder
            [0] => Array
                (
                    [info] => Array
                        (
                            [0] => 897
                            [1] => 616
                            [2] => 2
                            [3] => width="897" height="616"
                            [bits] => 8
                            [channels] => 3
                            [mime] => image/jpeg
                        )

                )

        )

    [1] => Array
        (
            [basename] => 2.JPG
            [size] => 56533
            [extension] => jpg
            [file] => folder\2.JPG
            [path] => folder
            [0] => Array
                (
                    [info] => Array
                        (
                            [0] => 897
                            [1] => 616
                            [2] => 2
                            [3] => width="897" height="616"
                            [bits] => 8
                            [channels] => 3
                            [mime] => image/jpeg
                        )

                )

        )
)

the problem I want to correct is that I want to access the file info like $file_array[0]['info'] and not $file_array[0][0]['info']. That second index is useless since there can be only one info for a file.

Any way to correct this?

Was it helpful?

Solution

Then don't add an array around your info key:

'info' => in_array(...) ? getimagesize($filename) : null

OTHER TIPS

$file_array[] = array(
   "basename"=>$file->getBasename(),
   "size"=>$file->getSize(), //in bytes
   "extension"=>$extension,
   "file"=>$filename,
   "path"=>$file->getPath(),
   "info" => in_array(strtolower($extension), array("jpg","jpeg","png","gif")) ? getimagesize($filename) : array()
   );

This should work

Your keys are incorrect. Try this. Note that I've added info as an index which sets the value from the condition.

$file_array[] = array(
   "basename"=>$file->getBasename(),
   "size"=>$file->getSize(), //in bytes
   "extension"=>$extension,
   "file"=>$filename,
   "path"=>$file->getPath(),
   'info' => in_array(strtolower($extension), array("jpg","jpeg","png","gif")) ? getimagesize($filename) : null
   );
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top