Asked  7 Months ago    Answers:  5   Viewed   30 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

99

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, March 31, 2021
 
hakimoun
answered 7 Months ago
72

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).

Wednesday, March 31, 2021
 
letrollpoilu
answered 7 Months ago
38

The __FILE__ constant returns the full path and filename of the file containing that line. It is possible for this file to be located inside a directory other than the current working directory. In your particular example you can use this:

include dirname(dirname(__FILE__)) . DIRECTORY_SEPARATOR . 'htmlPurifierAndMarkdown.php';

An example of what might be going on is as follows:

  1. The file being executed is /example.com/public_html/index.php
  2. Current working directory is /example.com/public_html/
  3. The index file includes /example.com/public_html/includes/classes/genral.class.php
  4. The above file attempts to include ../htmlPurifierAndMarkdown.php
  5. The .. is translated relative to current working directory and not relative to the included file. Thus you end up with /example.com/htmlPurifierAndMarkdown.php and a nasty error message.
Saturday, May 29, 2021
 
insomiac
answered 5 Months ago
38

os.path.abspath makes sure a path is absolute. Use the following helper function:

import os

def absoluteFilePaths(directory):
    for dirpath,_,filenames in os.walk(directory):
        for f in filenames:
            yield os.path.abspath(os.path.join(dirpath, f))
Tuesday, July 27, 2021
 
talkhabi
answered 3 Months ago
67

If you app is insalled on sdcard try this code :

Bitmap bitMap = BitmapFactory.decodeResource(getResources(),R.id.img1);

File mFile1 = Environment.getExternalStorageDirectory();

String fileName ="img1.jpg";

File mFile2 = new File(mFile1,fileName);
try {
       FileOutputStream outStream;

       outStream = new FileOutputStream(mFile2);

       bitmap.compress(Bitmap.CompressFormat.JPEG, 100, outStream);

       outStream.flush();

       outStream.close();

} catch (FileNotFoundException e) {
       // TODO Auto-generated catch block
       e.printStackTrace();
} catch (IOException e) {
       // TODO Auto-generated catch block
       e.printStackTrace();
}

String sdPath = mFile1.getAbsolutePath().toString()+"/"+fileName;

Log.i("hiya", "Your IMAGE ABSOLUTE PATH:-"+sdPath); 

File temp=new File(sdPath);

if(!temp.exists()){
       Log.e("file","no image file at location :"+sdPath);
}
Friday, July 30, 2021
 
barden
answered 3 Months ago
Only authorized users can answer the question. Please sign in first, or register a free account.
Not the answer you're looking for? Browse other questions tagged :