如何从 php 中的某个目录捕获文件或目录

how to catch file or dir from a certain dir in php

我正在尝试从某个目录中捕获(回显)文件或目录。

我想将它绑定到一个变量。

不,我正在使用 realpath,他给了我确切的文件位置,但来自服务器。

echo realpath($dir.'/'.$file);

所以我的回声显示了这个: /home/vhosts/mydomain.com/subdomains/develop3/httpdocs/uploads/sfm/c4ca4238a0b923820dcc509a6f75849b/Jellyfish.jpg/home/vhosts/mydomain.com/subdomains/develop3/httpdocs/uploads/sfm/c4ca4238a0b923820dcc509a6f75849b/nameofdirectory

应该是这样的:

uploads/sfm/c4ca4238a0b923820dcc509a6f75849b/Jellyfish.jpguploads/sfm/c4ca4238a0b923820dcc509a6f75849b/nameofdirectory

我怎样才能做到这一点?

我不确定你真正需要什么以及$dir的价值是多少,但你可以使用:

$fullPath = realpath($dir.'/'.$file);
$relativePath = preg_replace('%/home.*httpdocs/%i', '', $fullPath);