Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations Mike Lewis on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

can not open a text file

Status
Not open for further replies.

bluesugar

Programmer
Mar 5, 2008
2
I am trying to write on a text file. I get this message. Not sure what am I doing wrong. I checked the permission on the file, it has all the checkbox checked for "allow". I am hosting the site on a web hosting site.

Warning: fopen(testFile.txt) [function.fopen]: failed to create stream: Permission denied in c:\websites\sntradersonline22\guidancellc.com\contactsend.php on line 9
can't open file

Code:
$opinion=$_POST['opinion'];


$myFile = "testFile.txt";
$fh = fopen($myFile, 'w') or die("can't open file");
$stringData = $opinion;
fwrite($fh, $stringData);
fclose($fh);
 
I like to use fopen as follow
$fh=fopen("$myFile", "r+") or die("can't open file");
are you sure that the script calling fopen is within the same dir and not located somewhere like ../../ etc...

if you need to write to a specific location
specify the location using fseek() before fwrite()



 
this issue is simple. despite your statements to the contrary, the relevant user does _not_ have permission to open the file in a write context.

assuming you are using IIS, you need to make sure that permissions to the file are given to IUSR_[MACHINENAME]

if you are using apache, then you need to make sure that the permissions are given to the relevant user running the process.

sometimes it is not straightforward to determine who the user is. you can query
Code:
echo get_current_user();
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top