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!
Related
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!
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);
}
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?
So, I have this simple little php script. It runs and compiles fine and works the way I want it to on the machine that I coded it. I'm running it on a personal home web-server running Debian 6.0.6, 32bit. It's apache with php. And I know for a fact that php is working on the server.
<?php
$hitsfile = "hits.txt"; #name of file
$filehandle = fopen($hitsfile, 'r') or die ("Couldn't read file."); #Opens file, 'hitsfile' to be read.
$hits = fread($filehandle, 5); #reads file to the introduced variable, 'hits'
fclose($filehandle); #closes file
$hits++; #increments the variable that it read.
$filehandle = fopen($hitsfile, 'w') or die ("Couldn't write to file."); #opens file to be read.
fwrite($filehandle, $hits); #writes the hits variable to file.
fclose($filehandle); #closes file.
echo $hits; #outputs the hits variable.
?>
When I access the file from the server, via a web browser, I get the "Couldn't write to file." error. So then, it's opening the file properly, and reading it. And when it opens it to write, it fails. I'm assuming this is some sort of problem with permissions or something. I'm sort of at a loss as to how to solve the issue. Any ideas? Assistance would be greatly appreciated! I've googled for a couple days now, and I can't solve the issue. I'm a php 'noob' and I'm very new to running a linux-based web-server, but hey, you gotta learn somehow. :*l
tried to check the permissions to the file? The Linux file system have a very strict permission system. Write on terminal:
ls -la /path/to/my/file.txt
This would give you your permissions on the left column. Please read this article to be sure, and check if Apache have the "write" permissions to the file. If not, use the chmod command to give Apache access to the file (or the chown command, to change the owner of this file to apache, if the owner of this file have writing permissions).
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