I’ve always used the following codes to create a folder on a Apache server but recently on one of the server I’ve got permission denied error.
if(!is_dir('img/user/'.$id))
{
mkdir('img/user/'.$id, 0777, true);
chmod('img/user/'.$id, 0777);
}
On internet I found that to upload directory for httpd it needs to have write permissions like this:
drwxrwxrwx 2 user staff 512 Jan 07 12:32 uploads/
Where is this permission set? I do not direct access to the server. Is there any alternatively?
You never want to set permissions to be world writable if you can avoid it, or even readable for that matter.
0770would be a better option, if still a little broad. The main point is that the folder(s) in question need to be writable by the webserver user. For instance, on many webhosts apache will be run by the usernobody, so a more appropriate permission would look like this:Now, there is a problem if you can’t get direct access to set permissions yourself except through PHP, because some web hosts will disallow your ability to run
chmodor other permissions or ownership modifications from within PHP. That said, if you’re using cPanel (and likely other hosting systems do this as well) you can use the online file manager to accomplish what you want by browsing to the appropriate directory and usingchange permissionslocated at the top of the page.So, ultimately, here’s what you need: If you need to be able to create a directory in a particular place, you need to make sure that place is writable by the web server. In your example, that means that you need to set appropriate permissions on
img/userfirst before you attempt to createimg/user/$id. That means thatimg/usereither must have permissions of0770and must be owned bynobody:nobody(either user or group would work in this context, you don’t need both), or it must have permissions of0777. Then, when you create your specific user directory, you can do it like so:… because it will already be owned by the appropriate user and you’ll already have write access to it simply because your webserver created it in the first place.
If you can’t find a non-PHP way to do it, then you’ll have to get your webhost to help.