Ways to get file types and file information in PHP _php tips

Source: Internet
Author: User
Tags comparison table

This example describes how PHP obtains file types and file information. Share to everyone for your reference. The implementation methods are as follows:

<?php
$file = "Php.txt";
Open the file, R means read-only
$handle = fopen ($file, "R");
Gets the statistics for the file
$fstat = Fstat ($handle);
echo "FileName:". basename ($file). " <br> ";
echo "File size:". Round (FileSize ("$file")/1024,2). " Kb<br> ";
echo "File size:". Round ($fstat ["Size"]/1024,2). " Kb<br> ";
echo "Last Accessed Time:". Date ("Y-m-d h:i:s", Fileatime ($file)). " <br> ";
echo "Last Access Time:". Date ("Y-m-d h:i:s", $fstat ["Atime"]). " <br> ";
echo "Last modified:". Date ("Y-m-d h:i:s", Filemtime ($file)). " <br> ";
echo "Last modified:". Date ("Y-m-d h:i:s", $fstat ["Mtime"]);
? >

What is the MIME type, which is the type of file that sets an extension to open with an application, and when the file is accessed, the browser automatically opens with the specified application.
Used to specify some client-defined file names and how some media files are opened.

Reference Link: PHP file format (MIME type) comparison table.

1, Mime_content_type () function to judge get MIME type
Mime_content_type returns the MIME type of the specified file, using:

echo mime_content_type (' Php.gif '). "\ n";
echo mime_content_type (' test.php ');

Output:
Image/gif
Text/plain


But PHP 5.3.0 has discarded the function. If you still want to use this function, you can configure PHP to enable Magic_mime extensions.

2, PHP Fileinfo get file MIME type (finfo_open)


An alternative function of the official PHP recommendation Mime_content_type () is the FileInfo function. The PHP 5.3.0+ already supports the FileInfo function (FileInfo support-enabled) By default and can use Finfo_open () to determine the file MIME type without any configuration. Usage:

$finfo = Finfo_open (fileinfo_mime);
$mimetype = Finfo_file ($finfo, $filename);
Finfo_close ($finfo);

3, Image_type_to_mime_type () Get picture MIME type

If you need to determine a MIME-type file with only an image file, you can first use the Exif_imagetype () function to get the image type constant, and then use the Image_type_to_mime_type () function to convert the image type constant to the MIME type of the picture file.
Note: You need to configure open Php_mbstring.dll (Windows required) and Extension=php_exif.dll in php.ini.

4, PHP upload file to get MIME type
If you use PHP to upload a file and detect the MIME type of the uploaded file, you can use the global variable $_files[' uploadfile ' [' type '], which is detected by the client's browser to get the file MIME type.

5, by file extension to determine the file type

<?php 
$filename = "D:\\296.mid"; 
$file = fopen ($filename, "RB"); 
$bin = Fread ($file, 2); Read Only 2 bytes 
fclose ($file); 
$strInfo = @unpack ("C2chars", $bin); 
$typeCode = Intval ($strInfo [' chars1 ']. $strInfo [' chars2 ']); 
$fileType = '; 
Switch ($typeCode) 
{case 
7790: 
$fileType = ' exe '; 
break; 
Case 7784: 
$fileType = ' midi '; 
break; 
Case 8297: 
$fileType = ' rar '; 
break; 
Case 255216: 
$fileType = ' jpg '; 
break; 
Case 7173: 
$fileType = ' gif '; 
break; 
Case 6677: 
$fileType = ' bmp '; 
break; 
Case 13780: 
$fileType = ' png '; 
break; 
Default: 
echo ' unknown '; 
} 
Echo ' This is a '. $fileType. ' File: '. $typeCode; 

The above is the PHP file type to judge several methods, if you have a better way, you can leave a message

I hope this article will help you with your PHP program design.

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.