So I’m trying to develop a script where all users can upload an image to a dynamically created folder.
Here is the line I made.
$newID = 1
mkdir('uploads/job_images/'.$newID.'/',0755);
This will create a folder inside “uploads/job_images” which is already set up.
My problem is that if “job images” permission is set to 755 or 766 it wont let me create a directory.
But when I try to set the “job_images” permission to 777 it allows me to create a directory. But they say 777 is a security risk. Why do you think I cant create a folder inside “job_images” if permission is set to 755 but allows me to create a folder inside “job_images” if “job_images” permission is set to 777?
The created folder also have the “owner/group” set to 99/99(nobody). Is that owner group okay?
Questions:
- Can I do it without using 777 to “job_images” folder or leaving it to 777 is okay?
- Do I need to change the owner of the created folder? I might someday need to delete those folders.
- How do I default the group of those created folders by the users of the website?
Try to use
sudochownto change the ownership of your directory to Apache (or whatever software/service/daemon running your code :). If you are the creator of the directory, you don’t need to use sudo (but in most Unix/Linux system you’ll need to have the target group permissions too).Please read about Numeric Notation of filesystem permissions. The first 7 indicating that the owner group have right to Read, Write and Execute your file. 5 means Read & Execute, without write, so you can’t make new directory.