• Howdy! Welcome to our community of more than 130.000 members devoted to web hosting. This is a great place to get special offers from web hosts and post your own requests or ads. To start posting sign up here. Cheers! /Peo, FreeWebSpace.net
managed wordpress hosting

Get Filename

This is a quick way of doing it from what i can think:
PHP:
<?php
$url = "www.domainname.com/images/file.jpg";
$part1 = expand("/", $url);
echo $part1[3];
?>
 
Elaborating on Jonny's, because there may be more than 1 directory:

PHP:
<?php
$url = "www.domainname.com/images/file.jpg";
$part1 = expand("/", $url);
echo $part1[count($part1)-1];
?>
 
another one:
PHP:
$uri = "www.domainname.com/images/file.jpg";
$parts = explode('/' , $uri);
echo end($parts);
 
It's not about doing one better, I say this all the time, and anyone who is even a novice programmer will understand: The php authors have spent years developing php, they have spent countless hours on each and every function or class you use daily to achieve everything, if these people, with an awsome amount of experience, create a function for a purpose you can be damn sure that however they are doing it is best. So if there's a function for something, like get the filename from a path or url, or format a string properly, or anything you come across, you should definately use the predefined functions written by people with collectively hundreds of years experience in programming and most importantly programming php .....
 
I was only messing about, but there is some neat functions like that which not a lot of people use and know about.
 
theres a lot of functions like that actually, little known functions that server a highly specific purpose. just got to know them i guess.
 
Actually, they are some of the best known features of programming and not just with PHP. Basename is part of the POSIX standard ANSI C library....

Code:
#include <libgen.h>
#include <stdio.h>

int main( int argc, char *argv[]  )
{
        if( argv[1] )
        {
                puts( basename( argv[1] ) );
        }
        return 0;
}

basename exists as an executable on most unix systems because of this ^^ the above is a lot like the source ...

I maintain before you attempt to master PHP, you should have C under your belt ....
 
Back
Top