Hello and welcome to our community! Is this your first visit?
Register
Enjoy an ad free experience by logging in. Not a member yet? Register.
Results 1 to 3 of 3
  1. #1
    Regular Coder
    Join Date
    Jul 2002
    Location
    New Zealand
    Posts
    127
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Get A File's Name

    Say I have a file located at www.site.com/folder/file.php. I would like file.php to detect it's own file name (as the same script is used in multiple files).

    $_SERVER['PHP_SELF'] will return folder/file.php

    I really want to get the file name only (minus the folder(s) or the extension).

    Anyone know if this is this possible?
    eTheory - the theory of revolution

  • #2
    Regular Coder
    Join Date
    Jun 2002
    Location
    UK
    Posts
    577
    Thanks
    0
    Thanked 0 Times in 0 Posts
    I think you might need to substr the return from $_SERVER['PHP_SELF'] to get just the script name.

    $the_lot = $_SERVER['PHP_SELF'];
    $the_script = substr($the_lot,strrpos($the_lot,'/'));

    if you wanted just 'file' rather than file.php - maybe

    $the_script = substr($the_lot,strrpos($the_lot,'/'),strrpos($the_lot,'.')-strlen($the_lot));

    might need a +1 or -1 here or there.
    Ökii - formerly pootergeist
    teckis - take your time and it'll save you time.

  • #3
    Senior Coder
    Join Date
    Jun 2002
    Location
    frankfurt, german banana republic
    Posts
    1,848
    Thanks
    0
    Thanked 0 Times in 0 Posts
    Use

    basename($_SERVER['PHP_SELF]);

    to get only the file name + extension, and

    preg_replace('/(.+)(\..+?)$/', '\\1', basename($_SERVER['PHP_SELF']));

    to get only the filename without the extension. But beware - a file is not required to have a file extension, and the .something part may be irrelevant to the file's purpose.


  •  

    Posting Permissions

    • You may not post new threads
    • You may not post replies
    • You may not post attachments
    • You may not edit your posts
    •