How to Use _Dir_

How to use __dir__?

You can use __DIR__ to get your current script's directory. It has been in PHP only since version 5.3, and it's the same as using dirname(__FILE__). In most cases it is used to include another file from an included file.

Consider having two files in a directory called inc, which is a subfolder of our project's directory, where the index.php file lies.

project
├── inc
│   ├── file1.php
│   └── file2.php
└── index.php

If we do include "inc/file1.php"; from index.php it will work. However, from file1.php to include file2.php we must do an include relative to index.php and not from file1.php (so, include "inc/file2.php";). __DIR__ fixes this, so from file1.php we can do this:

<?php
include __DIR__ . "/file2.php";

To answer your question: to include the file warlock.php that is in your included file's upper directory, this is the best solution:

<?php
include __DIR__ . "/../warlock.php";

Why include __DIR__ in the require_once?

PHP scripts run relative to the current path (result of getcwd()), not to the path of their own file. Using __DIR__ forces the include to happen relative to their own path.

To demonstrate, create the following files (and directories):

- file1.php
- dir/
- file2.php
- file3.php

If file2.php includes file3.php like this:

include `file3.php`.

It will work fine if you call file2.php directly. However, if file1.php includes file2.php, the current directory (getcwd()), will be wrong for file2.php, so file3.php cannot be included.

PHP confused about use of __DIR__ and ../ in a path

<?php
require __DIR__ . "/some_file.php";
/* __DIR__ SIMPLY POINTS TO THE DIRECTORY IN WHICH THE ACTIVE SCRIPT LIVES
- THE DIRECTORY OF THE EXECUTING SCRIPT... THEN REQUIRES THE FILE
"some_file.php" WITHIN THAT DIRECTORY.
THE REAL-PATH SHOULD REFLECT SOMETHING LIKE: projectRoot/public/some_file.php
*/

require __DIR__ . "/../src/bootstrap.php";
/* FOLLOWING THE LOGIC ABOVE, __DIR__ . "/../" __DIR__ AGAIN POINTS TO
THE DIRECTORY IN WHICH THE ACTIVE SCRIPT LIVES WHILE "/../" TELLS
THE REQUIRE DIRECTIVE TO LOOK ONE DIRECTORY ABOVE THE CURRENT DIRECTORY
(AND LOCATE WITHIN THAT DIRECTORY ANOTHER DIRECTORY CALLED "src" IN THIS CASE...)
THEN WITHIN THAT "scr" DIRECTORY LOOK FOR A FILE CALLED:
"bootstrap.php" AND REQUIRE/INCLUDE IT...
THE REAL-PATH SHOULD REFLECT SOMETHING LIKE: projectRoot/src/bootstrap.php
*/

// TO UNDERSTAND THESE CONCEPTS MORE CLEARLY, IT WOULD BE ADVISED TO
// TRY SOMETHING LIKE THESE:
var_dump( realpath(__DIR__ . "/../src/bootstrap.php") );
var_dump( realpath(__DIR__ . "/index.php") );

PHP how to go one level up on dirname(__FILE__)

For PHP < 5.3 use:

$upOne = realpath(dirname(__FILE__) . '/..');

In PHP 5.3 to 5.6 use:

$upOne = realpath(__DIR__ . '/..');

In PHP >= 7.0 use:

$upOne = dirname(__DIR__, 1);

Is there any difference between __DIR__ and dirname(__FILE__) in PHP?

Their result is exactly the same ; so, no difference on that.


For example, the two following lines :

var_dump(dirname(__FILE__));
var_dump(__DIR__);

Will both give the same output :

string '/home/squale/developpement/tests/temp' (length=37)


But, there are at least two differences :

  • __DIR__ only exists with PHP >= 5.3
    • which is why dirname(__FILE__) is more widely used
  • __DIR__ is evaluated at compile-time, while dirname(__FILE__) means a function-call and is evaluated at execution-time

    • so, __DIR__ is (or, should be) faster.


As, as a reference, see the Magic constants section of the manual (quoting) :

__DIR__ : The directory of the file.

If used inside an include, the
directory of the included file is
returned.
This is equivalent to
dirname(__FILE__).
This
directory name does not have a
trailing slash unless it is the root
directory.
(Added in PHP 5.3.0.)

Get folder up one level

You could do either:

dirname(__DIR__);

Or:

__DIR__ . '/..';

...but in a web server environment you will probably find that you are already working from current file's working directory, so you can probably just use:

'../'

...to reference the directory above. You can replace __DIR__ with dirname(__FILE__) before PHP 5.3.0.

You should also be aware what __DIR__ and __FILE__ refers to:

The full path and filename of the file. If used inside an include, the name of the included file is returned.

So it may not always point to where you want it to.

How to use directory as an arg for a function in R

Rather than counting characters yourself, let formatC do it for you.

pm <- function(directory, sex, id = 1:440){
filenames <- paste0(formatC(id, digits = 0, width = 3, format="f", flag = "0"), ".csv")
filepaths <- file.path(directory, filenames)
list_of_data_frames <- lapply(filepaths, read.csv)

# Do things with the data here, such as lapply(list_of_data_frames, your_function)

}

How to use / (directory separator) in both Linux and Windows in Python?

Use os.path.join().
Example: os.path.join(pathfile,"output","log.txt").

In your code that would be: rootTree.write(os.path.join(pathfile,"output","log.txt"))

Moving all files from one directory to another using Python

Try this:

import shutil
import os

source_dir = '/path/to/source_folder'
target_dir = '/path/to/dest_folder'

file_names = os.listdir(source_dir)

for file_name in file_names:
shutil.move(os.path.join(source_dir, file_name), target_dir)

How to set the current working directory?

Try os.chdir

os.chdir(path)

        Change the current working directory to path. Availability: Unix, Windows.



Related Topics



Leave a reply



Submit