Skip to content
Advertisement

Get pathinfo from an open file handle

I’m reading data from a file and need to run checks on its extension

I opened the file handle with fopen and given the handle to pathinfo()

$handle2 = fopen('files/ppGM.txt', 'r');
$fileinfo = pathinfo($handle2);

I need the array of informations from path info but the functions requires the first parameter to be string:

Warning: pathinfo() expects parameter 1 to be string, resource given in /home/kroman02/public_www/p1tma/index.php on line 21

Advertisement

Answer

pathinfo() as it suggests – works with the path of a file and not a file handle. So you would usually use something like…

$fileName = 'files/ppGM.txt';
$handle2 = fopen($fileName, 'r');
$fileinfo = pathinfo($fileName);

and this would result with $fileinfo containing…

Array
(
    [dirname] => files
    [basename] => ppGM.txt
    [extension] => txt
    [filename] => ppGM
)
User contributions licensed under: CC BY-SA
10 People found this is helpful
Advertisement