博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
如何在PHP中获取文件扩展名? [重复]
阅读量:2381 次
发布时间:2019-05-10

本文共 1866 字,大约阅读时间需要 6 分钟。

本文翻译自:

Possible Duplicate: 可能重复:

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楼

参考:


#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: : 你可以使用实际设计的东西: :

$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 : 但是,要检查文件的类型,使用mime_content_type是一种更好的方法: ://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 . 你可以在找到文档。

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);

转载地址:http://sfexb.baihongyu.com/

你可能感兴趣的文章
ptyon urlib2
查看>>
django 模板中使用配置参数
查看>>
django admin扩展-自定义后台管理界面
查看>>
django 非常实用的无限级分类功能
查看>>
百度地图api实例练习
查看>>
百度地图-非常实用的搜索自定义
查看>>
百度地图修改鼠标样式
查看>>
百度地图-修改marker图标(icon)
查看>>
百度地图-点击事件问题
查看>>
百度地图-自定义搜索、自定义marker、地图选址实用实例
查看>>
django 修改model field后台默认的显示方式
查看>>
django sql_queries 模板中显示所有的sql查询调试信息
查看>>
django mysql 连接查询join
查看>>
html 移动互联网终端的javascript touch事件,touchstart, touchend, touchmove
查看>>
大龄码农经验那么丰富,为什么很多公司都不招?
查看>>
零基础Python学习路线,小白的进阶之路!
查看>>
python3.基础爬取网易云音乐【超详细版】
查看>>
程序员工作量大,坚持不下去了该如何解压?
查看>>
Python时操作几个坏习惯,你中了吗?
查看>>
python玩转街机游戏,操作亲民!
查看>>