PHP Developers Network

A community of PHP developers offering assistance, advice, discussion, and friendship.
 
Loading
It is currently Tue Nov 13, 2018 12:47 am

All times are UTC - 5 hours




Post new topic Reply to topic  [ 6 posts ] 
Author Message
PostPosted: Thu Apr 14, 2011 12:27 pm 
Offline
Forum Newbie

Joined: Thu Apr 14, 2011 12:14 pm
Posts: 2
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
Syntax: [ Download ] [ Hide ]
<?php
        $file_name = "home/muze/Example.txt";   //also tried <span style="font-style: italic">$file_name = "Example.txt";</span> 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.


Top
 Profile  
 
PostPosted: Mon Apr 18, 2011 9:18 am 
Offline
DevNet Master
User avatar

Joined: Wed Jun 27, 2007 9:44 am
Posts: 4313
Location: Sofia, Bulgaria
Syntax: [ Download ] [ Hide ]
$file_name = "home/muze/Example.txt";

=>
Syntax: [ Download ] [ Hide ]
$file_name = "/home/muze/Example.txt";

_________________
There are 10 types of people in this world, those who understand binary and those who don't


Top
 Profile  
 
PostPosted: Mon Apr 18, 2011 1:15 pm 
Offline
Jack of Zircons
User avatar

Joined: Thu Nov 09, 2006 9:30 pm
Posts: 4484
Location: California, USA
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:
Syntax: [ Download ] [ Hide ]
...or die(mysql_error());


Top
 Profile  
 
PostPosted: Mon Apr 18, 2011 1:24 pm 
Offline
DevNet Master
User avatar

Joined: Mon Feb 24, 2003 11:12 am
Posts: 2572
Location: The Republic of Texas
Or, since there is no MySQL in the code :wink: try using this at the top:

Syntax: [ Download ] [ Hide ]
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.


Top
 Profile  
 
PostPosted: Mon Apr 18, 2011 1:35 pm 
Offline
Jack of Zircons
User avatar

Joined: Thu Nov 09, 2006 9:30 pm
Posts: 4484
Location: California, USA
:banghead: Oh, of course!! Thanks, AbraCadaver!
(for the poster: my suggestion wasn't appropriate for a script that doesn't invoke the MySQL server.)


Top
 Profile  
 
PostPosted: Tue Apr 19, 2011 4:32 am 
Offline
DevNet Master
User avatar

Joined: Wed Jun 27, 2007 9:44 am
Posts: 4313
Location: Sofia, Bulgaria
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


Top
 Profile  
 
Display posts from previous:  Sort by  
Post new topic Reply to topic  [ 6 posts ] 

All times are UTC - 5 hours


Who is online

Users browsing this forum: No registered users and 3 guests


You cannot post new topics in this forum
You cannot reply to topics in this forum
You cannot edit your posts in this forum
You cannot delete your posts in this forum
You cannot post attachments in this forum

Jump to:  
Powered by phpBB® Forum Software © phpBB Group