Page 1 of 1

fopen PHP failing on Ubuntu 10.10

Posted: Thu Apr 14, 2011 12:27 pm
by muze
hello guys..I dont know whether this is right place to post this question. I recently installed LAMP on my ubuntu 10.10 (which is fine) and tried to write a simple php program which writes hello world in it. Then I placed this file in /var/www and using firefox ran the code. Here is what it looks like

Code: Select all

<?php
	$file_name = "home/muze/Example.txt";	//also tried [i]$file_name = "Example.txt";[/i] but in vain
	$text = "Muzammil";
	$file = fopen($file_name, "w") or die('Unable to open file');
	fwrite($file, $text) or die('Unable to write text');
	$contents = file_get_contents($file) or die('Unable to get contents');
	echo $contents;
	fclose($file);
?>
But this shows error ..."Unable to open file". Whats the problem with code or any configuration that I am missing.

Re: fopen PHP failing on Ubuntu 10.10

Posted: Mon Apr 18, 2011 9:18 am
by VladSun

Code: Select all

$file_name = "home/muze/Example.txt";
=>

Code: Select all

$file_name = "/home/muze/Example.txt";

Re: fopen PHP failing on Ubuntu 10.10

Posted: Mon Apr 18, 2011 1:15 pm
by califdon
As Vladsun pointed out, the "home" directory is normally in the file system's root directory, not subordinate to the web server's root directory, requiring you to precede the path with a "/". That's no doubt your problem, but I wanted to point out that you would have a lot more help in diagnosing the problem if you include the mysql_error() message in your "die" statements, like this:

Code: Select all

...or die(mysql_error());

Re: fopen PHP failing on Ubuntu 10.10

Posted: Mon Apr 18, 2011 1:24 pm
by AbraCadaver
Or, since there is no MySQL in the code :wink: try using this at the top:

Code: Select all

error_reporting(E_ALL);
ini_set('display_errors', '1');

Re: fopen PHP failing on Ubuntu 10.10

Posted: Mon Apr 18, 2011 1:35 pm
by califdon
:banghead: Oh, of course!! Thanks, AbraCadaver!
(for the poster: my suggestion wasn't appropriate for a script that doesn't invoke the MySQL server.)

Re: fopen PHP failing on Ubuntu 10.10

Posted: Tue Apr 19, 2011 4:32 am
by VladSun
Also, you may wish to use file_put_contents() instead of opening/writing/closing the file.