failed to open stream, permission denied even tho permissions are set - php

I'm working with the bing-ads api and try to download a report to a local folder.
I already found out the user that is executing php using
<?php echo exec('whoami'); ?>
which resulted in "dl-dominikl-pc\dolo"
I gave that user full control of the "reports" folder.
The code-part that is failing is:
fopen($downloadPath, 'wb');
with $downloadPath being "C:\\xampp\\htdocs\\crm\\bingAds\\examples\\reports"

Thanks to Saitama for reminiding me to check my path which didn't point to a file!

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!

Absolute path (www.example.com) in file_put_contents [duplicate]

Check this code:
<?php
$url = 'http://www.example.com/downloads/count.txt';
$hit_count = #file_get_contents($url);
$hit_count++;
#file_put_contents($url, $hit_count);
header('Location: wmwc.zip');
?>
#file_get_contents is working fine and the header location change to the downloaded file also works, but either the hit_count increase or #file_put_contents isn't working, because the number with the file doesnt increase by 1. I've set the file permission to 777, but when I try to set the directory permission to 777 also I get a 500 internal server error saying "The server encountered an unexpected condition which prevented it from fulfilling the request."
You can't write a remote file via http.(If you could do that, every one else could change that file also.)
You need to use the local path.
try changing directory properties
chown www-data:www-data <dirname>
and/or write as follows, if you host on linux
<?php
$var ="hi";
shell_exec('echo "'.$var.'">>log.txt');
?>

PHP Write a variable to a txt file

I am trying to get a variable from the query string and write it to a text file.
I have tried like this:
<?php
$content=( $_GET['var'] );
echo $content;
$file = fopen("etlLOG.txt","w+");
echo fwrite($file,$content);
fclose($file);
?>
I get the following errors:
Warning: fopen(etlLOG.txt) [function.fopen]: failed to open stream: Permission denied in E:\Users\george\listener.php on line 8
Warning: fwrite(): supplied argument is not a valid stream resource in E:\Users\george\listener.php on line 9
Warning: fclose(): supplied argument is not a valid stream resource in E:\Users\george\listener.php on line 10
You may need to change the permissions as an administrator. Open up terminal on your Mac and then open the directory that etlLOG.txt is located in. Then type:
sudo chmod 777 etlLOG.txt
You may be prompted for a password. Also, it could be the directories that don't allow full access.
OR
PHP
chmod : Attempts to change the mode of the specified file to that given in mode.
chmod
Well it appears that the web server does not have access to that file. So used ftp fopen provided the correct credentials and paths and finally i did managed to access and edit the file
If you are running WAMP on windows
1) login as an administrator
2) Install wamp
3) Download subinacl.exe from microsoft:
4) grant permissions to the regular user account to manage the WAMP services: (subinacl.exe is in C:\Program Files (x86)\Windows Resource Kits\Tools)
subinacl /SERVICE \MachineName\wampapache /GRANT=domainname.com\username=F
subinacl /SERVICE \MachineName\wampmysql /GRANT=domainname.com\username=F
5) logout and log back in as the user. They should now be able to launch the WAMP taskbar application and control the WAMP service.
My guess is that PHP is trying to create the file at 'C:\WINDOWS', so the solution wouldn't be to set permissions there.
I would suggest you use a specific location, example: create a folder at the same location as your php script and use it to create the files:
$filePath = dirname(_FILE_) . "/temp_vars/etILOG.txt";
$file = fopen($filePath ,"w+");
Kind regards,
The code you are running is trying to create the etlLOG.txt in the same folder as your PHP script. You need to give Full Permission to your IUSER_ account on the E:\Users\george folder.
If you do not know how you can browse the following links
http://technet.microsoft.com/en-us/library/bb727008.aspx
http://www.wikihow.com/Change-File-Permissions-on-Windows-7
How to set 777 permission on a particular folder?

PHP File_put_contents not working

Check this code:
<?php
$url = 'http://www.example.com/downloads/count.txt';
$hit_count = #file_get_contents($url);
$hit_count++;
#file_put_contents($url, $hit_count);
header('Location: wmwc.zip');
?>
#file_get_contents is working fine and the header location change to the downloaded file also works, but either the hit_count increase or #file_put_contents isn't working, because the number with the file doesnt increase by 1. I've set the file permission to 777, but when I try to set the directory permission to 777 also I get a 500 internal server error saying "The server encountered an unexpected condition which prevented it from fulfilling the request."
You can't write a remote file via http.(If you could do that, every one else could change that file also.)
You need to use the local path.
try changing directory properties
chown www-data:www-data <dirname>
and/or write as follows, if you host on linux
<?php
$var ="hi";
shell_exec('echo "'.$var.'">>log.txt');
?>

rename a file in php on a windows file server (unc path)

In PHP I want to rename (move/copy) a file on a windows file server:
"\myserver\folder1\folder2\myfile.pdf"
to
"\myserver\folder1\folder2\OLD\myfile.pdf"
(all folders already exist and destination file does not exist)
I tried this:
copy("\\\\myserver\\folder1\\folder2\\myfile.pdf", "\\\\myserver\\folder1\\folder2\\OLD\\myfile.pdf");
and
copy("//myserver/folder1/folder2/myfile.pdf", "//myserver/folder1/folder2/OLD/myfile.pdf");
I receive:
[function.copy]: failed to open stream: Permission denied
The computer I am on / user logged in as has permissions to rename/move/delete/copy to that share/folder.
I am guessing I need to somehow give php permissions, or run php as my user? OR?
PHP will be running as whatever user your web server runs as. You would need to grant permissions on that folder to whatever user account that is.
Dont use Copy... use move_uploaded instead
This is one example getting the image from a form:
$img = 'sample.jpg;
$path = '//nameofyourpcinyournetwork/sharedfolder/folderyoulike/';
$pathwithimg = $path.$img;
if (!is_dir($path)) {
mkdir($path, 0644, TRUE); // TRUE for make it recursive
}
if (file_exists($pathwithimg)) {
unlink($pathwithimg);
move_uploaded_file($_FILES["file"]["tmp_name"], $pathwithimg);
chmod($pathwithimg, 0644);
}
Change safe_mod to Off if you have it On
P.D. Yeah i know, this post is 5 years ago... but no one said a valid answer and other people (like me) may find this question

Categories