Permission Denied When Create New File
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.
Related videos on Youtube
Mert Özoğul
Developed in the past in C, C++, C#, VB. Currently developing in C# , PL/SQL, ASP.NET MVC and Javascript.
Updated on September 18, 2022Comments
-
Mert Özoğul over 1 year
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);
-
xiaodongjie almost 10 yearswhich daemon you use http server of?
-
Mert Özoğul almost 10 yearsI use apache 2.2.22 as server.
-
Benoit almost 10 yearsDo 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).
-
chaos almost 10 yearsCan you post the output of
ps -eo cmd,user,group | grep [a]pache
andls -la
in the folder where you php script should create the new file? -
Mert Özoğul almost 10 yearsI don't want to read a file. I want to create a new file with fopen in php.
-
Mert Özoğul almost 10 yearsThis error is occurred when I want to create a new file.
-
xiaodongjie almost 10 yearsplease look my answer.
-
-
Mert Özoğul almost 10 yearsThere are no files. I want to create a new file with createFolder.php which is under var/www/html/.
-
xiaodongjie almost 10 yearsThere is /Desktop directory?
-
Mert Özoğul almost 10 yearsI 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 almost 10 yearssudo 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?????