Forum Moderators: coopster

Message Too Old, No Replies

Filename

         

branmh

1:21 pm on Aug 18, 2003 (gmt 0)

10+ Year Member



How could I get the Filename out of this /dir1/filename.php

johannes

2:11 pm on Aug 18, 2003 (gmt 0)

10+ Year Member



I assume that $path="/dir1/filename.php";

Then you can get the filename with:
$filename=substr($path,strrpos($path,"/")+1);

jamie

2:12 pm on Aug 18, 2003 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member Top Contributors Of The Month



PATH_INFO gives you everything after the domain.

so explode your PATH_INFO - i.e. separate it into an array splitting it at the character you specify:

$new_array = explode("/", $PATH_INFO);

this gives you an array with three variables

$new_array[0] is empty (the bit to the left of the first slash don't ask why ;-)

$new_array[1] is 'dir1'

$new_array[2] is filename.php

then strip off the last 4 characters (.php) of $new_array[2] to leave just 'filename':

$filename = substr($new_array[2], 0, -4);

<added> that's much quicker johannes, - cheers :-))
allthough would have to substr to remove the .php

branmh

9:16 pm on Aug 20, 2003 (gmt 0)

10+ Year Member



I need this to be a preg_replace command.

coopster

9:26 pm on Aug 20, 2003 (gmt 0)

WebmasterWorld Administrator 10+ Year Member



Try the basename function:

<?php
$path = "/home/httpd/html/index.php";
$file = basename ($path); // $file is set to "index.php"
$file = basename ($path,".php"); // $file is set to "index"
?>

vincevincevince

9:54 pm on Aug 20, 2003 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



How could I get the Filename out of this /dir1/filename.php
i need this to be a preg_replace.


$text=preg_replace("/\/[^\/]\/(.*)\.php/i","$1",$text);