2 Replies - 2129 Views - Last Post: 02 December 2012 - 05:25 PM

#1 goku87  Icon User is offline

  • D.I.C Head
  • member icon

Reputation: 4
  • View blog
  • Posts: 77
  • Joined: 17-October 09

Settings for apache relative/absolute ("./path" vs "/path&

Posted 02 December 2012 - 03:39 AM

I'd like to mimic my server's environment in my localhost, as I'd feel that would be easier/more secure than trying to duplicate my localhost environment on my server. The reason for this is due to the way paths are handled. It's weird, but let me try to explain.

The following works on localhost but not on the server (called from script in root folder):
mkdir("images/sub/folder") // works
mkdir("/images/sub/folder") // also works

It actually fails in two ways on the server.

First, to make a new folder in the root directory (despite having the script residing within it that is attempting to create the new folder) I must precede the new folder with ./ else it will give me a "file/directory does not exist" error.

Second, if I attempt to make multiple folders at once (e.g. both /sub and /folder in the same path from the previous example) it will again kick back the dir-not-exist error. I must create each new folder separately, e.g.
mkdir("./images/sub");
mkdir("./images/sub/folder");

I'm assuming the second issue is related to the recursive parameter of the mkdir function, although I've not had a new opportunity to try it on my server just yet. Regardless, where can I change these particular settings to mimic these errors so I can develop in an environment similar to my server?

Is This A Good Question/Topic? 0
  • +

Replies To: Settings for apache relative/absolute ("./path" vs "/path&

#2 Atli  Icon User is offline

  • D.I.C Lover
  • member icon

Reputation: 3717
  • View blog
  • Posts: 5,981
  • Joined: 08-June 10

Re: Settings for apache relative/absolute ("./path" vs "/path&

Posted 02 December 2012 - 05:00 AM

It's usually safest to use absolute paths, instead of relying on whatever CWD is currently being used.

In PHP, you can always use __DIR__ (PHP 5.3+) or dirname(__FILE__) (PHP 5.2 and lower) to get the directory of the current file. Which means you can do stuff like this:
$path = implode(DIRECTORY_SEPARATOR, array(__DIR__, "sub", "folder"));

if (!file_exists($path)) {
    if (mkdir($path, 0777, true)) {
        echo "Done!";
    }
    else {
        trigger_error("Failed to create directory!", E_USER_ERROR);
    }
}
else {
    echo "Folder already exists!";
}


This, if run in a script located in the root, would create a "/sub/folder" directory.
Was This Post Helpful? 0
  • +
  • -

#3 Dormilich  Icon User is offline

  • 痛覚残留
  • member icon

Reputation: 3524
  • View blog
  • Posts: 10,169
  • Joined: 08-June 10

Re: Settings for apache relative/absolute ("./path" vs "/path&

Posted 02 December 2012 - 05:25 PM

is your localhost server on Windows?
Was This Post Helpful? 0
  • +
  • -

Page 1 of 1