如何在PHP中获取文件扩展名? [重复]

本文翻译自:How to get the file extension in PHP? [duplicate]

Possible Duplicate: 可能重复:
How to extract a file extension in PHP? 如何在PHP中提取文件扩展名?

I wish to get the file extension of an image I am uploading, but I just get an array back. 我希望获得我上传的图像的文件扩展名,但我只是得到了一个数组。

$userfile_name = $_FILES['image']['name'];
$userfile_extn = explode(".", strtolower($_FILES['image']['name']));

Is there a way to just get the extension itself? 有没有办法让扩展本身?


#1楼

参考:https://stackoom.com/question/hVFJ/如何在PHP中获取文件扩展名-重复


#2楼

怎么样

$ext = array_pop($userfile_extn);

#3楼

No need to use string functions. 无需使用字符串函数。 You can use something that's actually designed for what you want: pathinfo() : 你可以使用实际设计的东西: pathinfo()

$path = $_FILES['image']['name'];
$ext = pathinfo($path, PATHINFO_EXTENSION);

#4楼

A better method is using strrpos + substr (faster than explode for that) : 一个更好的方法是使用strrpos + substr(比爆炸更快):

$userfile_name = $_FILES['image']['name'];
$userfile_extn = substr($userfile_name, strrpos($userfile_name, '.')+1);

But, to check the type of a file, using mime_content_type is a better way : http://www.php.net/manual/en/function.mime-content-type.php 但是,要检查文件的类型,使用mime_content_type是一种更好的方法: http ://www.php.net/manual/en/function.mime-content-type.php


#5楼

You could try with this for mime type 您可以尝试使用此类型的mime

$image = getimagesize($_FILES['image']['tmp_name']);

$image['mime'] will return the mime type. $image['mime']将返回mime类型。

This function doesn't require GD library. 此功能不需要GD库。 You can find the documentation here . 你可以在这里找到文档。

This returns the mime type of the image. 这将返回图像的mime类型。

Some people use the $_FILES["file"]["type"] but it's not reliable as been given by the browser and not by PHP. 有些人使用$_FILES["file"]["type"]但它不像浏览器而不是PHP那样可靠。

You can use pathinfo() as ThiefMaster suggested to retrieve the image extension. 您可以使用pathinfo()作为ThiefMaster建议检索图像扩展名。

First make sure that the image is being uploaded successfully while in development before performing any operations with the image. 首先确保在开发过程中成功上载图像,然后再对图像执行任何操作。


#6楼

This will work as well: 这也可以:

$array = explode('.', $_FILES['image']['name']);
$extension = end($array);
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值