php mkdir() chmod and permissions

Thomas picture Thomas · Sep 22, 2010 · Viewed 39.4k times · Source

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

Answer

Knowledge Craving picture Knowledge Craving · Sep 22, 2010

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.