PDA

View Full Version : Writing to a file


mishwar222
08-29-2010, 03:24 AM
Hello there,

I am trying to write a task in php. In the end of the task, I need to write to a file called (flag.txt) which resides on my server. So far I couldn’t make the code write to that file. I tried many ways but it doesn’t work.
My code:

<?php
// ######################## SET PHP ENVIRONMENT ###########################
error_reporting(E_ALL & ~E_NOTICE);
if (!is_object($vbulletin->db))
{
exit;
}

//the task code here

// now in the end I want to write to the file
$myFile = "XXXXXXXXXXXX/flag.txt";
$fh = fopen($myFile, 'a') or die("can't open file");
$stringData = "TRUE";
fwrite($fh, $stringData);
fclose($fh);
?>

The flag.txt permissions is 644, and the writing to a file code works out the vb, but when i put them in the task code they do work.

Thank you for the help.

Jaxel
08-29-2010, 05:10 AM
flag.txt should be 777

mishwar222
08-29-2010, 06:00 AM
flag.txt should be 777

Thank you for your response. Actually I tried it 777 and every combination that i can think of, and it didn't work. If i run the script file outside the vbulletin folder it works very well with just 644. Any other ideas?

Boofo
08-29-2010, 06:28 AM
Change:

fopen($myFile, 'a')

to:

fopen($myFile, 'w')

mishwar222
08-29-2010, 06:34 AM
Change:

fopen($myFile, 'a')

to:

fopen($myFile, 'w')

Thank you for the response. I did and it did not work.

Boofo
08-29-2010, 06:58 AM
Are you getting any errors at all when you run the code?

mishwar222
08-29-2010, 07:21 AM
No, error message, but when I tried another function:

echo file_put_contents(""XXXXXXXXXXXX/flag.txt","Hello World. Testing!");
i got this error message:

"
Warning: file_put_contents() [function.file-put-contents]: Only 0 of 5 bytes written, possibly out of free disk space in [path]/includes/cron/test.php on line 10

done"

Boofo
08-29-2010, 07:40 AM
You have double quotes at the beginning of this line. That might be the cause of your error.

""XXXXXXXXXXXX

What are you using for the path? It should be like this:

/home/yourname/public_html/forums

mishwar222
08-29-2010, 07:57 AM
Sorry for the typing error. Actually in the original code there was no double quotes.
I am using this path:

"http://Mydomain.com/forum/includes/cron/flag.txt"

Is it correct?

Also somebody said to clear the tmp folder in the server.

--------------- Added 1283072597 at 1283072597 ---------------

I changed the path and it works! I will try it with other function as well and see.

Thank you very much Boofo, and Jaxel.