function doesnt write to the file

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
User avatar
dude81
Forum Regular
Posts: 509
Joined: Mon Aug 29, 2005 6:26 am
Location: Pearls City

function doesnt write to the file

Post by dude81 »

Hi,
this is a function that is avaialable on php.net. Im using this function for writing the content into file.

Code: Select all

function file_write($filename, &$content) {

       if (!is_writable($filename)) {
           if (!chmod($filename, 0666)) {
                 echo "Cannot change the mode of file ($filename)";
                 exit;
           };
       }
       if (!$fp = @fopen($filename, "w")) {
           echo "Cannot open file ($filename)";
           exit;
       }
       if (fwrite($fp, $content) === FALSE) {
           echo "Cannot write to file ($filename)";
           exit;
       }
       if (!fclose($fp)) {
           echo "Cannot close file ($filename)";
           exit;
       }
  }
I've created a file called $filename in the directory where this script runs. It returns the error cannot change the mode file file_content.xml. I gave all the permissons possible on the file. But it still doesnt write.. What could be the problem

Thank You
gavin1996
Forum Newbie
Posts: 22
Joined: Tue Jan 30, 2007 8:30 pm

Post by gavin1996 »

User avatar
Kieran Huggins
DevNet Master
Posts: 3635
Joined: Wed Dec 06, 2006 4:14 pm
Location: Toronto, Canada
Contact:

Post by Kieran Huggins »

@gavin: I think your use of giant, bold, coloured text is against forum rules. It's also tacky as hell. Please stop.
User avatar
dude81
Forum Regular
Posts: 509
Joined: Mon Aug 29, 2005 6:26 am
Location: Pearls City

Post by dude81 »

simple fopen doesn't work, though I gave 777 permissions and total user is apache... what could be this mysterious problem
facets
Forum Contributor
Posts: 273
Joined: Wed Apr 13, 2005 1:53 am
Location: Detroit

Post by facets »

I've created a file called $filename
That's the variable not the actual filename.
So in your code you should have something like

Code: Select all

$filename = "Data1.xml"
User avatar
dude81
Forum Regular
Posts: 509
Joined: Mon Aug 29, 2005 6:26 am
Location: Pearls City

Post by dude81 »

Though this function tmpname helped me solving my problem. I still want to know why I normal fopen didn't work properly.
That's the variable not the actual filename.
So in your code you should have something like

Code: Select all

$filename = "Data1.xml";
Agreed to your point. Then how do I call while this file is in someother directory.
I made filename almost like this

Code: Select all

$filename ="/opt/www/xampp/htdocs/myapp/files/recog.xml";
Will it not open the above file? I think it should.... Are my basics wrong??:roll:

@gavin, those big red color text really pains, Kindly make it small
User avatar
superdezign
DevNet Master
Posts: 4135
Joined: Sat Jan 20, 2007 11:06 pm

Post by superdezign »

When you get the variable $filename, try using $_SERVER['DOCUMENT_ROOT']."whereever/filename.blah" as your basis for all filenames.
facets
Forum Contributor
Posts: 273
Joined: Wed Apr 13, 2005 1:53 am
Location: Detroit

Post by facets »

I think your path should be within and limited to your webroot.
But I may be wrong. Perhaps try the file from within the same folder as your script.
User avatar
superdezign
DevNet Master
Posts: 4135
Joined: Sat Jan 20, 2007 11:06 pm

Post by superdezign »

Well it's always good to know what you $_SERVER['DOCUMENT_ROOT'] is, so be sure to echo it to yourself first. I've found that it's the most effective way of getting filename's to work correctly.
Post Reply