freddyzcount90
freddyzcount90

Reputation: 69

Permission denied when trying to write file

I'm getting this error:

Warning: fopen(name.txt): failed to open stream: Permission denied in /Applications/XAMPP/xamppfiles/htdocs/phptests/post.php on line 5 Could not write file

I'm trying to retrieve form data and pass it into a text file like so:

<?php
   $name    = $_POST['name'];
   $surname = $_POST['surname'];

   $fh = fopen("name.txt", "w") or die("Could not write file");

   fwrite($fh, $name, $surname);
   fclose($fh);   
?>

I'm using Komodo Edit and my php files are saved in the "htdocs" folder provided by XAMPP.

Can anyone tell me why I'm not able to write the file?

Upvotes: 2

Views: 6774

Answers (2)

Doruk Ayar
Doruk Ayar

Reputation: 344

I think it's because of that you are writing fwrite wrong.

You are doing:

fwrite($fh, $name, $surname);

You should do:

fwrite($fh, $name. ', '. $surname);

Function fwrite() excepts 2 parameters. You are giving 3.

Upvotes: 1

Andy Cheung
Andy Cheung

Reputation: 61

As the error telling you the file is not writable.

To fix it , you can simply update the file permission

cd <directory of name.txt>
chmod 777 name.txt

Upvotes: 2

Related Questions