user1769686
user1769686

Reputation: 535

php file_put_contents can't create files on apache server directory

I have installed apache server on a linux fedora machine and I put the following test.php and test.html on var/www/html but when I open the 127.0.0.1/test.html on firefox the test.php doesn't create the text.txt file, let alone write the string to the file and the there is also no output for "echo $var"

the error is

Warning: file_put_contents(test.txt): failed to open stream: Permission denied in /var/www/html/getdata.php on line 7

the permission for the directory is:

drwxr-xr-x. 2 root root 4096 Nov  6 14:14 html

test.php:

<?php
$v="x";
$fname='test.txt';
$rv=file_put_contents($fname,$v);
echo $rv;
echo $v;
?>

the test.html is so complex coz I planned to write something complex to a file on the server, but since there is some problem, I simplified the test.php

test.html:

<!DOCTYPE html>
<html>
<body>

<form id="yourFormID" method="POST" action="/getdata.php" ></form>

<script>
  function sendArray( theArray )
  {
    var frm = document.getElementById('yourFormID');
    fld = document.createElement("INPUT");
    fld.name ="data"; 
    fld.type = "hidden";
    fld.value = JSON.stringify(theArray);
    frm.appendChild(fld);  
    frm.submit();
   }

   var yourArray = [0.000023323,0.00001292,0.00003323];

    sendArray( yourArray );

    </script>
    </body>
    </html>

Upvotes: 2

Views: 5667

Answers (2)

Darren Cook
Darren Cook

Reputation: 28928

The html directory is currently owned by root, but under Fedora the web server runs as the "apache" user. (see "Apache File Security" section of https://fedoraproject.org/wiki/Administration_Guide_Draft/Apache?rd=Docs/Drafts/AGBeta/Apache )

So, as root, do:

 chown -R apache:apache /var/www/html/
 chmod -R 770 /var/www/html

The first makes the web server own the directory. The second makes sure that only users in the "apache" group can read/write files. It also says that no other users on the machine can even read them.

If you ever need another user to be able to write files into your web tree, add them to the "apache" group.

Upvotes: 2

honyovk
honyovk

Reputation: 2747

This is a permission problem with Linux. Try:

chmod 777 path/to/test.txt

at the command line.

EDIT: Here is a great article on Linux file permissions. http://www.tuxfiles.org/linuxhelp/filepermissions.html

EDIT 2: I might add, setting the appropriate permissions for a file is the only way PHP can manipulate said file with file_put_contents, fwrite, etc.

Upvotes: 2

Related Questions