`mkdir()`: Permission denied to create folders with PHP

I have this PHP script:

$dir = FCPATH . 'uploads' . DIRECTORY_SEPARATOR . 'posts'; // is in CodeIngiter
if (!is_dir($dir)) { mkdir($dir, 755);
}

Which creates a folder for storing posts' images. However I'm getting this error / warning:

Severity: Warning
Message: mkdir(): Permission denied

and I can't create the folder.

How can I fix this? My folder structure looks like this:

/opt/lampp/htdocs/www/my-site/public/ (uploads/posts) // all folders inside files will generate in php

Some further information: if I comment this code, manually create the posts folder and then try to upload a file I get an error saying that the destination path is not writable.

Output of ls -l /opt/lampp/htdocs/www/my-site/:

drwxrwxr-x 15 lykos lykos 4096 Sep 17 23:08 application
drwxrwxrwx 6 lykos lykos 4096 Sep 29 22:00 public
drwxrwxr-x 8 lykos lykos 4096 Sep 7 12:31 system

Output of ls -l /opt/lampp/htdocs/www/my-site/public:

drwxr-xr-x 4 lykos lykos 4096 Sep 18 21:48 css
drwxr-xr-x 2 lykos lykos 4096 Sep 27 19:18 img
-rwxrwxr-x 1 lykos lykos 9872 Sep 17 23:04 index.php
drwxr-xr-x 8 lykos lykos 4096 Sep 27 20:06 js
drwxr-xr-x 2 lykos lykos 4096 Sep 29 20:34 uploads
6

1 Answer

The problem is in your mkdir() call, not in Ubuntu.

As per your comment, ls -l /opt/lampp/htdocs/www/my-site/public/uploads doesn't exists, so you should call mkdir() passing the recursive parameter:

$dir = '/uploads/posts';
if (!is_dir($dir)) { mkdir($dir, 755, true);
}
1

Your Answer

Sign up or log in

Sign up using Google Sign up using Facebook Sign up using Email and Password

Post as a guest

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

You Might Also Like