/    Sign up×
Community /Pin to ProfileBookmark

Cannot create text file in PHP

Hi all,

I am facing a very strange problem which I myself cannot sort out. Let’s hope anyone in this forum can come up with a solution.

My platform is Linux and I am working on a project in PHP 4.3.4 with MySql 4.0.16 as the backend and Apache/2.0.48. I want to retrieve some email addresses from the backend and create a text file with the addresses.

To accomplish the above, I am using the PHP function fopen() to open the file, fwrite() to write in the file and fclose() to finally close the file.

However, when I am executing the page, the following warning crops up as a result of which the text file is not being created:

Warning: fwrite(): supplied argument is not a valid stream resource
Warning: fclose(): supplied argument is not a valid stream resource

However, please note that when I am executing the same page in Windows platform, it is working as expected without any problems and giving the desired output.

My study of the problem is that any file operations are not being permitted on the Linux server.

Can anyone please suggest any outcome?
Any help in this context will be highly appreciated.

Thanxs.

to post a comment
PHP

14 Comments(s)

Copy linkTweet thisAlerts:
@JonaJan 16.2004 — [font=arial]Try using the @ operator before your fopen function:[/font]

[code=php]
@fopen("file.txt",'r');
[/code]


[b][J]ona[/b]
Copy linkTweet thisAlerts:
@anirban2k2001authorJan 16.2004 — I've tried it.

It suppresses only the warning but does not create the text file.
Copy linkTweet thisAlerts:
@JonaJan 16.2004 — [i]Originally posted by anirban2k2001 [/i]

[B]I've tried it.



It suppresses only the warning but does not create the text file. [/B]
[/QUOTE]


[font=arial]The code I showed you is only set to read the file. Change the 'r' to 'w' for writing, or r+ for reading and writing.[/font]

[b][J]ona[/b]
Copy linkTweet thisAlerts:
@anirban2k2001authorJan 16.2004 — I will show you in brief what I am doing:

@$fp = fopen("messages/".$txt_fl_nm.".txt","a");

where $txt_fl_nm contains the file name

@fwrite($fp,$output);

where $output contains the email address string

@fclose($fp);
Copy linkTweet thisAlerts:
@JonaJan 16.2004 — [font=arial]Try...[/font]

[code=php]
$fp = @fopen("messages/".$txt_fl_nm.".txt","a");
fwrite($fp,$output);
fclose($fp);
[/code]


[b][J]ona[/b]
Copy linkTweet thisAlerts:
@anirban2k2001authorJan 16.2004 — Tried it, but again the same warning is displayed for fwrite() and fclose()
Copy linkTweet thisAlerts:
@JonaJan 16.2004 — [font=arial]Hmm, that's odd... Are there any file permissions that are set differently for that folder?[/font]

[b][J]ona[/b]
Copy linkTweet thisAlerts:
@anirban2k2001authorJan 16.2004 — No.

The permissions are the same as the other folders.

For your reference, I will state them.

The messages folder has the following attributes -

dwrxr-xr-x
Copy linkTweet thisAlerts:
@JonaJan 16.2004 — [font=arial]Very odd. I just happened to be using some code that I'd written about a month ago, which has worked fine, and it produced the same error! This is very odd, indeed, as I've never gotten this error - there is nothing wrong with my script. ? I'll figure it out eventually...[/font]

[b][J]ona[/b]
Copy linkTweet thisAlerts:
@JonaJan 16.2004 — [font=arial]I'm out for the night... Not sure why it's still not working... I'll look into it again tomorrow... (Unless this forum's savior, pyro, intervenes for the small people [us].) ?[/font]

[b][J]ona[/b]
Copy linkTweet thisAlerts:
@anirban2k2001authorJan 16.2004 — Thanxs for the initiative taken.
Copy linkTweet thisAlerts:
@pyroJan 16.2004 — Did you give the messages directory write permissions? *nix servers introduce a wonderful thing not known to Windows called CHMOD. :p
Copy linkTweet thisAlerts:
@JonaJan 16.2004 — [i]Originally posted by pyro [/i]

[B]Did you give the messages directory write permissions? *nix servers introduce a wonderful thing not known to Windows called CHMOD. :p [/B][/QUOTE]


[font=arial]I think I mentioned file permissions - I just didn't specifically say, "CHMOD."[/font] ?

[b][J]ona[/b]
Copy linkTweet thisAlerts:
@pyroJan 16.2004 — Ah, yes you did... And now that I look close he said the directory is set to -rwxr-xr-x (755)... Try -rw-rw-rw- (666) (or -rwxrw-rw- [766]).
×

Success!

Help @anirban2k2001 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.19,
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: @AriseFacilitySolutions09,
tipped: article
amount: 1000 SATS,

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

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