


PHP uses the finfo_file() function to implement the method of detecting the type of uploaded images
这篇文章主要介绍了PHP使用finfo_file()函数检测上传图片类型的实现方法,结合实例形式分析了finfo_file()函数的功能、使用方法及相关注意事项,需要的朋友可以参考下
在输入输出中,文件的交互必不可少,比如文件的上传什么的。这里我们来解决一个小问题,就是如何判断用户上传文件的文件类型。
举一个应用场面:在我们的Web应用中,比如用户上传头像,要求是png,jpg,gif格式,接收到图片后会根据图片格式类型做不同的头像切割处理,但个别用户会传一些只更改过文件后缀的非标准图片,比如nowamagic.jpg 强行修改成 nowamagic.png,这样程序无法对该类型图片进行切割,切割失败。这个时候就需要对文件进行检测,并且仅仅根据后缀来判断是不行的。
这种这么常见的需求,PHP肯定自带了某些函数来实现的,这个函数就是 finfo_file
。
finfo_file
(PHP >= 5.3.0, PECL fileinfo >= 0.1.0)
finfo_file -- finfo::file — Return information about a file,This function is used to get information about a file.
函数很简单,看看官方文档就大概清楚了,这里就举一个使用例子吧,就是上传一张图片,并判断它的类型:
<html> <meta http-equiv="Content-Type" content="text/html; charset=utf-8"> <head> <title>文件上传</title> </head> <body> <form accept="" method="post" enctype="multipart/form-data"> <input type="file" name="img[]" /><br /> <input type="submit" name="s" /><br /> </form> <?php //上传文件信息 $img = $_FILES['img']; if ($img) { //文件存放目录,和本php文件同级 $dir = dirname(__file__); $filename = $img['name'][0]; $tempname = $img['tmp_name'][0]; $savepath = "$dir\\$filename"; $state = move_uploaded_file($tempname, $savepath); //如果上传成功,预览 if($state) { echo "<img src='$filename' alt='$filename' /> "; } $finfo = finfo_open(FILEINFO_MIME_TYPE); //$mime = finfo_file($finfo, dirname(__FILE__)."/".$filename); $mime = finfo_file($finfo, $savepath); echo $mime; } ?> </body> </html>
以上就是本文的全部内容,希望对大家的学习有所帮助。
相关推荐:
PHP函数checkdnsrr用法详解(Windows平台用法)_php技巧
The above is the detailed content of PHP uses the finfo_file() function to implement the method of detecting the type of uploaded images. For more information, please follow other related articles on the PHP Chinese website!

PHPidentifiesauser'ssessionusingsessioncookiesandsessionIDs.1)Whensession_start()iscalled,PHPgeneratesauniquesessionIDstoredinacookienamedPHPSESSIDontheuser'sbrowser.2)ThisIDallowsPHPtoretrievesessiondatafromtheserver.

The security of PHP sessions can be achieved through the following measures: 1. Use session_regenerate_id() to regenerate the session ID when the user logs in or is an important operation. 2. Encrypt the transmission session ID through the HTTPS protocol. 3. Use session_save_path() to specify the secure directory to store session data and set permissions correctly.

PHPsessionfilesarestoredinthedirectoryspecifiedbysession.save_path,typically/tmponUnix-likesystemsorC:\Windows\TemponWindows.Tocustomizethis:1)Usesession_save_path()tosetacustomdirectory,ensuringit'swritable;2)Verifythecustomdirectoryexistsandiswrita

ToretrievedatafromaPHPsession,startthesessionwithsession_start()andaccessvariablesinthe$_SESSIONarray.Forexample:1)Startthesession:session_start().2)Retrievedata:$username=$_SESSION['username'];echo"Welcome,".$username;.Sessionsareserver-si

The steps to build an efficient shopping cart system using sessions include: 1) Understand the definition and function of the session. The session is a server-side storage mechanism used to maintain user status across requests; 2) Implement basic session management, such as adding products to the shopping cart; 3) Expand to advanced usage, supporting product quantity management and deletion; 4) Optimize performance and security, by persisting session data and using secure session identifiers.

The article explains how to create, implement, and use interfaces in PHP, focusing on their benefits for code organization and maintainability.

The article discusses the differences between crypt() and password_hash() in PHP for password hashing, focusing on their implementation, security, and suitability for modern web applications.

Article discusses preventing Cross-Site Scripting (XSS) in PHP through input validation, output encoding, and using tools like OWASP ESAPI and HTML Purifier.


Hot AI Tools

Undresser.AI Undress
AI-powered app for creating realistic nude photos

AI Clothes Remover
Online AI tool for removing clothes from photos.

Undress AI Tool
Undress images for free

Clothoff.io
AI clothes remover

Video Face Swap
Swap faces in any video effortlessly with our completely free AI face swap tool!

Hot Article

Hot Tools

DVWA
Damn Vulnerable Web App (DVWA) is a PHP/MySQL web application that is very vulnerable. Its main goals are to be an aid for security professionals to test their skills and tools in a legal environment, to help web developers better understand the process of securing web applications, and to help teachers/students teach/learn in a classroom environment Web application security. The goal of DVWA is to practice some of the most common web vulnerabilities through a simple and straightforward interface, with varying degrees of difficulty. Please note that this software

MantisBT
Mantis is an easy-to-deploy web-based defect tracking tool designed to aid in product defect tracking. It requires PHP, MySQL and a web server. Check out our demo and hosting services.

SublimeText3 Chinese version
Chinese version, very easy to use

SecLists
SecLists is the ultimate security tester's companion. It is a collection of various types of lists that are frequently used during security assessments, all in one place. SecLists helps make security testing more efficient and productive by conveniently providing all the lists a security tester might need. List types include usernames, passwords, URLs, fuzzing payloads, sensitive data patterns, web shells, and more. The tester can simply pull this repository onto a new test machine and he will have access to every type of list he needs.

SublimeText3 Mac version
God-level code editing software (SublimeText3)
