1

I could create file with php fopen(..) when I used the ubuntu 12.04. But now I can't create file with php, because I pass ubuntu 14.04. I can open existing file to read or to write. But I can't create new file because of permission denied.

fopen(../../../../../Desktop/file): failed to open stream: Permission denied in /var/www/html/createFolder.php on line 17
Cannot open file: ../../../../..//Desktop/file" error is occured.


$my_file = ".." . DIRECTORY_SEPARATOR . ".." . DIRECTORY_SEPARATOR . ".." . DIRECTORY_SEPARATOR . ".." . DIRECTORY_SEPARATOR . ".." . DIRECTORY_SEPARATOR . "home" . DIRECTORY_SEPARATOR . "username" . DIRECTORY_SEPARATOR . "Desktop" . DIRECTORY_SEPARATOR . "file";
$handle = fopen($my_file, 'w') or die('Cannot open file:  '.$my_file);
$data = 'New data line 1';
fwrite($handle, $data);
$new_data = "\n".'New data line 2';
fwrite($handle, $new_data);
  • which daemon you use http server of? – xiaodongjie Jun 20 '14 at 13:32
  • I use apache 2.2.22 as server. – Mert Özoğul Jun 20 '14 at 13:39
  • Do you get the same error if you give the absolute path of the file you want to read to fopen() ? What about the permissions on this file (Apache runs under the www-data user). – Benoit Jun 20 '14 at 14:04
  • Can you post the output of ps -eo cmd,user,group | grep [a]pache and ls -la in the folder where you php script should create the new file? – chaos Jun 20 '14 at 14:06
  • I don't want to read a file. I want to create a new file with fopen in php. – Mert Özoğul Jun 20 '14 at 14:10
0

The error occurs because of a permissions issue.

You wish to create a file in your users Desktop directory.

Your script is not being run by your user it is being run by apahce's user, which is ussually www-data. This user most likely does not have permission to create files in your users home directory.

Therefore you need to allow the apache user write access to your Desktop director.

Note: This is a bad idea but you could do it a couple ways.

1.Add the apache user to your user group.

2.Change the permission of your desktop directory to allow anyone to write to it (probably the worst idea).

3.Add yourself to www-data group and change the group owner of your desktop folder to www-data.

Be careful with any of these techniques as they open up your home directory to others.

Unrelated: Why use so many ..? Just use and absolute path.

  • I can't create a file in both Desktop and www directory. How to create a new file in www directory with apache's user (mine is www-data).What can I do to create a new file in www directory with www-data user? – Mert Özoğul Jun 20 '14 at 18:22
  • sudo adduser <username> www-data<--> sudo chown -R www-data:www-data /var/www<--> sudo chmod -R g+rw /var/www The problem was solved this commands. No longer www-data apache'user can create a file in www directory. But I am worried that for security. Does it occur security problems????? – Mert Özoğul Jun 21 '14 at 6:22
0

You need a permission to create the file in ../../../../../Desktop.

Because the php code is exucuted with apache user privaliage.

If you want it, You must check and change with following commands.

ls -l ../../../../../Desktop

sudo chmod 777 ../../../../../Desktop

or

sudo chown xxx ../../../../../Desktop

the xxx is apache user name.

  • There are no files. I want to create a new file with createFolder.php which is under var/www/html/. – Mert Özoğul Jun 20 '14 at 14:25
  • There is /Desktop directory? – xiaodongjie Jun 20 '14 at 14:28
  • Yes it is directory which shows file locate. – Mert Özoğul Jun 20 '14 at 14:59

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.