查找上传文件的扩展名 (PHP)

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/5427222/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-25 21:22:44  来源:igfitidea点击:

Finding Extension of uploaded file (PHP)

php

提问by Peter

Possible Duplicate:
What's the best way/practice to get the extension of a uploaded file in PHP

可能的重复:
在 PHP 中获取上传文件的扩展名的最佳方法/实践是什么

Can We find out the extension of the original file from the $_FILES["file"]["tmp_name"] ? For example jpg or png etc? Thanks.

我们可以从 $_FILES["file"]["tmp_name"] 中找出原始文件的扩展名吗?例如jpg或png等?谢谢。

回答by Dejan Marjanovic

$name = $_FILES["file"]["name"];
$ext = end((explode(".", $name))); # extra () to prevent notice

echo $ext;

回答by JKS

You could use pathinfo():

你可以使用pathinfo()

$path_parts = pathinfo($_FILES["file"]["name"]);
$extension = $path_parts['extension'];

回答by GWW

Yes you can use $_FILES['file']['name']to get the original name of the uploaded file. Just keep in mind that the extension may not always represent the real contents of the file.

是的,您可以使用它$_FILES['file']['name']来获取上传文件的原始名称。请记住,扩展名可能并不总是代表文件的真实内容。

回答by thf

Yes, assuming it's accurately named. It will retain its original name and extension.

是的,假设它被准确命名。它将保留其原始名称和扩展名。