Page 1 of 1

function doesnt write to the file

Posted: Fri Feb 02, 2007 12:50 am
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

Posted: Fri Feb 02, 2007 1:36 am
by gavin1996

Posted: Fri Feb 02, 2007 2:42 am
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.

Posted: Fri Feb 02, 2007 2:43 am
by dude81
simple fopen doesn't work, though I gave 777 permissions and total user is apache... what could be this mysterious problem

Posted: Fri Feb 02, 2007 4:09 am
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"

Posted: Fri Feb 02, 2007 6:16 am
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

Posted: Fri Feb 02, 2007 6:21 am
by superdezign
When you get the variable $filename, try using $_SERVER['DOCUMENT_ROOT']."whereever/filename.blah" as your basis for all filenames.

Posted: Fri Feb 02, 2007 6:23 am
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.

Posted: Fri Feb 02, 2007 6:35 am
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.