fopen won't open file on Ubuntu server - php

The following function is normally able to open a file on xampp with no problems
/* converts string to xml*/
public function stringToXMLFile($string){
$file = __DIR__."/xml/feed.xml";
$fh = fopen($file, 'w+') or die("can't open file: ".$file);
fwrite($fh, $string);
fclose($fh);
}
But since uploading all my files onto an Ubuntu server I can not get fopen() to open and edit any files, is there something that I have to do on a newly installed Ubuntu Apache server which will give permission to perform such tasks?

After having discussion in the chat, the problems were:
Permissions on /var/www/xml directory.
Permissions on /var/www/xml/feed.xml file.
After adjusting them to proper values, problem was solved.

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 Permissions Denied fopen

I ran into a really bizarre problem. I am trying to perform writing to file using fopen().
This is what I tried in writetofile.php:
$fw = fopen('/test.txt', 'w');
fwrite($fw, 'hello world' . "\r\n");
fclose($fw);
This is the error I keep getting:
Warning: fopen(/test.txt):
failed to open stream: Permission denied in C:\inetpub\wwwroot\writetofile.php on line 41
Warning: fwrite() expects parameter 1 to be resource, boolean given...
I am 100% sure I have permissions to the server. I am the Administrator. Furthermore, I temporarily gave full permissions to everyone. I even tried running the php script locally, directly from the server using localhost. I am not using apache, I am using IIS. I tried restarting IIS after modifying permissions. I am not running php in safe mode.
Any idea on what might be causing this issue?
/test.txt would be a file in the ROOT directory of your filesystem, where user accounts generally do NOT have write privileges (unless you're running this code as root). This is especially true of PHP running under the webserver's user account.
You probably want just test.txt (no leading slash)` which will try to put the file into the script's "current working directory" - usually the same directory the script itself is in.
1- when you rollout website, delete all logs folder names
2- inside the code create folder name as below and create the logs insides
3- write at top of file. (during init the web)
$ClientUserName = gethostbyaddr($_SERVER['REMOTE_ADDR']);
function Data_Log($dataline)
{
global $ClientUserName;
$dir = 'UserInputLog' ;
$fileName = $ClientUserName. '_ServerWebLog.txt';
if(is_dir($dir) === false)
mkdir($dir);
$fileName = $dir. '\\'.$fileName;
$myfile = fopen($fileName, "a") or die("Unable to open file!");
fwrite($myfile, "$dataline\r\n");
fclose($myfile);
}

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

PHP fopen doesn't work although file checks as writable

Helping a friend with an issue he's having. His site is using a fopen function to open a file and save the changes to it but the fopen is failing with a "Permission Denied" error. The file is on a remote server and permissions seem to be correct.
The code he is using is...
if (isset($_POST['save'])) {
$filepath = "string.txt";
if (file_exists($filepath)) {
$file = fopen($filepath, "w+");
fwrite($file, $_POST['save']);
fclose($file);
}
}
I used VolkerK's code here php fopen returns false but file is readable/writable to ascertain the read/write permissions and get the following.
PHP Version: 5.4.9
Uname: Windows NT
{File} exists
{File} is readable
{File} is writable
Last error: array(4){["type']=>int(2)["message"]=>string(131 "fopen({file}):failed to open stream: Permission denied.
At first I thought it was a file permissions problem but I think if the file is being seen by PHP as writable this should not be the issue, do I read that correctly?
Tried FTP and got there. Had to properly set context options for FTP connection though.
Thanks for pointing me in the right direction.
if (isset($_POST['save'])) {
//Setup FTP connection
$ftp = "ftp://user:password#example.com/filepath/filename.txt";
//Set FTP Options
$stream_options = array('ftp' => array('overwrite' => true));
$stream_context = stream_context_create($stream_options);
//Open file and write changes
if (file_exists($ftp)) {
$file = fopen($ftp, "w", 0 , $stream_context);
fwrite($file, $_POST['save']);
fclose($file);
}
}

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