PHP File modifies log file, but nothing is written? - php

<?php
define('LOG','testfolder/test.html');
$fp = fopen(LOG, 'a+');
fwrite($fp, $_ENV['REDIRECT_REMOTE_USER'] . "\n");
fclose($fp);
?>
While using this code the test.html gets modified at the right times, but nothing is written but blank space. What's the coding error here?
PHP file has 644 permissions, test.html has 744 permissions.

Related

fopen/fwrite path above public_html issue

I'm testing php scripts on my web server.
I'm using a basic fopen / fwrite to write data to file.
The following scripts works perfectly in public_html/ but fails to work in folders above "I'm trying in /test/"
centOS with WHM/cPanel, latest.
<?php
$fp = fopen('lidn.sh', 'w');
fwrite($fp, 'Cats chase mice');
fclose($fp);
?>
This works fine. However:
<?php
$fp = fopen('/test/lidn.sh', 'w');
fwrite($fp, 'Cats chase mice');
fclose($fp);
?>
doesn't write anything to file.
I need php to output data to a ".sh" file, so having it inside public_html would, I think, be a gigantic security flaw.
The goal is to securely output data to a ".sh" via PHP
You should check the test folder permissions.

Why is this PHP code not opening or writing to a file?

I am using PHP to write information to a text file. I have used this code before, and it worked, but in a new project, it is not working.
I think the problem might be a permissions issue. The server is Ubuntu.
$message = "RESULTS = " . $data . "\n";
$fh = fopen("test.txt", 'a'); //open file and create if does not exist
fwrite($fh, "\n\n******* " . date('l, F j, Y (g:i A)') . " *********\n\n"); //Just for spacing in log file
fwrite($fh, $message); //write data
fclose($fh); //close file
When I debug using Netbeans, fopen() returns a boolean value of 0, which I take to means it failed, but I'm not sure what the reason is.
Is there anything wrong with the code above? If not, what permissions settings do I need on the directories or files involved to ensure I'm not getting blocked by a permission setting?
Is there anything else I should be looking for?
Check your directory
add dot-slash (./) at the beginning of your directory
$fh = fopen("./test.txt",'a'); //open file and create if does not exist
In Ubuntu ,
for a single file :
sudo chmod 777 /var/www/your-path/file.txt
for a directory
sudo chmod -R 777 /var/www/your-path

cannot edit any php files using specific functions

I cannot update any txt files using php. When I write a simple code like the following:
<?php
// create file pointer
$fp = fopen("C:/Users/jj/bob.txt", 'w') or die('Could not open file, or fike does not exist and failed to create.');
$mytext = '<b>hi. This is my test</b>';
// write text to file
fwrite($fp, $mytext) or die('Could not write to file.');
$content = file("C:/Users/jj/bob.txt");
// close file
fclose($fp);
?>
Both files do exist in the folder. I just cannot see any updates on bob.txt.
Is this a permission error in windows? It works fine on my laptop at home. I also cannot change the php files on my website, using filezilla.
It may very well be a file permissions issue.
Try your code using a direct pointer to the file instead of a path to it, using the following code:
I added a chmod directive. See the comments above chmod ($file, 0644);
Tested succesfully on my hosted WWW website:
<?php
// create file pointer
$file = "bob.txt";
$fp = fopen($file, 'w') or die('Could not open file, or fike does not exist and failed to create.');
// chmod ($file, 0777); // or use 0777 if 0644 does not work
chmod ($file, 0644);
$mytext = '<b>hi. This is my test</b>';
// write text to file
fwrite($fp, $mytext) or die('Could not write to file.');
$content = file("bob.txt");
// close file
fclose($fp);
?>
Perhaps you have to set the permission from bob.txt to 0777 (or something else). In FileZilla it is very easy, because you can just check the permissions you want.

not writing in text file on server side using php

I'm trying to put the results from the database into a text file after fetching it, and if the text file doesnt exist it creates one and put the data into it. after that i showed the the data by extracting from those txt files in another page. the problem is it is working fine in my local xampp server. but i dont know why its not working in my aws account. the result page shows blank in aws elastic ip.but its connecting to the database and if there is no results found it also shows the message. so its just not putting the datas into the txt file nt even creating it. i tried by creating the txt file manually. still not working. but its working fine in my local server. Here is the code i used-
$wineFile ="WineName.txt";
file_put_contents($wineFile, $W);
From your comments , seems like there is not enough permission. You need to CHMOD 777 on the file.
So make use of CHMOD command on PHP
Something like this
$file ="WineName.txt";
chmod($file, 0777);
$fp = fopen($file, 'w');
fwrite($fp, $content);
fclose($fp);
try
$wineFile = "WineName.txt";
$fh = fopen($wineFile, 'w') or die("can't open file");
$stringData = "Tasty Wine\n";
fwrite($fh, $stringData);
fclose($fh);

Unable to write into a file using PHP in sourceforge

I want to HTTP POST data to a PHP file which will in-turn write the data into a file. The script I used is as follows,
<?php
#error_reporting(E_ERROR | E_PARSE);
$msglen=strlen($_POST["msgarea"]);
$msg=$_POST["msgarea"];
$fp = fopen("dinesh.txt", 'w');
fwrite($fp, $msg);
fclose($fp);
echo "Data Written -> $msg";
?>
I am hosting this script file in sourceforge.
I have already just created the empty file dinesh.txt and placed the file in the same directory as that of the script file. But unfortunately its not written in the file.
What is the reason ?
Thanks in advance
Check that $_POST["msgarea"] is not empty and the file is writeable for the user who tries to write it.
And check the logs for errors of course.
<?php
error_reporting(E_ALL);
var_dump($_POST["msgarea"]);
$msglen=strlen($_POST["msgarea"]);
$msg=$_POST["msgarea"];
$fp = fopen("dinesh.txt", 'w');
$result = fwrite($fp, $msg);
fclose($fp);
if ($result) {
echo "Data Written -> $result";
} else {
echo "Error";
}
I think you are writing in read-only webspace, check file permissions and path
From: http://sourceforge.net/apps/trac/sourceforge/ticket/2772
In generally it is not allowed to write to folders and files that reside in the project's web space. If you need to write to the file system you should use the folder named persistent that is on the same level as htdocs.
I use on my computer:
$ mkdir persistent
$ chmod 0777 persistent
$ scp -r persistent my_account#web.sourceforge.net:/home/project-web/my_project/
And set in PHP:
$fp = fopen("../persistent/dinesh.txt", 'w');
And it works!
Edit:
You can get access for SSH console for 4 hours (https://sourceforge.net/apps/trac/sourceforge/wiki/Shell%20service). And you can go to web directory of your project, make dirs, set privileges, remove files etc. Midnight Commander is available.
you can, check that folder permission, do they have 777 permission
$myFile = "testFile.txt";
$fh = fopen($myFile, 'w') or die("can't open file");
$stringData = "Bobby Bopper\n";
fwrite($fh, $stringData);
fclose($fh);

Categories