douduan5086 2016-04-29 09:54
浏览 23
已采纳

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

I am trying to catch (echo) a file or directory from a certain directory.

I want to bind it to a variable.

No i am experiencing with realpath and he gives me the exact file location but from the server.

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

So my echo shows me this: /home/vhosts/mydomain.com/subdomains/develop3/httpdocs/uploads/sfm/c4ca4238a0b923820dcc509a6f75849b/Jellyfish.jpg or /home/vhosts/mydomain.com/subdomains/develop3/httpdocs/uploads/sfm/c4ca4238a0b923820dcc509a6f75849b/nameofdirectory

it should be this:

uploads/sfm/c4ca4238a0b923820dcc509a6f75849b/Jellyfish.jpg and uploads/sfm/c4ca4238a0b923820dcc509a6f75849b/nameofdirectory

How can i achieve that?

  • 写回答

1条回答 默认 最新

  • donglu6805 2016-04-29 10:02
    关注

    I'm not sure of what you really need and what is the value of $dir, but you can use:

    $fullPath = realpath($dir.'/'.$file);
    $relativePath = preg_replace('%/home.*httpdocs/%i', '', $fullPath);
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
编辑
预览

报告相同问题?

手机看
程序员都在用的中文IT技术交流社区

程序员都在用的中文IT技术交流社区

专业的中文 IT 技术社区,与千万技术人共成长

专业的中文 IT 技术社区,与千万技术人共成长

关注【CSDN】视频号,行业资讯、技术分享精彩不断,直播好礼送不停!

关注【CSDN】视频号,行业资讯、技术分享精彩不断,直播好礼送不停!

客服 返回
顶部