How to get a file in PHP?

In PHP, there exist various ways of getting a file extension. In our tutorial, we have chosen the 5 best ways that will help you to get a file extension straightforwardly.

Go ahead and choose the one that matches your project better.

The first way is exploding a file variable and getting the last element of the array. That will be a file extension. Here is how you can do it:

In the framework of the second approach, we recommend detecting the last occurrence of ‘.’, which will return a ‘.jpg’. Afterwards, you need to remove the first character of a string using substr. As a result, the exact file extension ‘.jpg’ will be returned.

Here is an example:

The next way is to use strrpos for detecting the position of the last occurrence of ‘.’ inside a file name, incrementing that position by 1 so that it explodes a string from (.).

The example is shown below:

In the framework of this approach, you should apply a regular expression reach and replacement.

Here is an example:

And, the final approach that we recommend is to use the PHP pathinfo() function. It is aimed at returning information about the file. In case the second optional parameter is not passed, an associative array will be returned. It will include the dirname, extension, basename, and filename. Once the second parameter is passed, then specific information will be returned.

Here is an example:

So, after learning about all the possible ways described above, you are free to choose the best one for you. In our opinion, the simplest, yet the most efficient way among all these options is the pathinfo() function.

$_SERVER is an array of stored information such as headers, paths, and script locations. These entries are created by the webserver. There is no other way that every web server will provide any of this information.

Syntax: 

 $_SERVER[‘SCRIPT_NAME’]
  • ‘SCRIPT_NAME’ gives the path from the root to include the name of the directory.

Method 1: The following method uses the strpos() and substr() methods to print the values of the last occurrences.

PHP




<?php

function fileExtension($s) {

 $_SERVER[‘SCRIPT_NAME’]
0
 $_SERVER[‘SCRIPT_NAME’]
1

 $_SERVER[‘SCRIPT_NAME’]
0
 $_SERVER[‘SCRIPT_NAME’]
3

 $_SERVER[‘SCRIPT_NAME’]
0
 $_SERVER[‘SCRIPT_NAME’]
5

 $_SERVER[‘SCRIPT_NAME’]
0
 $_SERVER[‘SCRIPT_NAME’]
7
 $_SERVER[‘SCRIPT_NAME’]
8
 $_SERVER[‘SCRIPT_NAME’]
9
php
0$s
php
2
php
3
php
4

 $_SERVER[‘SCRIPT_NAME’]

 $_SERVER[‘SCRIPT_NAME’]
0
php
7

 $_SERVER[‘SCRIPT_NAME’]
0
php
9
php
0
 $_SERVER[‘SCRIPT_NAME’]
7
 Name: 001510d47316b41e63f337e33f4aaea4
 Extension: php
2

 Name: 001510d47316b41e63f337e33f4aaea4
 Extension: php
3
 Name: 001510d47316b41e63f337e33f4aaea4
 Extension: php
4
 Name: 001510d47316b41e63f337e33f4aaea4
 Extension: php
5
 Name: 001510d47316b41e63f337e33f4aaea4
 Extension: php
6

 $_SERVER[‘SCRIPT_NAME’]
0
 Name: 001510d47316b41e63f337e33f4aaea4
 Extension: php
8

 Name: 001510d47316b41e63f337e33f4aaea4
 Extension: php
3
 Name: 001510d47316b41e63f337e33f4aaea4
 Extension: php
4
 Name: file.index
 Extension: php
1
php
0$s
php
2
 $_SERVER[‘SCRIPT_NAME’]
7
 Name: file.index
 Extension: php
6

 Name: file.index
 Extension: php
7

 Name: 001510d47316b41e63f337e33f4aaea4
 Extension: php

 Name: file.index
 Extension: php
9

<?php0

 $_SERVER[‘SCRIPT_NAME’]
8<?php2<?php3<?php4<?php5

 

<?php6

<?php7 fileExtension(<?php0

php
4

function1

Output

php

Method 2: The following method uses a predefined function pathinfo(). In the output, the “Name:” shows the name of the file and “Extension:” shows the file extension.

How to fetch file in PHP?

The file_get_contents() reads a file into a string. This function is the preferred way to read the contents of a file into a string. It will use memory mapping techniques, if this is supported by the server, to enhance performance.

How to download a file using PHP?

PHP enables you to download file easily using built-in readfile() function. The readfile() function reads a file and writes it to the output buffer..
<? ... .
header('Content-Type: application/octet-stream');.
header("Content-Transfer-Encoding: utf-8");.

How to get the URL of a file in PHP?

Create a PHP variable that will store the URL in string format. Check whether the HTTPS is enabled by the server. If it is, append “https” to the URL string. If HTTPS is not enabled, append “http” to the URL string.

How to get data from text file in PHP?

The PHP fgetc() function is used to read single character from the file. To get all data using fgetc() function, use ! feof() function inside the while loop.