Php mkdir () chmod and permissions

i used this basic script:

$folderPath = "../path/to/$folder/"; mkdir("$folderPath"); 

I create this directory and then upload the photos to it. I have been doing this for 4-5 months and suddenly I start getting "FORBIDDEN" errors when I try to view the contents of a folder through a web browser

The catalog is created in the same way, and the photos still load without problems, but I can’t access the photos

I tried rewriting the script and using chmod to change permissions, but I had no luck

All old folders were created using: -w-rwx rx rx

and i can't get this recreated

I tried adding the chmod line to my script:

 $folderPath = "../sales/inventory/$folder/"; mkdir("$folderPath"); chmod("$folderPath", 0755); 

but I can’t recreate the same permissions, I’m trying to understand how chmod works, but I can’t figure out how to return this main function again.

+6
php mkdir
source share
3 answers

Try to find the HTAccess file, where the "-Indexes Options" parameter will be indicated, since this is mainly used to not show the contents of the folder in a web browser. The file must be searched as follows: -

  • In the folder " root_folder/sales/inventory/$folder/ ", where " $folder " is listed in your code.
  • If not found, try in the folder "root_folder / sales / inventory /".
  • If not found, try in the folder "root_folder / sales /".
  • If not found, try in the "root_folder /" folder.

When you get the “Parameters -Indexes” code recorded in the HTAccess file, you can delete / comment on this line of code, or you can also write another HTAccess file in the required folder “ $folder ”, where the code will be “Option Indexes”.

Also on the PHP page, the logic should be like this: -

 <?php $folderPath = "../sales/inventory/$folder/"; mkdir("$folderPath"); chmod("$folderPath", 0755); // Use of "copy()" / "move_uploaded_file()" function here, using some "$targetFile" variable. chmod($targetFile, 0755); ?> 

This will help you when you disconnect / delete downloaded files from the "$ folder" folder.

Hope this helps.

+9
source share

If your $ folder variable contains some subdirectories, your parent directories may not be encoded for the correct permissions. This was a problem that I encountered on a leased OVH Gentoo server.

Imagine $folder = '/store1/ally23/shelf42'; , so your final directory structure is ../sales/inventory/store1/ally23/shelf42 , and you want 0777 permissions. You do:

 mkdir($folderPath, 0777, true) || chmod($folderPath, 0777); 

Only the destination directory of shelf42 changed to 0777 . Intermediate directories are created with default permissions (in my case 0744 ).

There is no recursive option in PHP chmod, so you need to iterate over the intermediate directories and chmod them individually.

+6
source share

If you are in a shared environment, you can also chown after downloading to be on the safe side. Especially if you use your web server as a user other than your virtual host, it has permission to access (EG: "nobody" vs "mysite".) This is often found with cPanel, FWIW servers.

+2
source share

All Articles