Problem with fopen(), fwrite()

PHP programming forum. Ask questions or help people concerning PHP code. Don't understand a function? Need help implementing a class? Don't understand a class? Here is where to ask. Remember to do your homework!

Moderator: General Moderators

Post Reply
Azfar
Forum Newbie
Posts: 5
Joined: Sat May 02, 2009 10:12 pm

Problem with fopen(), fwrite()

Post by Azfar »

Dear fellows,

It's been few days since I started learning to program in PHP.

Following is my code:

Code: Select all

 
<?php
 
$fp = fopen("data.txt", w+);
fwrite ($fp, "Azfar");
fclose ($fp);
 
$fp = fopen("/home/Azfar/data.txt", w+);
fwrite ($fp, "Azfar");
fclose ($fp);
 
?>
When I access the web-page (.htm) in which the code is placed with my browser, neither do I see any error message, nor is any file created (the files don't pre-exist; I am depending upon the fopen() functionality to create them).

Any assistance from your side would be appreciated.
Last edited by Benjamin on Sat May 02, 2009 10:48 pm, edited 1 time in total.
Reason: Changed code type from text to php.
User avatar
requinix
Spammer :|
Posts: 6617
Joined: Wed Oct 15, 2008 2:35 am
Location: WA, USA

Re: Problem with fopen(), fwrite()

Post by requinix »

When writing code, your php.ini should be configured to show errors.

Code: Select all

; change these settings:
error_reporting = E_ALL
display_errors = on
Then reboot your server.

As for the problem,

Code: Select all

$fp = fopen("data.txt", w+);
"w+" doesn't make sense.
"w" isn't a constant. Since you didn't put quotes around it, that's what PHP assumes it is. Except the + means addition, which needs something on the left and the right (the two things to add together). You didn't give something on the right.
"w+" is supposed to be a string, just like "data.txt".

When you turn up error_reporting and enable display_errors you'll see a parse error which tells you that's the problem (but not in as much detail as that).
Azfar
Forum Newbie
Posts: 5
Joined: Sat May 02, 2009 10:12 pm

Re: Problem with fopen(), fwrite()

Post by Azfar »

Dear tasairis,

Thank you for your reply.

I did change the "display_errors" setting from "OFF" to "ON" in the "php.ini" file (/etc/php.ini). The "error-reporting" variable was already set to "E_ALL". Still, I see no error-related message when I access the web-page.

I made the other changes as well. But still the situation seems to be the same.

Regarding "include_path"

I read somewhere that one doesn't need to specify path when "include_path" variable in /etc/php.ini file is set properly.

My php.ini file reads as follows:
; UNIX: "/path1:/path2"
;include_path = ".:/php/includes"
;
; Windows: "\path1;\path2"
;include_path = ".;c:\php\includes"
Do I need to remove the semi-colon following ".:/php/includes"? How does PHP read the php.ini file? What does ".:/php/includes" imply?

Regarding Relative and Absolute Paths

I understand that $Document_Root in PHP corresponds to DocumentRoot variable in the web-server's main configuration file.

Is it necessary to include it in the path if my file lies in the working directory (/var/www/html/ in my case)?

How can one reference some file lying in the parent directory of $Document_Root using Relative path?

Are there any additional settings required for using absolute/relative paths?

I would be grateful if anyone addresses these questions.
User avatar
Bill H
DevNet Resident
Posts: 1136
Joined: Sat Jun 01, 2002 10:16 am
Location: San Diego CA
Contact:

Re: Problem with fopen(), fwrite()

Post by Bill H »

Did you try this as tasairis suggested?

Code: Select all

$fp = fopen("data.txt", "w+");
Azfar
Forum Newbie
Posts: 5
Joined: Sat May 02, 2009 10:12 pm

Re: Problem with fopen(), fwrite()

Post by Azfar »

Dear Bill H,

Yes I did; problem isn't resolved as yet.
User avatar
requinix
Spammer :|
Posts: 6617
Joined: Wed Oct 15, 2008 2:35 am
Location: WA, USA

Re: Problem with fopen(), fwrite()

Post by requinix »

You did do that for both of the fopens, right?
Azfar
Forum Newbie
Posts: 5
Joined: Sat May 02, 2009 10:12 pm

Re: Problem with fopen(), fwrite()

Post by Azfar »

tasairis wrote:You did do that for both of the fopens, right?
Dear tasairis,

The problem is resolved. Following were the main causes of the problem, other than the syntax error:
  • As you pointed out earlier, the "display_error" variable should have been set to ON.

    After making this change, I got to know what were the errors; which in my case were the "can't open stream" permission denials. All the subsequent errors were related to invalid file handler.
  • SELinux was preventing the HTTP daemon to access the /home/Azfar directory, or any other directory for that matter.

    I switched SELinux into it's permissive mode (temporarily shutting it off) by the following command at the CLI.

    Code: Select all

    echo 0 > /selinux/enforce
This has partially resolved the problem; I, now, have two text files written in the desired locations. However, I am looking into ways I could accomplish the same without switching SELinux off. I understand that there is no way one can go about file/folder permission settings without using "chmod" command.
User avatar
Bill H
DevNet Resident
Posts: 1136
Joined: Sat Jun 01, 2002 10:16 am
Location: San Diego CA
Contact:

Re: Problem with fopen(), fwrite()

Post by Bill H »

The "chmod" merely changes the permission settings of the target directory. You are using the default directioy, for which that would not be desireable to do that, but is there any reason why you cannot create a specified directory for your files, chmod() it to 777 and use it?
Azfar
Forum Newbie
Posts: 5
Joined: Sat May 02, 2009 10:12 pm

Re: Problem with fopen(), fwrite()

Post by Azfar »

Bill H wrote:The "chmod" merely changes the permission settings of the target directory. You are using the default directioy, for which that would not be desireable to do that, but is there any reason why you cannot create a specified directory for your files, chmod() it to 777 and use it?
Dear Bill H,

Thank you for your comment.

No, there isn't any specific reason for doing so.

I've tried the following code, and have found my attempt succesful to my delight.

Code: Select all

 
<?php
 
$base=$_SERVER ['DOCUMENT_ROOT'];
 
/* The "register_global", in PHP.ini, is set to OFF by default; and I am keeping it that way.*/
 
$fp = fopen ("$base/../../public_dir/text1.txt", "w+");
fwrite ($fp, "Azfar");
fclose($fp);
?>
 
 
 
 
 
 


As one can note, public_dir is located two levels up the working directory.
Post Reply