fopen/fwrite path above public_html issue - php

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.

Related

PHP cannot access local file

I'm trying to simply write to a text file with PHP and every time I try it doesn't return an error but just doesn't write. I'm doing...
$fp = fopen('file.txt', 'w');
fwrite($fp, '1') or die('error');
fclose($fp);
And very time it returns "error". file.txt is definitely in the same directory as the PHP file. I figured PHP couldn't get access to the file. I'm using Windows Server 2008. Does anyone know what the problem could be?
Two things can be happening.
One, consider setting the full path to the file within the directory like this; change /full/path/to/the/file/ to match the actual full path to the file:
$fp = fopen('/full/path/to/the/file/file.txt', 'w');
fwrite($fp, '1') or die('error');
fclose($fp);
Next, does the file itself have permissions that allow the server itself to access it. Remember, the Apache server will run as another user other than you. So need to make sucre the ownership & permissions match the Apache user.

PHP File modifies log file, but nothing is written?

<?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.

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);

Saving and opening a logfile

I have a vb.net app that webrequests a PHP file which does this:
<?php
$msg = $_GET['w'];
$logfile= 'savedrv.idps';
$fp = fopen($logfile, "w");
fwrite($fp, $msg);
fclose($fp);
?>
I want to make a PHP file that will open the new file created "savedrv.idps" so I can read it in vb.net. This is what I tried:
<?php
$logfile= 'reg.idps';
$fp = fopen($logfile, "r");
fclose($fp);
?>
How can I accomplish this?
Most likely your IIS settings for this Virtual Directory forbid a file with this extension to be browsed to. I got 404.7 error attempting to open a file in IE when I browsed to the URL: http://localhost/mysite/myvbfile.vb Using your browser, try to open the same URL that your VB program is attempting to access. I anticipate that you will get the 404.7 error in the browser window too.
You have two approaches here:
Have your PHP script write the file to a location outside of IIS where your VB.Net program can access.
Modify the Request Filtering in IIS for your site so that this file can be browsed.
(screenshot) http://support.citrix.com/article/html/images/1CTX132655-1.gif

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