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?
$_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.
oops...having a bug... $arbitraryfilename=$_SERVER["SCRIPT_FILENAME"]; exit($arbitraryfilename); It prints: "/cgi-bin/php" ??? Maybe I didn't make myself clear. I wanted to return the name of the document (e.g. myfavoritecolas.php).
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)
This returns the full path, including the directory. This returns: Array ( ) 1 I didn't think this would be so hard. btw: I didn't install php manually, my web host had it configured for me...
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.
//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.