Return name of the file / string manipulation

Discussion in 'PHP' started by david_sakh, Jan 25, 2005.

  1. #1
    Is there any way to return the filename of a document in php?

    Also, I'm a little foggy about string manipulation, how can I delete, say, the first or last x characters from a given string?
     
    david_sakh, Jan 25, 2005 IP
  2. digitalpoint

    digitalpoint Overlord of no one Staff

    Messages:
    38,334
    Likes Received:
    2,613
    Best Answers:
    462
    Trophy Points:
    710
    Digital Goods:
    29
    #2
    $_SERVER["SCRIPT_FILENAME"] will give you the file name of your script.

    substr($string, x) will give you the first x characters and substr($string, -x) will give you the last x characters.
     
    digitalpoint, Jan 25, 2005 IP
    david_sakh likes this.
  3. david_sakh

    david_sakh Peon

    Messages:
    1,225
    Likes Received:
    29
    Best Answers:
    0
    Trophy Points:
    0
    #3
    oops...having a bug...

    $arbitraryfilename=$_SERVER["SCRIPT_FILENAME"];
    exit($arbitraryfilename);

    It prints: "/cgi-bin/php"

    ???

    Maybe I didn't make myself clear. :eek: I wanted to return the name of the document (e.g. myfavoritecolas.php).
     
    david_sakh, Jan 25, 2005 IP
  4. digitalpoint

    digitalpoint Overlord of no one Staff

    Messages:
    38,334
    Likes Received:
    2,613
    Best Answers:
    462
    Trophy Points:
    710
    Digital Goods:
    29
    #4
    Oh... if you are running it from a shell, you would use the $argv array... for example: $argv[1]
     
    digitalpoint, Jan 25, 2005 IP
  5. david_sakh

    david_sakh Peon

    Messages:
    1,225
    Likes Received:
    29
    Best Answers:
    0
    Trophy Points:
    0
    #5
    God I'm so stupid. I just can't get it to return anything....this returns nothing:

    
    $arbitraryfilename=$_SERVER[$argv[1]];
    exit($arbitraryfilename);
    
    Code (markup):
    neither does

    
    $arbitraryfilename=$argv[1];
    exit($arbitraryfilename);
    
    Code (markup):
    I think I'm doing something wrong, but this seemed like the right syntax after I did a few g searches on argv...

    Does anyone know what is stored in argv? I wouldn't know the dif between each array element (especially since they seem to be empty)
     
    david_sakh, Jan 25, 2005 IP
  6. hergo

    hergo Peon

    Messages:
    85
    Likes Received:
    1
    Best Answers:
    0
    Trophy Points:
    0
    #6
    what about $_SERVER["PHP_SELF"]
     
    hergo, Jan 25, 2005 IP
  7. digitalpoint

    digitalpoint Overlord of no one Staff

    Messages:
    38,334
    Likes Received:
    2,613
    Best Answers:
    462
    Trophy Points:
    710
    Digital Goods:
    29
    #7
    Are you running it from a shell? Try print_r ($argv);
     
    digitalpoint, Jan 25, 2005 IP
  8. david_sakh

    david_sakh Peon

    Messages:
    1,225
    Likes Received:
    29
    Best Answers:
    0
    Trophy Points:
    0
    #8
    This returns the full path, including the directory. :(

    This returns: Array ( ) 1

    I didn't think this would be so hard. :confused:

    btw: I didn't install php manually, my web host had it configured for me...
     
    david_sakh, Jan 25, 2005 IP
  9. nevetS

    nevetS Evolving Dragon

    Messages:
    2,544
    Likes Received:
    211
    Best Answers:
    0
    Trophy Points:
    135
    #9
    I think $php_self returns different values based on compilation options and it changed a little sometime along the 4.x set of releases.

    you can build a function to suit your needs using php_self and midstr or substr. Look it up at php.net and you'll see some examples. I also think there are some functions for parsing a URL to get the filename, but I haven't been down that road in a while.

    Sorry I couldn't be more specific, but hopefully I'm pointing you down a useful path.
     
    nevetS, Jan 25, 2005 IP
  10. david_sakh

    david_sakh Peon

    Messages:
    1,225
    Likes Received:
    29
    Best Answers:
    0
    Trophy Points:
    0
    #10
    
    //this script obtains the name of the current file
    //David Saharkhiz, [url]http://www.websitenova.com[/url]
    $path=$_SERVER["PHP_SELF"];
    //get the name of the subpath (file not included) and the length of the full path (file included)
    $subpath=dirname($path);
    $subpathlength=(strlen($subpath));
    $pathlength=(strlen($path));
    $resultlength=-($pathlength-$subpathlength-1);
    //now we want the last x characters (-x)
    $arbitraryfilename=substr($path, $resultlength);
    exit($arbitraryfilename);
    
    Code (markup):
    It took a good hour, but I finally got this to work. yay!

    Thanks shawn, nevet, and hergo.
     
    david_sakh, Jan 25, 2005 IP