Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

mkdir not working in PHP

Tags:

php

mkdir

Have been pulling out my hair for the past 2 hours on this and am sure I am doing something really stupid.

<?php
mkdir("blah", 0777);
?>

This works through the command line and the folder gets created. But the same thing doesn't work when I try to run it through the browser. Any file permission issues?

like image 238
Abhinav Avatar asked Feb 07 '10 03:02

Abhinav


People also ask

Why is mkdir permission denied?

[ErrorException] mkdir(): Permission denied. That means you do not have write permission on your project folder. Create a new folder, say 'myproject and run sudo chmod 777 myproject . Then move to 'myproject' folder and create project.

How create directory if not exists in PHP?

Methods: file_exists(): It is an inbuilt function that is used to check whether a file or directory exists or not. is_dir(): It is also used to check whether a file or directory exists or not. mkdir() : This function creates a directory.

What is mkdir in PHP?

The mkdir() creates a new directory with the specified pathname. The path and mode are sent as parameters to the mkdir() function and it returns TRUE on success or FALSE on failure. The mode parameter in mkdir() function is ignored on Windows platforms.

Why mkdir is not working in Ubuntu?

A directory in the path /usr/local/myfolder/ is missing, this is why you get the error. If you call mkdir -p , the missing path is created as well.


2 Answers

Could it possibly be that while running under the command line, the script inherits your permissions, but when running from the browser it doesn't?

In that case you would want to make your directory permissions 'write' for group.

like image 140
Tyler Carter Avatar answered Sep 27 '22 23:09

Tyler Carter


Your web server (apache?) is running as it's own user, and doesn't have permission to write to the directory you're running mkdir in.

Give your web server's user permission to write to the directory by either A) making it Owner, B) adding it to the Group if the Group has write permission, or C) give Everyone write permission (not recommended for most setups).

like image 34
Dolph Avatar answered Sep 27 '22 21:09

Dolph