Search code examples
phpfile-uploadpathdirmagic-constants

Can I change current directory to parent directory using PHP __DIR__ constant?


I am using PHP magic constant to move uploaded file to a new location, and the upload handler file location is inside "_inc" file inside the main application folder like this:

"path/tasksapp/_inc/upload_handelr.php"

So; when I use __DIR__ I get the path where the upload hander is exist:

"path/tasksapp/_inc/"

While the new location for uploaded file should be:

"path/tasksapp/uploads"

Is there's any option or attribute I can use with __DIR__ constant so I can change current directory to parent directory:

change:

"path/tasksapp/_inc/"

to be:

"path/tasksapp/"


Solution

  • I recommend dirname(__DIR__):

    • it's portable (works on Windows where a/b/../c is not allowed)
    • it's readable