Приглашаем посетить
Бианки (bianki.lit-info.ru)

Using Relative Paths for File Access

Previous
Table of Contents
Next

Using Relative Paths for File Access

dirname(__FILE__);
basename(__FILE__);


Usually, files are opened (or searched) relative to the path of the document. If you are using PHP as an ISAPI module under Windows, the location of php4ts.dll or php5ts.dll may be relevant. To be sure that you are searching to the current scripts' path, you can use a two-step approach:

  • The constant __FILE__ contains the full path of the current script

  • The function dirname() determines the directory name portion of a path

Determining Directory Name and Filename (pathinfos.php)
<?php
  $directory = dirname(__FILE__);
  $filename = basename(__FILE__);
  print "This script is called $filename and resides
    in $directory.";
?>

To use a relative path, you can now call dirname(__FILE__) and then attach the relative path, taking into consideration the directory separator character, which is / on UNIX/Linux, \on Windows, and : on Mac OS X. Usually, / works fine on most systems, but you should note the requirements of the system on which you want to host your site.

The sister function to dirname() is basename(); this one determines the filename portion of a path.

The listing at the beginning of this phrase uses both basename() and dirname() and __FILE__ to determine information about the current path: directory and filename. Figure 6.2 shows the script's output.

Figure 6.2. Detecting the script's name and its directory.

Using Relative Paths for File Access



Previous
Table of Contents
Next