wantTheBest
wantTheBest

Reputation: 1720

localhost permissions with php mkdir()?

In preparation for my move to a production server later, I'm using this code (php) to make a new directory that will store a user's files:

function createNewUserFolder($newUserName)
{
    $siteRoot = "http://" . $_SERVER['HTTP_HOST'] . "/myWebsite/";
    $newUserSubfolder = $siteRoot . $newUserName;

    if (!mkdir($newUserSubfolder, 0755))
    {
        echo "PROBLEM...";
    }
}

I've tried 0644 permissions to no avail -- I only get 'PROBLEM...' when this executes. I check the string containing the full path and it's correct - "http://localhost/myWebsite/myNewUserSubfolder".

So I'm suspecting the "http://localhost/myWebsite/" part of the above string is the problem. Yet I don't see why -- after all, this is php code running on my web server, so why can't my php code create a folder on my web server?

Upvotes: 1

Views: 3434

Answers (2)

Vyktor
Vyktor

Reputation: 21007

You are trying to create directory via http protocol on "remote server", just try doing in linux shell:

 mkdir "http://google.com/mydir"

Http server has no way of knowing that it's you and AFAIK it doesn't even support creating directories directly (via http protocol without script).

Anyway mkdir works only for the file:// protocol (or wrapper if you want) which is implicit in all file system functions.

Therefor when you run mkdir() with parameter /my/path is evaluated as file:///my/path (which it handles correctly). For http://... it's just unsupported protocol.

Upvotes: 2

Jaspreet Chahal
Jaspreet Chahal

Reputation: 2750

for windows environments you may want to do something like this

$siteRoot = "c:\\path\\to\\siteroot\\myWebsite/";
$newUserSubfolder = $siteRoot . $newUserName;

if (!mkdir($newUserSubfolder, 0755))
{
    echo "PROBLEM...";
}

Try that

Or try using FTP if there is FTP support on your server read more here http://php.net/manual/en/book.ftp.php

Upvotes: 5

Related Questions