dirname()function in PHP


The dirname() function returns the directory name from the specified path. If the path has no slashes, a dot ('.') is returned. This indicates the current directory.

Syntax

dirname(file_path)

Parameters

  • file_path − File to be specified.

Return

The dirname() function returns the path of the directory.

Example

 Live Demo

<?php
   $file_path = "D:/amit/java.docx";
   $dir_name = dirname($file_path);
   echo "Name of the directory: $dir_name
"; ?>

Output

Name of the directory: D:/amit

Let us see another example.

Example

 Live Demo

<?php
   $file_path = "/amit/java.docx";
   $dir_name = dirname($file_path);
   echo "Name of the directory: $dir_name
"; ?>

Output

Name of the directory: /amit

Samual Sam
Samual Sam

Learning faster. Every day.

Updated on: 24-Jun-2020

364 Views

Kickstart Your Career

Get certified by completing the course

Get Started
Advertisements