Cara menggunakan get file extension php

We will use the built-in function pathinfo() to get the file extension. This function extracts the path information from the given path. The correct syntax to use this function is as follows.

pathinfo($pathName, $options);

The built-in function pathinfo() has two parameters. The details of its parameters are as follows

ParametersDescription
 
2mandatoryIt is the
 
3 containing the path with file name and extension. We will extract path info from this
 
3.
 
5optionalThis parameter specifies the path elements. For example, if we want to find a file name only, we can pass
 
6 as an option. The other options are
 
7,
 
8, and
 
9.

This function returns an associative array containing a directory name, base name, extension, and file name. If the

 
0 parameter is passed, it returns a string.

The program below shows can we use the pathinfo() function to get file extension.

 

We have passed the

 
0 parameter. The function has returned a string containing file extension.

Output:

If we don’t pass the

 
0 parameter, the function will return an associative array.

 

Output:

The associative array is: 
array(4) {
  ["dirname"]=>
  string(1) "."
  ["basename"]=>
  string(20) "E:\work\CM\myppt.ppt"
  ["extension"]=>
  string(3) "ppt"
  ["filename"]=>
  string(16) "E:\work\CM\myppt"
}

Use 4 Construct and 5 Function to Get File Extension in PHP

In PHP, we can also use

 
4 construct to get the file extension. This construct will create a new SplFileInfo object. After that we can use
 
5 function to get the file extension. The correct syntax to use this construct is as follows:

$variableName = new SplFileInfo($pathName);

The construct

 
4 accepts one parameter. The detail of its parameter is as follows

ParametersDescription
 
2mandatoryIt is the
 
3 that contains our file’s path. We will use this string to extract file extension.

We will use

 
5 function to get the file extension. The correct syntax to use this function is as follows:

File handling is an important part of any web application. You often need to open and process a file for different tasks.


PHP Manipulating Files

PHP has several functions for creating, reading, uploading, and editing files.

Be careful when manipulating files!

When you are manipulating files you must be very careful.

You can do a lot of damage if you do something wrong. Common errors are: editing the wrong file, filling a hard-drive with garbage data, and deleting the content of a file by accident.


PHP readfile() Function

The readfile() function reads a file and writes it to the output buffer.

Assume we have a text file called "webdictionary.txt", stored on the server, that looks like this:

AJAX = Asynchronous JavaScript and XML
CSS = Cascading Style Sheets
HTML = Hyper Text Markup Language
PHP = PHP Hypertext Preprocessor
SQL = Structured Query Language
SVG = Scalable Vector Graphics
XML = EXtensible Markup Language

The PHP code to read the file and write it to the output buffer is as follows (the readfile() function returns the number of bytes read on success):

The readfile() function is useful if all you want to do is open up a file and read its contents.

The next chapters will teach you more about file handling.



PHP Exercises

Test Yourself With Exercises

Exercise:

Assume we have a file named "webdict.txt", write the correct syntax to open and read the file content.

pathinfo() which can be used with UTF filenames.

  function pathinfo_utf($path)
  {
    if (strpos($path, '/') !== false) $basename = end(explode('/', $path));
    elseif (strpos($path, '\\') !== false) $basename = end(explode('\\', $path));
    else return false;
    if (empty($basename)) return false;

    $dirname = substr($path, 0, strlen($path) - strlen($basename) - 1);

    if (strpos($basename, '.') !== false)
    {
      $extension = end(explode('.', $path));
      $filename = substr($basename, 0, strlen($basename) - strlen($extension) - 1);
    }
    else
    {
      $extension = '';
      $filename = $basename;
    }

    return array
    (
      'dirname' => $dirname,
      'basename' => $basename,
      'extension' => $extension,
      'filename' => $filename
    );
  }
?>