简体   繁体   中英

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.

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:

$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

What you usually do is use __DIR__ to build relative paths from the current script's folder. 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.

$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.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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