Tuesday, May 7, 2024
 Popular · Latest · Hot · Upcoming
1
rated 0 times [  1] [ 0]  / answers: 1 / hits: 7789  / 3 Years ago, mon, july 12, 2021, 9:31:47

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 = "
".'New data line 2';
fwrite($handle, $new_data);

More From » 14.04

 Answers
7

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.


[#24528] Wednesday, July 14, 2021, 3 Years  [reply] [flag answer]
Only authorized users can answer the question. Please sign in first, or register a free account.
freezidepu

Total Points: 394
Total Questions: 105
Total Answers: 118

Location: Libya
Member since Mon, Dec 7, 2020
3 Years ago
freezidepu questions
Tue, Apr 19, 22, 16:03, 2 Years ago
Fri, Nov 11, 22, 18:04, 2 Years ago
Sat, Mar 18, 23, 10:59, 1 Year ago
;