PHP - fopen() failed to open stream: permission denied Windows 10 - php

Good evening,
I am fairly new to PHP, right now I am getting permissions errors. I am using a Windows 10 enviroment with PHP, and composer installed. I am trying to pull files from an array of url's, but fopen gives me a permission error. Here is the code:
$fp = fopen($result['url'], 'r');
$out= fopen($filePath, 'w');
while ($buf = fread($fp, 5e+9)) {
fwrite($out, $buf);
usleep(500);
}
fclose($out);
fclose($fp);
$finished = true;
where $filePath is defined as 'D:\PST'. Is there any massive errors I am missing? D:\PST has full control permissions for the user group 'Everyone'. I am running the powershell window that I prompt PHP from in Administrator. I am using an Administrator account.
Thanks,

Please follow link http://forums.iis.net/t/1167645.aspx
And check with this link http://www.addictivetips.com/windows-tips/windows-7-access-denied-permission-ownership/

Related

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

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

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

fopen won't open file on Ubuntu server

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.

php cli reading files and how to fix it?

While using PHP cli in ubuntu i got apache as web server i cant read the file which i can while using it in the browser.
And about the error it just shows nothing and the next like i just used this code for my testing purposes :
<?
$handle = fopen("testFile.txt", "r");
if ($handle) {
while (($buffer = fgets($handle, 4096)) !== false) {
echo $buffer;
}
if (!feof($handle)) {
echo "Error: unexpected fgets() fail\n";
}
if(!is_readable($handle)
{
die("Not readable");
}
fclose($handle);
}
?>
How do i fix that ?
EDIT :
After removing the '#' before fopen i got the following error
fopen(testFile.txt): failed to open
stream: No such file or directory in
/var/www/log/ka.php on line 2
When you run your script through CLI, it uses your shell's working directory, in opposition to the file's directory (which Apache hands as the current directory). This is important for you, since your fopen call depends on a relative path; and relative paths are resolved relatively to the working directory, not to the script.
To have your script behave like it does with Apache, you either need to cd /var/www/log prior to running the php command, or add this at the beginning of your PHP script:
chdir(dirname(__FILE__));

Categories