2

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
  • 1
    The directory /opt/lampp/htdocs/www/my-site/public/ needs to be owned by the same user running php – Panther Sep 29 '15 at 21:19
  • Does the folder exists already? If it doesn't, please add the output of ls -l /opt/lampp/htdocs/www/my-site/public/uploads – kos Sep 29 '15 at 21:20
  • @bodhi.zazen How can I check the user running php? – ltdev Sep 29 '15 at 21:22
  • with most web servers it is www-data on Ubuntu. Who owns /opt/lampp/htdocs/www/my-site/public/uploads ? Note it is a bit of a security risk to allow php rw access. – Panther Sep 29 '15 at 21:23
  • @kos I run this command on command line and get this total 0 – ltdev Sep 29 '15 at 21:24
3

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);
}
| improve this answer | |
  • Comments are not for extended discussion; this conversation has been moved to chat. – Mitch Oct 3 '15 at 17:18

Your Answer

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

Not the answer you're looking for? Browse other questions tagged or ask your own question.