Support Joomla!

references:joomla.framework:filesystem:jfile-getname

Table of Contents

getName

API Package Subpackage Class Method Reference Last reviewed Doc status
API Home Package Joomla.Framework Subpackage FileSystem Class JFile Method getName Reference getName() Never Work in Progress

This function returns the file name component of a provided path. Unlike the built-in PHP function basename(), an empty string is returned if the path ends with a trailing *directory separator*.

Warning: Do not use this with URIs! Use the JURI class instead which provides several methods to extract bits and pieces of URIs and URLs.

Syntax

boolean getName ( $file )

$file string is a string that contains a file path.

Examples

Example 1: This example returns the filename component of each provided path.

Example 1

$filename = JFile::getName( 'images/stories/clock.jpg' );
echo "File 1: '", $filename, "'";

$filename = JFile::getName( 'images/stories/' );
echo "File 2: '", $filename, "'";

$filename = JFile::getName( 'images/stories/foobar' );
echo "File 3: '", $filename, "'";

will produce:

Results

File 1: "clock.jpg"
File 2: ""
File 2: "foobar"

Example 2: The same examples using PHP‘s basename():

Example 2

$filename = basename( 'images/stories/clock.jpg' );
echo "File 1: '", $filename, "'";

$filename = basename( 'images/stories/' );
echo "File 2: '", $filename, "'";

$filename = basename( 'images/stories/foobar' );
echo "File 3: '", $filename, "'";

will produce:

Results

File 1: "clock.jpg"
File 2: "stories"
File 2: "foobar"

This function utilizes the DS constant which holds the valid directory separator character of the webserver’s operating system; / on UNIX-systems, \ on Windows. Additionally there is no check provided whether the path or file are actually present on the webserver’s filesystem; use JFolder::exists or JFile::exists if you need to know.


Discussion

Full name:
E-Mail:
 
references/joomla.framework/filesystem/jfile-getname.txt (680 views) · Last modified: 2007/08/15 15:05