简体   繁体   English

glob-设置相对路径根目录

[英]glob - set relative path root directory

I'm having some trouble with glob in php and although I did some research, I just couldn't seem to find an answer to my problem. 我在php中使用glob遇到了一些麻烦,尽管我做了一些研究,但似乎找不到解决我问题的答案。

First of all to my directory structure: 首先是我的目录结构:

/ (root)
 /images (folder)
       - gallery/ (sub folder containing image files)
 /pages (folder)
       - gallery.php

I want to access the subfolder 'gallery' in the folder 'images'. 我要访问文件夹“图像”中的子文件夹“图库”。 So: In my gallery.php file I got the following line: 因此:在我的gallery.php文件中,我得到了以下内容:

$images = glob("/images/gallery/*.*");

And this doesn't work. 这是行不通的。

What does work is, if I change the path to " ../ " 如果我将路径更改为“ ../ ”,那是可行的

$images = glob("../images/gallery/*.*");

or if I change the code to: 或者如果我将代码更改为:

define('BASE', $_SERVER['DOCUMENT_ROOT']);
$images = glob(BASE."/images/gallery/*.*");

Unfortunatly the sourcecode then shows some info I'm not sure I want to actually make public 不幸的是,源代码随后显示了一些信息,我不确定我是否想真正公开

e.g.    
/home/scecjwkh/htdocs/images/gallery/3.JPG

I hope the information I provided is enough to actually understand my problem. 我希望我提供的信息足以实际理解我的问题。 Not sure why I have so much trouble with a relative path oO 不知道为什么我在相对路径上这么麻烦

Thank you in advance, Stuben 预先感谢您,Stuben

What you usually do is use __DIR__ to build relative paths from the current script's folder. 通常,您使用__DIR__从当前脚本的文件夹中构建相对路径。 You can also use __DIR__ to know what part to cut from the final paths, because you should know where in the project your current file is and thus know where the root is relative to it. 您还可以使用__DIR__来了解从最终路径中剪切__DIR__部分,因为您应该知道当前文件在项目中的位置,从而知道根相对于文件的位置。

$images = glob(__DIR__."/../images/gallery/*.*"); gets the image list without caring about current working dir , after which you can use realpath(__DIR__.'/..') to figure out how much to snip from each file path. 获取图像列表而不关心当前的工作目录 ,之后您可以使用realpath(__DIR__.'/..')找出从每个文件路径中剪取多少。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM