/    Sign up×
Community /Pin to ProfileBookmark

Relative Paths

would like to know why the following code block doesnt work?
(text files in the corresponding positions) it cant find the file when the last block does with the exact same string?

[CODE]

$comments = fopen(“/stuff/comments.txt”,”a”);
fputs($comments, $txt.”rn”);


[/CODE]

and why these do work?

[CODE]

$comments = fopen(“comments.txt”,”a”);
fputs($comments, $txt.”rn”);


[/CODE]

[CODE]

define(“CV_DIR”, “/stuff/comments.txt”);
$comments = fopen(“CV_DIR”, “a”);
fputs($comments, $txt.”rn”);


[/CODE]

thanks,

to post a comment
PHP

3 Comments(s)

Copy linkTweet thisAlerts:
@artoAug 16.2005 — [code=php]$comments = fopen("/stuff/comments.txt","a");
fputs($comments, $txt."rn");[/code]
This one doesn't work, because [I]/stuff[/I] is probably invalid path. Even if this directory exists, you may not have permissions to write there.


[code=php]$comments = fopen("comments.txt","a");
fputs($comments, $txt."rn");[/code]
This works because it appends to the file in current directory, where you have permissions.


[code=php]define("CV_DIR", "/stuff/comments.txt");
$comments = fopen("CV_DIR", "a");
fputs($comments, $txt."rn");[/code]
This one appends to file [I]CV_DIR[/I] in current directory, because you quoted constant name. If you change it to [I]fopen(CV_DIR, "a")[/I] it will stop working, because, again, directory [I]/stuff[/I] probably doesn't exist.

Arto
Copy linkTweet thisAlerts:
@WaylanderauthorAug 16.2005 — hmm figured some of it out...

wasnt what you suggested though... had the files in the right places each time

still dont know why it cant find any files besides ones in the same dir,
Copy linkTweet thisAlerts:
@NogDogAug 16.2005 — "/stuff/comments.txt" would look for the directory "stuff" right under the root directory on the current logical disk.

"stuff/comments.txt" would look for the directory "stuff" right under the directory from which the script is running.

"{$_SERVER['DOCUMENT_ROOT']}/stuff/comments.txt" would look for the directory "stuff" right under your base http document directory (e.g.: right under your "public_html" directory).

"../stuff/comments.txt" would look for the directory "stuff" under the directory which is one level up from the directory where your script is running.
×

Success!

Help @Waylander 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.5,
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,
)...