PHP Permissions Denied fopen - php

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

Related

fopen: failed to open stream: Permission denied in PHP on Mac [duplicate]

This question already has answers here:
PHP - Failed to open stream : No such file or directory
(10 answers)
Closed 6 years ago.
I wrote this piece of code:
if (file_exists("testfile.rtf")) echo "file exists.";
else echo "file doesn't exist.";
$fh = fopen("testfile.rtf", 'w') or die("impossible to open the file");
$text = <<< _END
Hi buddies,
I write for the first time
in a PHP file!
_END;
fwrite($fh, $text) or die("Impossible to write in the file");
fclose($fh);
echo ("writing in the file 'testfile.rtf' succeed.");
But when I run it it says:
fopen(testfile.rtf): failed to open stream: Permission denied in /Applications/XAMPP/xamppfiles/htdocs/test/test1.php on line 64
impossible to open the file
I use XAMPP on my MAC to work on local server.
I found several topics where some people had the same problem, and solved it using something called "chmod 777" but I don't know what that means, where to write this, etc.
The error you are seeing simply means that your php script does not have the permission to open testfile.rtf for writing.
do not "chmod 777" everything anytime you are getting permission issues, this might introduce security problems later! instead- give the correct users the correct permissions.
In this case:
Since you are running XAMPP, then you need to make sure that your web server gets the write permission to the specific path you are trying to create new files in and\or edit existing files.
In the case of OSX and apache, I believe the webserver's user is named "_www".
Here's a small example (you might need to change it to your needs):
sudo chown -R _www:_www /path/to/folder
The example above will give _www (webserver) ownership of the /path/to/folder and all of the files in it.
Also: before you do any command-line based administration, you might want to read a little bit about unix commands, such as chown,chmod,etc..
https://www.techonthenet.com/linux/commands/chown.php
https://www.techonthenet.com/linux/commands/chmod.php
Hope it helps a bit!

PHP datelog localhost on mac permission denied

I am trying to make a datelog file with PHP. I am running this on xampp on a macbook. It says all the time that permission is denied. How do i fix this?
the error that i am getting is:
Warning: fopen(Friday-01-April-16.txt): failed to open stream: Permission denied in /Applications/XAMPP/xamppfiles/htdocs/aowigman/index.php on line 6
Unable to open file!
The code:
<?php
date_default_timezone_set("Europe/Amsterdam");
$datum = date("l-d-F-y");
$ip = $_SERVER["REMOTE_ADDR"];
$myfile = fopen("$datum".".txt", "a+") or die("Unable to open file!");
$txt = $datum. " ". $ip."\n";
fwrite($myfile, $txt);
fclose($myfile);
?>
Thanks in advance!
You may need to change the permissions as an administrator. Open up terminal on your Mac and then open the directory that file is located in. Then type:
sudo chmod 777 -R on directory file located
Go to your Xammp directory, right click and select Read informations. At the bottom of it, click on the chains, enter your password and change permission to read and write to everyone. Now, click on the cog and choose Apply to included elements.
Refresh your page that includes the code, and tadam, fopen(), file_put_contents(), etc, will work.

How to save file into network share folder

I am trying to save xml file into share folder.But I am getting below error.
Warning: fopen(\\XYZ-server\xml\10052.xml): failed to open stream: Permission denied
PHP code
$myFile = "\\\\XYZ-server-server\\xml\\$caseref.xml";
$fh = fopen($myFile, 'w') or die("can't open file");
I check that folder's permission.Everyone can create/delete/update files.
is there a way you can specify the credentials to be used to access a network share?
Have you checked what username PHP is running under and that that user has the right permissions? On our IIS server it runs as IUSR not as the user who is actually using the website.

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_put_contents works with file, no file gets "failed to open stream: No such file or directory"

The PHP file_put_contents function works perfectly fine when the file exists. If the file does not exist I receive the error "failed to open stream: No such file or directory".
$file = '../templates/stuff.xml';
if (!file_exists($file)) {$file = '../'.$file;}
$var['xhtml'] = $_POST['post_xhtml'];
$file_contents = serialize($var);
file_put_contents($file,$file_contents);
I tried the same thing with fopen and fwrite using the correct flags (w, w+ and tried the others) yet still had the same problem: if the file already existed it worked just fine, otherwise it would give me the same error message.
I know the file path is correct. I'm using Windows 7 for local development.
When the file doesn't exist, you are prepending ../ to the path, thus you are trying to write to:
../../templates/stuff.xml
Are you sure that the folder ../../templates exists (and that PHP can write to it)?
Before you write to a file, you need to check that the folder exists. Try using is_dir():
if(is_dir(dirname($file))){
file_put_contents($file, $file_contents);
}

Categories