45

I have the following code from Codeigniter index.php

My understanding is that,

If / of string position in $system_folder (in this case CIcore_1_7_1) is false, and if realpath function exists AND (?) is not false, $system_folder is assigned to (?) /$system_folder. else $system_folder is assigned to $system_folder with replacing \\ with /.

Q1. What does realpath function means?

Q2. What does this mean?

@realpath(dirname(__FILE__))

Q3. Am I right? Do I have any misunderstanding?

Q4. What kind of situation do you need the following?

str_replace("\\", "/", $system_folder)

$system_folder = "CIcore_1_7_1";

/*
|---------------------------------------------------------------
| SET THE SERVER PATH
|---------------------------------------------------------------
|
| Let's attempt to determine the full-server path to the "system"
| folder in order to reduce the possibility of path problems.
| Note: We only attempt this if the user hasn't specified a 
| full server path.
|
*/

if (strpos($system_folder, '/') === FALSE)
{
    if (function_exists('realpath') AND @realpath(dirname(__FILE__)) !== FALSE)
    {
        $system_folder = realpath(dirname(__FILE__)).'/'.$system_folder;
    }
}
else
{
    // Swap directory separators to Unix style for consistency
    $system_folder = str_replace("\\", "/", $system_folder); 
}

3 Answers 3

61
  1. The realpath() function gives you the file-system path, with any symbolic links and directory traversing (e.g. ../../) resolved. The dirname() function gives you just the directory, not the file within it.

  2. __FILE__ is a magic constant that gives you the filesystem path to the current .php file (the one that __FILE__ is in, not the one it's included by if it's an include.

  3. Sounds about right.

  4. This is to translate from Windows style (\) paths to Unix style (/).

3
  • Why are there two \s, instead of one \?
    – shin
    Oct 2, 2009 at 10:03
  • 5
    Because \ is the escape character - to put a quote inside a string you use " this is \" a quote". So to put a backslash you need two of them: "this is \\ a backslash"
    – Greg
    Oct 2, 2009 at 10:06
  • excellent answer but I have a question where I can use this constant in PHP
    – simon
    Sep 13, 2019 at 17:17
13

__FILE__ is simply the name of the current file. realpath(dirname(__FILE__)) gets the name of the directory that the file is in -- in essence, the directory that the app is installed in. And @ is PHP's extremely silly way of suppressing errors.

3
  • 1
    Perfect explanation. Thanks.
    – Jonny
    Apr 1, 2014 at 18:28
  • 1
    Incorrect, since PHP 4.01 __FILE__ always returns absolute path. php.net/manual/en/language.constants.predefined.php
    – iantonuk
    Nov 12, 2017 at 4:37
  • 1
    amazing thank you so much then I can use this constants for get the directory that the file ? for example something like this : $system_folder = realpath(dirname(FILE)).'/'.$system_folder; echo $system_folder;
    – simon
    Sep 13, 2019 at 17:20
7
__FILE__

The full path and filename of the file. If used inside an include, the name of the included file is returned. Since PHP 4.0.2, FILE always contains an absolute path with symlinks resolved whereas in older versions it contained relative path under some circumstances.


string dirname  ( string $path  )

Given a string containing a path to a file, this function will return the name of the directory.


str_replace("\\", "/", $system_folder)

You need this to be consisten in path separators between different operating systems. Windows uses \ and *nix uses /, you keep with /.

2
  • if i want to block this function in php how do i should do?
    – simon
    Sep 13, 2019 at 17:40
  • @simon What do you mean by block?
    – naglas
    Dec 30, 2019 at 9:18

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

Not the answer you're looking for? Browse other questions tagged or ask your own question.