什么是在PHP中获得上传文件扩展名的最佳方法/做法 [英] What's the best way/practice to get the extension of a uploaded file in PHP

查看:491
本文介绍了什么是在PHP中获得上传文件扩展名的最佳方法/做法的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个允许用户上传文件的表格,但是我需要获取文件扩展名,但是我不确定我是否使用了最有效的解决方案

I have a form that allow users to upload files, I however need to get the file extension, which I am able to get, but not sure if I'm using the most effective solution

我可以通过以下方式获得它

I can get it using the following ways

$fileInfo = pathinfo($_FILES['File']['name']);
echo $fileInfo['extension'];

$ext = end(explode('.',$_FILES['File']['name']));
echo $ext;

哪种方法最适合使用,或者有更好的解决方案来扩展?

Which method is the best to use or are there even better solutions that would get the extension?

推荐答案

pathinfo($_FILES['File']['name'], PATHINFO_EXTENSION)

  • 尽可能使用内置函数(@Sarfraz在此同时表示),
  • 仅提取所需的信息(options = PATHINFO_EXTENSION)
  • Use a built-in function whenever possible (what @Sarfraz said in the meantime), and
  • Extract only the needed information (options = PATHINFO_EXTENSION)

这篇关于什么是在PHP中获得上传文件扩展名的最佳方法/做法的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆