php mkdir() chmod and permissions

41,938

Solution 1

Try looking out for a HTAccess file, where the "Options -Indexes" option will be mentioned, as this is mostly used for not showing the contents of a folder in a web browser. The file needs to be searched in the following manner:-

  • In the folder "root_folder/sales/inventory/$folder/", where "$folder" is as mentioned 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 folder "root_folder/".

When you get the code of "Options -Indexes" written in the HTAccess file, you can remove / comment that line of code from there, or you can also write another HTAccess file in your required folder of "$folder", where the code will be "Options Indexes".

Also in the PHP page, the logic must 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 will be unlinking / deleting the uploaded files from the "$folder" folder.

Hope it helps.

Solution 2

If your $folder variable includes some sub-directories your parent directories are maybe not being chmoded to the right permissions. This was the problem I was having on a hired OVH Gentoo server.

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

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

Only the final directory shelf42 is chmoded to 0777. The intermediary directories are created with default permissions (in my case 0744).

There is no recursive option in PHP's chmod command, so you have to loop over the intermediary directories and chmod them individually.

Solution 3

If you're in a shared environment, you may also want to chown after upload, to be on the safe side. Especially if you're running your web server under a user other than your virtual host has permission to access (EG: "nobody" vs "mysite".) This is common with cPanel servers, FWIW.

Share:
41,938
Thomas
Author by

Thomas

Updated on July 14, 2022

Comments

  • Thomas
    Thomas almost 2 years

    i was using this basic script:

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

    i create this directory and then upload photos to it. I've been doing this for a good 4-5 months now and suddenly i start getting 'FORBIDDEN' errors when I attempt to view the contents of the folder via web browser

    The directory is being created the same and the photos are still uploading without a problem, but I cannot access the photos

    I tried rewriting the script and using chmod to change the permissions but I'm having no luck at all

    All the older folders were being created with: -w- rwx r-x r-x

    and I can't get this recreated

    I've tried adding a chmod line into 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 get this very basic function working properly again

  • Thomas
    Thomas almost 14 years
    thanks for the help, this does help but I'm still having a problem with the actual permissions reflecting what I plug into the script, for example your example works fine except that when I look at the permissions from the directory tree, the created $folder isn't rwx r-x r-x as should be expected, but instead comes out as rwx r-x ---
  • Thomas
    Thomas almost 14 years
    on second thought i Just reread what you said and I must have missed the chmod on the upload files part at the end, I tried adding that to the upload script and it works fine. I really appreciate the help everything seems to be working alright, for now I just can't figure out why this broke in the first place
  • Knowledge Craving
    Knowledge Craving almost 14 years
    @thomas - It's a common part, which I also did this wrong thing, when I myself was a fresher. So nothing to worry about & cheer up! Best of Luck!!!