fopen PHP failing on Ubuntu 10.10

Whether you are using Linux on the desktop or as a server, it's still good that you're using Linux. Linux related questions go here.

Moderator: General Moderators

Post Reply
muze
Forum Newbie
Posts: 2
Joined: Thu Apr 14, 2011 12:14 pm

fopen PHP failing on Ubuntu 10.10

Post 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.
User avatar
VladSun
DevNet Master
Posts: 4313
Joined: Wed Jun 27, 2007 9:44 am
Location: Sofia, Bulgaria

Re: fopen PHP failing on Ubuntu 10.10

Post by VladSun »

Code: Select all

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

Code: Select all

$file_name = "/home/muze/Example.txt";
There are 10 types of people in this world, those who understand binary and those who don't
User avatar
califdon
Jack of Zircons
Posts: 4484
Joined: Thu Nov 09, 2006 8:30 pm
Location: California, USA

Re: fopen PHP failing on Ubuntu 10.10

Post 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());
User avatar
AbraCadaver
DevNet Master
Posts: 2572
Joined: Mon Feb 24, 2003 10:12 am
Location: The Republic of Texas
Contact:

Re: fopen PHP failing on Ubuntu 10.10

Post 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');
mysql_function(): WARNING: This extension is deprecated as of PHP 5.5.0, and will be removed in the future. Instead, the MySQLi or PDO_MySQLextension should be used. See also MySQL: choosing an API guide and related FAQ for more information.
User avatar
califdon
Jack of Zircons
Posts: 4484
Joined: Thu Nov 09, 2006 8:30 pm
Location: California, USA

Re: fopen PHP failing on Ubuntu 10.10

Post 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.)
User avatar
VladSun
DevNet Master
Posts: 4313
Joined: Wed Jun 27, 2007 9:44 am
Location: Sofia, Bulgaria

Re: fopen PHP failing on Ubuntu 10.10

Post by VladSun »

Also, you may wish to use file_put_contents() instead of opening/writing/closing the file.
There are 10 types of people in this world, those who understand binary and those who don't
Post Reply