Page 1 of 1

Problem with fopen(), fwrite()

Posted: Sat May 02, 2009 10:23 pm
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.

Re: Problem with fopen(), fwrite()

Posted: Sat May 02, 2009 10:45 pm
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).

Re: Problem with fopen(), fwrite()

Posted: Sat May 02, 2009 11:56 pm
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.

Re: Problem with fopen(), fwrite()

Posted: Sun May 03, 2009 12:47 am
by Bill H
Did you try this as tasairis suggested?

Code: Select all

$fp = fopen("data.txt", "w+");

Re: Problem with fopen(), fwrite()

Posted: Sun May 03, 2009 4:11 am
by Azfar
Dear Bill H,

Yes I did; problem isn't resolved as yet.

Re: Problem with fopen(), fwrite()

Posted: Sun May 03, 2009 5:16 am
by requinix
You did do that for both of the fopens, right?

Re: Problem with fopen(), fwrite()

Posted: Sun May 03, 2009 5:35 am
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.

Re: Problem with fopen(), fwrite()

Posted: Sun May 03, 2009 8:22 am
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?

Re: Problem with fopen(), fwrite()

Posted: Sun May 03, 2009 10:07 am
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.