Home » Php » PHP code mkdir('images','0777') creates a folder with 411 permissions! Why?

PHP code mkdir('images','0777') creates a folder with 411 permissions! Why?

Posted by: admin July 12, 2020 Leave a comment

Questions:

I could swear this was working yesterday. Now however the code below destroys the folder with no problem but creates a new folder with 411 permissions when it should be 777. My code was doing this yesterday.

The purpose of this is to zip up a folder, deliver it, delete the images, then create a new directory for the images.

Can someone tell me what I am doing wrong or what i should be doing? Thanks

function delete_directory($dirname) {
   if (is_dir($dirname))
      $dir_handle = opendir($dirname);
   if (!$dir_handle)
      return false;
   while($file = readdir($dir_handle)) {
      if ($file != "." && $file != "..") {
         if (!is_dir($dirname."/".$file))
            unlink($dirname."/".$file);
         else
            delete_directory($dirname.'/'.$file);     
      }
   }
   closedir($dir_handle);
   rmdir($dirname);
   return true;
}

$directoryToZip="jigsaw/"; // This will zip all the file(s) in this present working directory

$outputDir="/"; //Replace "/" with the name of the desired output directory.
$zipName="jigsaw.zip";

include_once("createzipfile/CreateZipFile.inc.php");
$createZipFile=new CreateZipFile;

/*
// Code to Zip a single file
$createZipFile->addDirectory($outputDir);
$fileContents=file_get_contents($fileToZip);
$createZipFile->addFile($fileContents, $outputDir.$fileToZip);
*/

//Code toZip a directory and all its files/subdirectories
$createZipFile->zipDirectory($directoryToZip,$outputDir);

/*
$rand=md5(microtime().rand(0,999999));
$zipName=$rand."_".$zipName;
*/
$fd=fopen($zipName, "wb");
$out=fwrite($fd,$createZipFile->getZippedfile());
fclose($fd);
$createZipFile->forceDownload($zipName);

@unlink($zipName);
delete_directory('jigsaw/assets/images/jigsaw_image');

mkdir('jigsaw/assets/images/jigsaw_image','0777');
How to&Answers:

Because you should be using the octal literal 0777, not the number-in-a-string "0777", which is actually 01411 in octal.

Answer:

I had kinda same problem, but even after removing quotes, the permission will not be set to 0777.

mkdir("infosheets/c/" , 0777);

but the created folder is set to 0755!

thats the solution:

$test="infosheets/c/";
mkdir($test);
chmod($test,0777);

you should first make the folder and than set it’s permission to 0777. they should be done separately for an unknown reason to me! strange!

Answer:

bool mkdir(string $pathname [, int $mode = 0777 [, bool $recursive = false [, resource $context]]])

The $mode param is an integer, not a string. 🙂

Here’s the example:

<?php
mkdir("/path/to/my/dir", 0700);
?>

You must use:

mkdir('jigsaw/assets/images/jigsaw_image', 0777);

Answer:

http://php.net/manual/en/function.mkdir.php

The second argument is supposed to be an int, not a string. Take out the quotes.

Answer:

so here is another solution if you like me try to move such things like settings out of the application into a configuration file [strings].

important to understand is that the permissions system is octal ! and uses a bit-flag system grouped in triplets 000 111 111 100 which equals the base 8 integer 0774.

if we skip the first 3 binary digits and think about the declarative representation “rwx” which is “write” “read” “execute” and you basically just switched all of them to “on” per user, group and only allowed reads for the “others”.

So if you think about it an byte has 8 bits and this a really efficient way to to store 255 (2^8)-1 different possibilities. Especially if you think about when unix was written.

If you translate it to the decimal (base 10) representation you just change the visual grouping and you fill up the missing zero bits based on the little or big endian system (https://en.wikipedia.org/wiki/Endianness)

As an 32 bit representation for little endian example
00000000 00000000 00000001 11111100
aka 508 which you actually can cast as integer and it will work. Even if you don’t cast it the type inference the interpreter does will work.

It’s likely they use small-int which only would use 16 bits instead of 32 🙂 but this is even more off topic…

more detailed information can be found here 🙂 or google
https://en.wikipedia.org/wiki/Filesystem_permissions

this is just meant as context information so understand what’s happening and how the following solution will work.

TL;DR

octdec("0774")

does the trick