/    Sign up×
Community /Pin to ProfileBookmark

How to do correct path with function fopen?

How to do correct path with [B]function fopen[/B]?

I’m not sure if path is correct:

[code=php]

$handle = fopen(“/myfile1/filename.txt”, “r”);

[/code]

Will work / or without this?*

to post a comment
PHP

3 Comments(s)

Copy linkTweet thisAlerts:
@NogDogDec 08.2010 — The leading slash in this case -- for a file system path -- does not mean the same thing as in HTML, where it would indicate a path relative to the web root directory. If that is your intention, then:
[code=php]
$handle = fopen($_SERVER['DOCUMENT_ROOT'] . '/myfile1/filename.txt', 'r');
[/code]

If, instead, you mean relative to the directory of the current script, then just use you code without the leading "/" in the path.
Copy linkTweet thisAlerts:
@toplisekauthorDec 17.2010 — What if this file is outside public_html using $_SERVER['DOCUMENT_ROOT'] ?

$_
SERVER['DOCUMENT_ROOT'] will be direction to public_html.

Is this possible or I have to use simple /home/.../.../
Copy linkTweet thisAlerts:
@ChipzzzDec 18.2010 — What if this file is outside public_html using $_SERVER['DOCUMENT_ROOT'] ?

$_
SERVER['DOCUMENT_ROOT'] will be direction to public_html.

Is this possible or I have to use simple /home/.../.../[/QUOTE]


You have many alternatives... among them are:
[code=php]
'home/.../.../';
'~/.../.../';
$_SERVER['DOCUMENT_ROOT'] . '/../../upTwoLevels/somewhereOutsideTheWebsite/someFile';
[/code]


Anything that translates to a valid path within the operating system is ok.

Have fun ?
×

Success!

Help @toplisek 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 5.3,
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: @Yussuf4331,
tipped: article
amount: 1000 SATS,

tipper: @darkwebsites540,
tipped: article
amount: 10 SATS,

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