Viewed   116 times

I have searched high and low and get a lot of different solutions and variables containing info to get the absolute path. But they seem to work under some conditions and not under others. Is there one silver bullet way to get the absolute path of the executed script in PHP? For me, the script will run from the command line, but, a solution should function just as well if run within Apache etc.

Clarification: The initially executed script, not necessarily the file where the solution is coded.

 Answers

2

The correct solution is to use the get_included_files function:

list($scriptPath) = get_included_files();

This will give you the absolute path of the initial script even if:

  • This function is placed inside an included file
  • The current working directory is different from initial script's directory
  • The script is executed with the CLI, as a relative path

Here are two test scripts; the main script and an included file:

# C:UsersRedactedDesktopmain.php
include __DIR__ . DIRECTORY_SEPARATOR . 'include.php';
echoScriptPath();

# C:UsersRedactedDesktopinclude.php
function echoScriptPath() {
    list($scriptPath) = get_included_files();
    echo 'The script being executed is ' . $scriptPath;
}

And the result; notice the current directory:

C:>php C:UsersRedactedDesktopmain.php
The script being executed is C:UsersRedactedDesktopmain.php
Wednesday, October 19, 2022
2

If you give include() or require() (or the *_once versions) an absolute pathname, that file will be included. An absolute pathname starts with a "/" on unix, and with a drive letter and colon on Windows.

If you give a relative path (any other path), PHP will search the directories in the configuration value "include_path" in order, until a match is found or there are no more directories to search.

So, in short, to include an absolute filename, give an absolute filename. See also the function realpath().

If you want to set your own include "root", have a look at this question (specifically my answer of course :-)

Sunday, November 13, 2022
 
5

Common practice is to have a "common.php" or "includes.php" file that includes the include/include_once calls (for the sake of simplicity). e.g.

  • root
  • [lib]
    • a.php
    • b.php
    • includes.php
  • index.php

Then includes.php contains:

<?php
  include_once('a.php');
  include_once('b.php');
?>

Then in any script it's a matter of including the includes.php file.

However, to answer your original question, you can only include one file at a time, per call. You can use something like opendir and readdir to iterate over all files in a specific directory and include them as found (automated so-to-speak) or write out each include yourself based on the files you're creating.

Also, all setting the include path does is set a directory to look in when an include call is made. It's not a directory where the files should automatically be loaded (which is the impression I get from your post).

Sunday, October 23, 2022
 
5

You can use the WinAPI function ExpandEnvironmentStrings:

function ExpandEnvStr(const szInput: string): string;
  const
    MAXSIZE = 32768;
  begin
    SetLength(Result,MAXSIZE);
    SetLength(Result,ExpandEnvironmentStrings(pchar(szInput),
      @Result[1],length(Result)) - 1);
  end;
Tuesday, August 23, 2022
3

Reading your question (your project are in workspace directory) I suppose you're talking of a project in Eclipse.

Well the default directory where your app run into Eclipse is right the base dir of your project.

So if you run something like this in your main:

Files.newDirectoryStream(Paths.get("."))
     .forEach(path -> {
       System.out.println(path);
       System.out.println(path.toFile().getAbsolutePath());
     });

You should see all the files and directory that are in your project.

So if what you want is just the absolute path to your project run:

System.out.println(Paths.get(".").toFile().getAbsolutePath());

If you want open the resource Input.xml specifying only the name, I suggest to move all the files you need in a directory and run a method like this:

  public static File getFileByName(String name, String path) throws IOException {
    ArrayList<File> files = new ArrayList<>();
    Files.newDirectoryStream(Paths.get(path))
         .forEach(p -> {
           if (p.getFileName()
                   .equals(name))
             files.add(p.toFile());
         });
    return files.size() > 0 ? files.get(0) : null;
  }
Friday, October 21, 2022
 
bvoleti
 
Only authorized users can answer the search term. Please sign in first, or register a free account.
Not the answer you're looking for? Browse other questions tagged :