Ubuntu – Permission Denied When Create New File

14.04Apache2permissionsPHP

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);

Best Answer

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.

Related Question