Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

php mkdir() chmod and permissions

Tags:

php

mkdir

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

like image 375
Thomas Avatar asked Sep 21 '10 22:09

Thomas


1 Answers

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.

like image 59
quayph Avatar answered Oct 22 '22 08:10

quayph