doufang1954 2015-10-03 12:38
浏览 342

glob - 设置相对路径根目录

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 o.O

Thank you in advance, Stuben

  • 写回答

1条回答 默认 最新

  • dsxd62219570 2015-10-03 14:32
    关注

    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.

    评论

报告相同问题?

悬赏问题

  • ¥15 关于#hadoop#的问题
  • ¥15 (标签-Python|关键词-socket)
  • ¥15 keil里为什么main.c定义的函数在it.c调用不了
  • ¥50 切换TabTip键盘的输入法
  • ¥15 可否在不同线程中调用封装数据库操作的类
  • ¥15 微带串馈天线阵列每个阵元宽度计算
  • ¥15 keil的map文件中Image component sizes各项意思
  • ¥20 求个正点原子stm32f407开发版的贪吃蛇游戏
  • ¥15 划分vlan后,链路不通了?
  • ¥20 求各位懂行的人,注册表能不能看到usb使用得具体信息,干了什么,传输了什么数据