chmod($assets_folder, '0777');  
mkdir ("$assets_folder/$new_folder", 0777); chmod($assets_folder, '0755');

Heres what I am trying to do: The default permission of the $assets_folder (with path) is 0755. This is so that noone can execute any malicious script from outside of my server. Whenever I want to create a new directory inside $assets_folder, I will just change the permission to 0777, make the directory ($new_folder) and then change the permission back to 0755

Two problems:

1) for the chmod, it gives me an error "chmod(): Operation not permitted in /path/index.php on line xx"

2) If I just take out the chmod parts, and chmod $assets_folder to 0777 and just use

mkdir ("$assets_folder/$new_folder", 0777);

, it does create a new folder, but the permission of that new folder is seen as 0755. Worse yet, I can not upload/move anything in that folder. Not even via FTP. So the new folder becomes virtually useless!

Does anyone have any idea how to fix those problems? Thanks in advance 🙂

    Have you checked to see if there is anything preventing it from being done in php.ini? Have you tried using your full file path?

      Originally posted by Kudose
      Have you checked to see if there is anything preventing it from being done in php.ini? Have you tried using your full file path?

      I am using the path /home/goreal/public_html/assets ....and what should I look for in php.ini?

        A couple of quick things...

        First, try removing the quotes:

        chmod($assets_folder, 0777);
        

        If that doesn't work, then I will refer you to something interesting annotated in the PHP Manual for the [man]chmod()[/man] function... (It was a user contributed note in the downloadable CHM version of the PHP Manual)

        To let users manipulate their file rights (via PHP fs interface) you can call a shell_exec()/system() however,
        it doesn't get any more ugly then that.
        The problem, chmod only want's (int) as mode type.
        However PHP converts: 0755 to it's octal value (493)
        and if you force to stay int, the value becomes: 755.
        This fails:

        <?
           chmod("fubar.txt", "0". 777);
           chmod("fubar.txt", "0777");
           chmod("fubar.txt", 777);
        ?>
        

        The work-around:

        <?
        $chmod = "0777";    # File mode
        $file = "fubar.txt";    # file
        // Create test file (if it doesn't exists)
           touch($file);
        // setting chmod
           eval("chmod(\"". $file ."\", ". $chmod .");");    
        // Showing current permissions echo "permissions: ". sprintf("%o", fileperms($file)); // Output: permissions: 100777 (which is: (7)rwx (7)rwx (7)rwx) ?>

        I don't know if this is the fix for your issue, but I found it to be pretty interesting... You never know.

          Well, I tried the eval method:

          			$file = $assets_folder."/screenshots";
          			$chmod = "0777";		
          			eval("chmod(\"". $file ."\", ". $chmod .");");
                      mkdir ($assets_folder."/screenshots/".$new_folder, 0777);
          			$chmod = "0755";		
          			eval("chmod(\"". $file ."\", ". $chmod .");");
          

          And I get this error:

          Warning: chmod(): Operation not permitted in /full_path/index.php(73) : eval()'d code on line 1

          Warning: mkdir(/full_path/screenshots/70): Permission denied in /full_path/index.php on line 74

          Warning: chmod(): Operation not permitted in /full_path/index.php(77) : eval()'d code on line 1

          n.b. line 1 doesnt have the eval code, line 73 and 77 does.

            Write a Reply...