How to print file name in php

Possible Duplicate:
Getting the name of file in PHP

For example, I a php file called: hello.php

Can I print back the

hello.php

from code? Thanks.

asked Jan 20, 2012 at 14:33

DNB5brimsDNB5brims

28k47 gold badges128 silver badges192 bronze badges

0

echo __FILE__ ;

has the full name

answered Jan 20, 2012 at 14:35

Eugen RieckEugen Rieck

63k10 gold badges69 silver badges92 bronze badges

Yes, combine the __FILE__ magic constant with basename.

echo basename(__FILE__);

answered Jan 20, 2012 at 14:35

Alex TurpinAlex Turpin

46.1k23 gold badges111 silver badges144 bronze badges

You can use basename( __FILE__), take a look at predefined constants in php, there's also lot of interesting information about the script in $_SERVER, just use print_r( $_SERVER); and study values.

answered Jan 20, 2012 at 14:35

VyktorVyktor

20.1k5 gold badges59 silver badges95 bronze badges

when running in SAPI mode (in a webserver), you can use $_SERVER['SCRIPT_NAME']. For CLI mode (command line), there's $argv[0] ($argv contains the command-line arguments, and element 0 is always the script name).

answered Jan 20, 2012 at 14:36

Marc BMarc B

351k42 gold badges402 silver badges486 bronze badges

$_SERVER['PHP_SELF'] gives the path to the running script. This is often a better solution than __FILE__ because your code may reside in another file imported by the script that is actually running.

answered Jan 20, 2012 at 14:37

View Discussion

Improve Article

Save Article

  • Read
  • Discuss
  • View Discussion

    Improve Article

    Save Article

    In this article, we will learn how to get the current filename using PHP.

    Input : c:/xampp/htdocs/project/home.php
    Output : project/home.php
    
    Input : c:/xampp/htdocs/project/abc.txt
    Output : project/abc.txt

    Sometimes, we need to get the current file name with the directory name in which our page is stored for different purposes. Since we are using PHP, we can easily get the current file name or directory name of the current page by using $_SERVER[‘SCRIPT_NAME’].

    Using $_SERVER[‘SCRIPT_NAME’]: $_SERVER is an array of stored information such as headers, paths, and script locations. These entries are created by the webserver. There is no other way that every web server will provide any of this information.

    Syntax: ‘SCRIPT_NAME’gives the path from the root to include the name of the directory.

    1. To get the current file name. We use
      $currentPage= $_SERVER['SCRIPT_NAME'];
    2. To show the current file name. We use
      echo $currentPage;

    PHP code: The following is the complete code to get the current file name with the directory name.

    PHP

    $currentPage= $_SERVER['SCRIPT_NAME'];

    $currentPage = substr($currentPage, 1);

    echo $currentPage

    ?>

    Output:

       project/home.php

     In this code, substr() PHP function is used to extract the part of string from $currentPage variable string. 

    How do I get the current file name?

    Syntax: 'SCRIPT_NAME' gives the path from the root to include the name of the directory..
    To get the current file name. We use $currentPage= $_SERVER['SCRIPT_NAME'];.
    To show the current file name. We use. echo $currentPage;.

    How do you name a file in PHP?

    File Naming should be in all lowercase. Furthermore, class file names should match the name of the class itself. For example, if you have a class named Myclass , then its filename must be Myclass. php.

    How can get upload file name in PHP?

    In PHP, we can access the actual name of the file which we are uploading by keyword $_FILES[“file”][“name”]. The $_FILES is the by default keyword in PHP to access the details of files that we uploaded. The file refers to the name which is defined in the “index. html” form in the input of the file.

    What is $_ files in PHP?

    PHP $_FILES The global predefined variable $_FILES is an associative array containing items uploaded via HTTP POST method. Uploading a file requires HTTP POST method form with enctype attribute set to multipart/form-data.