使用PHP从URL字符串获取文件路径/扩展名(Use PHP to Get File Path/Extension from URL string)
我正在阅读pathinfo basename之类的东西。 但是我从中收集的所有信息都是如何在使用绝对/相对路径时获得我想要的细节。 我想弄清楚的是,如何从url字符串(不一定是活动URL,也许是用户输入url)获取filename.ext 。
目前我正在寻找获取包含图像的用户提供的URL的文件名称和扩展名。 但可能想要进一步延伸这一点。 所以我想弄清楚如何获得文件名和扩展名
我想过尝试使用一些preg_match逻辑来查找url中的最后一个,将它分开,找到? 从那个(如果有的话)中删除,然后尝试在剩下的部分之后将其整理出来。 但是如果文件有多个,我会陷入困境. 名字如: 2012.01.01-name.jpg
所以我正在寻找一种理想的最佳方式来做到这一点,而不会有太大的误差。
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.
原文:https://stackoverflow.com/questions/16582359
更新时间:2020-01-13 12:55
最满意答案
使用parse_url($url, PHP_URL_PATH)来获取URI并在pathinfo / basename中使用URI。
use parse_url($url, PHP_URL_PATH) to get URI and use URI in pathinfo/basename.
2015-08-04
相关问答
您应该能够检查请求的URI是否包含“.stm”,如果是,则重定向到完全相同的URI并且“.stm”被翻录。 在你的settings.php中放置这样的东西: if (strrpos(request_uri(), '.stm') !== FALSE) {
header("Location: " . str_replace('.stm', '', request_uri()));
exit;
}
You should be able to check if the requested
...
你可以试试这个, Matcher m = Pattern.compile("[^/.]*\\.(\\w+)[^/]*$").matcher(s);
if(m.find())
{
System.out.println(m.group(1));
}
DEMO You may try this, Matcher m = Pattern.compile("[^/.]*\\.(\\w+)[^/]*$").matcher(s);
if(m.find())
{
System.out.println(m.gro
...
看看: http : //php.net/manual/en/function.pathinfo.php mixed pathinfo(string $ path [,int $ options = PATHINFO_DIRNAME | PATHINFO_BASENAME | PATHINFO_EXTENSION | PATHINFO_FILENAME]) <?php
$path_parts = pathinfo('/www/htdocs/inc/lib.inc.php');
echo $pat
...
你可以使用pathinfo函数: $prod['item_image_url'] = "new-thumb-01.jpg";
$fileparts = pathinfo($prod['item_image_url']);
$bigFileName = $fileparts['filename'] . "-big." .$fileparts['extension'];
$bigFileName包含new-thumb-01-big.jpg You can make use of pathinfo
...
要在查找文件扩展名之前删除查询字符串,可以更改此设置: var extension = (/[.]/.exec(href)) ? /[^.]+$/.exec(href) : undefined;
var filePath = href;
对此: var fname = href;
var query = href.indexOf("?");
if (query !== -1) {
fname = href.substr(0, query);
}
var extension
...
使用parse_url($url, PHP_URL_PATH)来获取URI并在pathinfo / basename中使用URI。 use parse_url($url, PHP_URL_PATH) to get URI and use URI in pathinfo/basename.
解决方案很简单 RewriteRule ^cfm/recDetailsP\.cfm$ /recording.php [L]
默认情况下,查询字符串未更改。 我在这里找到答案http://simonecarletti.com/blog/2009/01/apache-rewriterule-and-query-string/ The solution is simple RewriteRule ^cfm/recDetailsP\.cfm$ /recording.php [L]
the query
...
您需要添加一个检查,当您向请求添加.php扩展名时,php文件实际存在,并且您应该在 php扩展程序之前移动用户重写:
Options -Multiviews
Options +FollowSymLinks
RewriteEngine On
RewriteRule ^channels/page/([0-9]+)/?$ channels/index.php?p=$1 [L]
RewriteRule ^channels/([_0-9a-z-]+)/?
...
按照以下顺序制定规则: Options -MultiViews
RewriteEngine On
RewriteBase /
RewriteCond %{HTTPS} !=on
RewriteRule ^ https://%{HTTP_HOST}%{REQUEST_URI} [L,R=301]
RewriteRule ^login/([\w=-]+)/?$ login.php?user=$1 [L,QSA,NC]
RewriteCond %{REQUEST_FILENAME} !-f
Rew
...
我认为你需要将php检查应用于这两个规则: RewriteCond %{REQUEST_URI} ^(.*)/$
RewriteCond %{DOCUMENT_ROOT}%1.php -f
RewriteRule (.*)/$ $1.php [L]
RewriteCond {REQUEST_FILENAME}.php -f
RewriteRule .*[^/]$ $0/ [L,R=301]
此外,-f检查需要在没有尾部斜杠的情况下进行 I think you need to apply th
...