如何使用php抓取文件的相对路径 [英] how to grab the relative path of a file with php

查看:78
本文介绍了如何使用php抓取文件的相对路径的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试捕获文件的相对路径以创建共享链接. 从我在Web服务器上的httpdocs文件夹中,我的文件在这里:

i am trying to catch the relative path to a file to create a share link. From my httpdocs folder on the webserver, my file is here:

jack/single/uploads/folder1/image.jpg

var $dir . '/' . $file给了我这个输出:

The var $dir . '/' . $file gives me this output:

uploads/folder1/image.jpg

realpath($dir . '/' . $file给我这个输出:

/home/vhosts/example.com/subdomains/develop3/httpdocs/jack/single/uploads/folder1/image.jpg

我要实现的是此输出:

`http://develop3.example.com/jack/single/uploads/folder1/image.jpg`

我该如何实现这一点,以便创建共享链接?

How can i achieve this, so that i can create a share link?

推荐答案

您可以在realpath的输出上使用preg_replace,用站点的URL替换直到httpdocs的所有内容:

You could use preg_replace on the output of realpath to replace everything up to httpdocs with your site's URL:

echo preg_replace('#^' . preg_quote($_SERVER['DOCUMENT_ROOT']) . '[\\\\/]#', "{$_SERVER['HTTP_HOST']}/", realpath('test6.php')) . "\n";

这篇关于如何使用php抓取文件的相对路径的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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