PHP get file name suffix Instance Summary _php tutorial

Source: Internet
Author: User
In PHP file upload we need to get the file name suffix after the simple file type judgment, and in PHP, the file name suffix is very simple and there are many kinds, let me summarize below.

1.basename ()-Returns the file name of the path

Take a look at the following PHP code:

The code is as follows Copy Code

$path = "/usr/www/html/index.php";
Echo basename ($path). "
";
If you select suffix, the extension is ignored
Echo basename ($path, ". php");
?>


Operation Result:
index.php
Index
2.dirname ()-Returns the file path of the current script!
PHP Code:

The code is as follows Copy Code


--file__ return file Full path
$dir = DirName (__file__);
Echo $dir;
?>

Operation Result:
F:webzendexercise

3.pathinfo () returns an associative array containing information about path.
Includes the following array cells: path name dirname, file name basename, and extension name extension.
Take a look at the following simple code demo:

The code is as follows Copy Code
$path = "/usr/www/html/index.php";
$pathinfo = PathInfo ($path);
echo "Catalog name: $pathinfo [dirname]
";
echo "File name: $pathinfo [basename]
";
echo "extension: $pathinfo [extension]";
?>

Operation Result:
Catalog Name:/usr/www/html
File name: index.php
Extension: PHP
4.realpath--Returns the normalized absolute path name
The PHP code is as follows:

The code is as follows Copy Code


$path = "./exercise/php.txt";
$realpath = Realpath ($path);
Echo $realpath;
?>

Finally note a tip: Different paths of File path operators may, different, Windows can use "/" and "",
Linux can only use "/", so the development of the time, the recommendations are used "/", such as my file path above the wording!

Method One:

The code is as follows Copy Code
function Extend_1 ($file _name)
{
$retval = "";
$pt =strrpos ($file _name, ".");
if ($pt) $retval =substr ($file _name, $pt +1, strlen ($file _name)-$pt);
return ($retval);
}

Method Two
PHP code

The code is as follows Copy Code
function Extend_2 ($file _name)
{
$extend = PathInfo ($file _name);
$extend = Strtolower ($extend ["extension"]);
return $extend;
}


Method Three
PHP code

The code is as follows Copy Code
function Extend_3 ($file _name)
{
$extend =explode (".", $file _name);
$va =count ($extend)-1;
return $extend [$va];
}


Method Four
PHP code

The code is as follows Copy Code
function Getfileext ($file _name)
{
while ($dot = Strpos ($file _name, "."))
{
$file _name = substr ($file _name, $dot + 1);
}
return $file _name;
}

?>

http://www.bkjia.com/PHPjc/633058.html www.bkjia.com true http://www.bkjia.com/PHPjc/633058.html techarticle in the php file upload we need to get the file name suffix after the simple file type judgment, and in PHP, the file name suffix is very simple and there are many kinds, let me summarize ...

  • Contact Us

    The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

    If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

    A Free Trial That Lets You Build Big!

    Start building with 50+ products and up to 12 months usage for Elastic Compute Service

    • Sales Support

      1 on 1 presale consultation

    • After-Sales Support

      24/7 Technical Support 6 Free Tickets per Quarter Faster Response

    • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.