When working with files in PHP, one common task is to retrieve the file extension, which can be vital for various purposes like validating file types, processing uploads, or serving specific content. In this post, we’ll delve into two distinct methods for fetching file extensions in PHP, equipping you with the knowledge you need to efficiently work with different file types in your web applications. Whether you’re developing a file management system, an image gallery, or handling user uploads, understanding how to extract file extensions is a fundamental skill for any PHP developer.

This post explains about how to get file extenstion using the PHP. In the we have two types to retrieve the file extensions.

How to get file extension using PHP by Anil Kumar Panigrahi

How to get file extension using PHP by Anil Kumar Panigrahi

See the  following script :

Type 1:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
<?php
function findextension ($filename)
{
$filename = strtolower($filename) ;
$exts = split("[/\\.]", $filename) ;
$n = count($exts)-1;
$exts = $exts[$n];
return $exts;
}
$filename="image.jpg";
echo findextension ($filename);
// Here we will get the file extension

$filename="image.jpg";

echo findextension ($filename);

// Here we will get the file extenstion

?>

Type 2:

Another way to get file extension:

1
2
3
4
5
6
7
8
9
<?php

function file_extension($filename)
{
return end(explode(".", $filename));
}

echo file_extension("anil.labs.jpg");
?>

File extensions are a fundamental aspect of web development when using PHP, allowing developers to interact with a wide variety of file formats and deliver rich user experiences. This guide has provided insights into the significance of file extensions in the context of PHP, along with practical examples of how to work with different file types. From validating file uploads to generating dynamic content, PHP’s versatility in handling file extensions empowers developers to create interactive and robust web applications. By mastering the art of managing file extensions with PHP, developers can enhance their skills and offer users a seamless, feature-rich online experience. Whether it’s processing images, documents, or multimedia files, PHP remains a powerful tool for handling file extensions and ensuring that web applications can effectively manage, display, and distribute various types of content.


4 Comments

Srinivas Tamada · April 6, 2009 at 5:54 am

Can u help me.

how to redirect URL’s using .htaccess file

like this

http://domain.com/index.php?id=srinivas&page=2

to

http://domain.com/srinivas?page=2

Mustafa Ali · May 17, 2013 at 11:10 am

more simpler:
strtolower(end(explode(‘.’,$filename));

also worth reading http://php.net/manual/en/function.pathinfo.php

How to download a file using php code - Anil Labs · November 8, 2011 at 1:21 pm

[…] website have list of files and users can download those files. In my previous post i have explain how to get extension of file, using that post get the […]

Code snippets of PHP and Javascript - Anil Labs · October 29, 2013 at 6:31 am

[…] Code We can get the two type of snippets from earlier file extension using PHP […]

Leave a Reply

Avatar placeholder

Your email address will not be published. Required fields are marked *