<tutorialjinni.com/>

PHP Get File Information

Posted Under: PHP, Programming, Snippets on Feb 27, 2020
PHP Code snippet to get Information about a file such as File Name, File Extension, File Size, File MIME Type, File Complete Path.
<?php

$tragetFile="/var/www/html/sample.php";

$file = new SplFileObject($tragetFile);
$finfo = new finfo(FILEINFO_MIME);

echo "File Name with Extension: ".$file->getFilename();
echo "<br/><br/>";
echo "File Extension: ".$file->getExtension();
echo "<br/><br/>";
echo "File Name without Extension: ".substr($file->getFilename(), 0, (strlen($file->getExtension())>0?(-1* strlen($file->getExtension())-1):strlen($file->getFilename())));
echo "<br/><br/>";
echo "File Path: ".$file->getPath();
echo "<br/><br/>";
echo "File Size (Bytes): ".$file->getSize();
echo "<br/><br/>";
echo "File MIME Type With encoding: ".$finfo->file($tragetFile);

?>


imgae