/    Sign up×
Community /Pin to ProfileBookmark

[RESOLVED] scandir() won’t accept my path?

I am trying to use scandir to list the contents of a folder somewhere else on the server.

The only way that I have been able to get this to work is with an absolute path from the linux root ie: /mnt/path/to/folder

I want to be able to use a path starting from the root of my domain name ie:
/directory_containing_index.php/otherfolder/otherfolder
so that the script will easily be able to be moved or used on any website any where without manual modification of the script.

Does anyone know what I need to do to get this to work?

I can provide my script if that helps. I figured it wasn’t necessary though.

to post a comment
PHP

5 Comments(s)

Copy linkTweet thisAlerts:
@NogDogOct 21.2011 — Use
[code=php]
$_SERVER['DOCUMENT_ROOT'] . "/path/to/file"
[/code]
Copy linkTweet thisAlerts:
@DrPepperauthorOct 21.2011 — Thanks NogDog!!!

I have now realized the importance of studying and knowing all of the pre set variables functions, etc. available to me. I could have saved a lot of my hair had I read through the php documentation on preset variables in its entirety and not just the few that I thought I needed.
Copy linkTweet thisAlerts:
@NogDogOct 21.2011 — It's both a curse and a blessing: PHP has functions, constants, and variables for all sorts of things, but unless you have a photographic memory, you'll probably never memorize all of them.
Copy linkTweet thisAlerts:
@DrPepperauthorOct 21.2011 — Just clarification for anyone who finds this in a search...

scandir() needs a path starting with the root of the server

(i think there might be a work around with the context parameter but I am way to confused about that to make it work)

$_SERVER['DOCUMENT_ROOT'] provides the path from the root of the server to the end of your domain name ie:

/mnt/path/to/domain/name/example.com

to access directories with scandir() in your domain space simply add the relative path to the end of $_SERVER['DOCUMENT_ROOT'] in any manner you choose ie:

[code=php]
$path = $_SERVER['DOCUMENT_ROOT'] . "/path/to/file/or/folder";

[/code]
Copy linkTweet thisAlerts:
@NogDogOct 22.2011 — You can probably use a relative path, too, e.g.:
[code=php]
scandir("../../up_2_levels/foo/bar.txt")
[/code]

But that can be a maintenance headache if you decide to move the calling file, or cut-and-paste the code to another file, or use it from an include; so I generally prefer either the $_SERVER['DOCUMENT_ROOT'] technique, or defining some sort of base application path constant in a common config/include file or such, so that you never have to worry about where you are in the directory tree at any given moment -- you just need to know where the directory/file is that you want to reference.
×

Success!

Help @DrPepper spread the word by sharing this article on Twitter...

Tweet This
Sign in
Forgot password?
Sign in with TwitchSign in with GithubCreate Account
about: ({
version: 0.1.9 BETA 6.17,
whats_new: community page,
up_next: more Davinci•003 tasks,
coming_soon: events calendar,
social: @webDeveloperHQ
});

legal: ({
terms: of use,
privacy: policy
});
changelog: (
version: 0.1.9,
notes: added community page

version: 0.1.8,
notes: added Davinci•003

version: 0.1.7,
notes: upvote answers to bounties

version: 0.1.6,
notes: article editor refresh
)...
recent_tips: (
tipper: @nearjob,
tipped: article
amount: 1000 SATS,

tipper: @meenaratha,
tipped: article
amount: 1000 SATS,

tipper: @meenaratha,
tipped: article
amount: 1000 SATS,
)...