PHP Write a variable to a txt file

青春壹個敷衍的年華 提交于 2019-12-11 20:11:20

问题


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


回答1:


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




回答2:


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




回答3:


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.




回答4:


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,




回答5:


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?



来源:https://stackoverflow.com/questions/19046684/php-write-a-variable-to-a-txt-file

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!