Trying to create a new folder

New coder here trying to figure out how to use PHP to create a folder.

From reading the PHP manual the following function is use to create either a folder or a file.

            $filename = '/images/minis/newfolder';
            mkdir($filename, 0777, true);

This is not making a new folder though. The code for doing this is one level up from the root (i.e. root/controls/foldercreator.php).

I am trying to create this folder here: root/images/minis/

The folders “images” and “minis” already exist.

Any idea why this is not working for me?

You probably want to use

$filename = '/root/images/minis/newfolder';

If you are wanting to create a /root/images/minis/newfolder folder.

Or if you are wanting to do this relatively.

$filename = '../images/minis/newfolder';

/images/minis/newfolder is referencing an image folder under the server’s root (top-level) folder.

Don’t confuse root top-level (i.e. /) and root’s home directory (i.e. /root - at least that’s the common directory)

1 Like

Try adding these lines to the top of the page and errors and warnings should be shown. Copying, pasting and searching the displayed errors will no doubt supply numerous solutions.


<?php 
// Remove when onlinedeclare (strict_types=1);
error_reporting(-1); // maximum errors 
ini_set('display_errors', 'true'); // displayy on screen 


 $filename = '/images/minis/newfolder';
 $success = mkdir($filename, 0777, true);
if($success && file_exists($filename)) {
  echo 'no problem';
}

// Errors and warnings will appear here

Here is the error message I receive when I run this:

Warning: mkdir(): Permission denied in /home/ubuntu/workspace/minisgallery v2/Live Files/controls/filename-create-test.php on line 8 Call Stack: 0.0006 234656 1. {main}() /home/ubuntu/workspace/minisgallery v2/Live Files/controls/filename-create-test.php:0 0.0006 235192 2. mkdir() /home/ubuntu/workspace/minisgallery v2/Live Files/controls/filename-create-test.php:8

line 8 is:
$success = mkdir($filename, 0777, true);

I guess it might be a setting on my IDE that is prohibiting this?

Forget guessing. as mentioned in my post:
"Copying, pasting and searching the displayed errors will no doubt supply numerous solutions".

The search query:

Search ubuntu Permission denied

Specifically: The PHP user that the command is being run under doesn’t have write access to /.
Keep in mind that mkdir is a filepath manipulating command. If your web document root is in /var/www/html/mysite, then "/images" does NOT refer to /var/www/html/mysite/images in this context.

1 Like

Indeed.

If you are running the script as a normal user (non root) then you probably won’t have write access to /root