I’m sure this is probably an easy question but I’m a PHP novice. Can somebody tell me how to seperate a path and a file? (e.g. /home/login/directory/etc/filename.jpg -> variable1 = /home/login/directory/etc/ and variable2 = filename.jpg)
there are several possible ways … I normally use
<?
$PATH = '/home/login/directory/etc/filename.jpg' ;
$file = substr( strrchr( $PATH, "/" ), 1) ;
$dir = str_replace( $file, '', $PATH ) ;
?>
[left]Thanks for your help! Could you tell me what the best way would be to go about adding an prefix in front of the file name? How’s this? (the original path/filename doesn’t contain the extension just to note)
echo "$variable1" . "john-" . "$variable2" . ".jpg";
[/left]
Why don’t you use the function pathinfo ?
like I said its only 1 of several ways , look at parse_url() as well etc …
as for how to stick it together your code would be fine, note that you dont really need to quote PHP $variables , e.g. echo $var , not echo “$var”
<?
$PATH = '/home/login/directory/etc/filename.jpg' ;
$file = substr(strrchr($PATH, "/"), 1);
$dir=str_replace($file,'',$PATH);
echo $dir . 'john-' . $file . '.jpg' ;
?>
Thanks for the help!