Iterate through directories recursively in PHP -
when upload photos web server try split photos several folders won't have many photos in 1 single folder.
for example inside class:
$filename = hash('crc32b', mt_rand()); $img_dir = upload_dir.ds.'img'.ds.$filename[0].$filename[1].ds.$filename[2].$filename[3].ds.$filename[4].$filename[5];
ds
short version of directory_separator
this create directories these:
public_html\assets\upload\img\25\55\8b public_html\assets\upload\img\00\8c\2a
if file not exist , there no directories already
$img_path = $img_dir.ds.$filename.'.jpg'; if (!file_exists($img_path) && !is_dir($img_dir)) { $mode = 0755; mkdir($img_dir, $mode, true); chmod(upload_dir, $mode); chmod(upload_dir.ds.'img', $mode); chmod(upload_dir.ds.'img'.ds.$filename[0].$filename[1], $mode); chmod(upload_dir.ds.'img'.ds.$filename[0].$filename[1].ds.$filename[2].$filename[3], $mode); chmod(upload_dir.ds.'img'.ds.$filename[0].$filename[1].ds.$filename[2].$filename[3].ds.$filename[4].$filename[5], $mode); }
i wan't folders 755, how can recursively?
edit:
also why 493
output when echo $mode;
or echo 0755;
?
if have access shell, , assuming os linux, i'd go with:
system('chmod -r ' . escapeshellarg(upload_dir));
as edit, 0755
octal in decimal 493
.
edit: can try function, beware $path
should full path single file, or path directory without trailing slash
function chmod_recursive($path, $mode) { if(is_dir($path)) { foreach(glob("$path/*") $file) { chmod_recursive($file, $mode); } } else if(is_file($path)) { chmod($path, $mode); } }
calling this:
chmod_recursive(upload_dir, 0755);
Comments
Post a Comment