使用PHP从URL字符串获取文件路径/扩展 [英] Use PHP to Get File Path/Extension from URL string

查看:107
本文介绍了使用PHP从URL字符串获取文件路径/扩展的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在阅读 pathinfo basename 之类的东西。但我从这些收集的是如何获得我想要的细节,当使用绝对/相对路径。我想弄清楚的是,我怎样才能从一个url字符串(不一定是活动的URL,也许是一个用户输入url)获取 filename.ext



目前我正在寻找获取包含图像的用户提供的URL的文件名和扩展名。但可能想要进一步延伸这一路。所以我想弄清楚如何得到文件名和扩展名。



我想过要使用一些 preg_match 逻辑找到最后/在网址中,分裂它,找到?从那里(如果有的话),并删除超过这一点,然后试图解决什么剩下。但是如果文件中有多个,例如: 2012.01.01-name.jpg



所以我正在寻找一个完美的方法来做到这一点,而不会有太大的误差。解决方案

使用 parse_url($ url,PHP_URL_PATH) 来获取URI并在pathinfo / basename中使用URI。

I'm reading up on pathinfo basename and the like. But all I am gathering from those are how to obtain the details I want when using an absolute/relative path. What I am trying to figure out is how can I get the filename.ext from a url string (not necessarily the active URL, maybe a user input url).

Currently I am looking to get the file name and extension of user provided URLs containing images. But may want to extend this further down the road. So in all I would like to figure out how I can get the filename and extension

I thought about trying to use some preg_match logic finding the last / in the url, spliting it, finding the ? from that (if any) and removing the point beyond that and then trying to sort it out after with whats left. but I get stuck in cases where the file has multiple . in the name ie: 2012.01.01-name.jpg

So I am looking for a sane optimal way of doing this without to much margin of error.

解决方案

use parse_url($url, PHP_URL_PATH) to get URI and use URI in pathinfo/basename.

这篇关于使用PHP从URL字符串获取文件路径/扩展的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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