PHP exists

We can easily create a folder in PHP, but before that, you have to check if the folder or directory already exists or not. So In this article, you will learn both to Check and Create a folder or directory in PHP. 

Methods:

  1. file_exists(): It is an inbuilt function that is used to check whether a file or directory exists or not.
  2. is_dir(): It is also used to check whether a file or directory exists or not.
  3. mkdir() :  This function creates a directory.

Method 1: Using file_exists() function: The file_exists() function is used to check whether a file or directory exists or not.

Syntax:

file_exists( $path )

Parameters: The file_exists() function in PHP accepts only one parameter $path. It specifies the path of the file or directory you want to check.

Return Value: It returns True on success and false on failure. 

Example:

PHP




<?PHP

      

// Checking whether file exists or not

The file path doesn't exists in GeeksforGeeks directory
0
The file path doesn't exists in GeeksforGeeks directory
1
The file path doesn't exists in GeeksforGeeks directory
2
The file path doesn't exists in GeeksforGeeks directory
3

The file path doesn't exists in GeeksforGeeks directory

The file path doesn't exists in GeeksforGeeks directory
5
The file path doesn't exists in GeeksforGeeks directory
6
The file path doesn't exists in GeeksforGeeks directory
7
The file path doesn't exists in GeeksforGeeks directory
6
The file path doesn't exists in GeeksforGeeks directory
0
is_dir( $file )
0

is_dir( $file )
1
is_dir( $file )
2
is_dir( $file )
3
The file path doesn't exists in GeeksforGeeks directory
3

is_dir( $file )
5

is_dir( $file )
6
is_dir( $file )
7

is_dir( $file )
1
is_dir( $file )
2
Given https://www.geeksforgeeks.org doesn't exists in GeeksforGeeks Portal
0
The file path doesn't exists in GeeksforGeeks directory
3

is_dir( $file )
5

The file path doesn't exists in GeeksforGeeks directory

Given https://www.geeksforgeeks.org doesn't exists in GeeksforGeeks Portal
4

Output

The file path doesn't exists in GeeksforGeeks directory

Method 2: Using is_dir() function: The is_dir() function is used to check whether the specified file is a directory or not.

Syntax:

is_dir( $file )

Parameters: The is_dir() function in PHP accepts only one parameter. It specifies the path of the file or directory that you want to check.

Return Value: It returns True if the file is a directory otherwise returns false.

Example:

PHP




Given https://www.geeksforgeeks.org doesn't exists in GeeksforGeeks Portal
5

Given https://www.geeksforgeeks.org doesn't exists in GeeksforGeeks Portal

Given https://www.geeksforgeeks.org doesn't exists in GeeksforGeeks Portal
7
The file path doesn't exists in GeeksforGeeks directory
1
Given https://www.geeksforgeeks.org doesn't exists in GeeksforGeeks Portal
9
The file path doesn't exists in GeeksforGeeks directory
3

The file path doesn't exists in GeeksforGeeks directory

Given https://www.geeksforgeeks.org doesn't exists in GeeksforGeeks Portal

mkdir(path, mode, recursive, context)
3

The file path doesn't exists in GeeksforGeeks directory
5
The file path doesn't exists in GeeksforGeeks directory
6
mkdir(path, mode, recursive, context)
6
The file path doesn't exists in GeeksforGeeks directory
6
Given https://www.geeksforgeeks.org doesn't exists in GeeksforGeeks Portal
7
mkdir(path, mode, recursive, context)
9

is_dir( $file )
1
is_dir( $file )
2
The file path doesn't exists in GeeksforGeeks directory
6
1
3
1
4

is_dir( $file )
6

is_dir( $file )
1
is_dir( $file )
2
The file path doesn't exists in GeeksforGeeks directory
6
1
9
1
4

The file path doesn't exists in GeeksforGeeks directory

Given https://www.geeksforgeeks.org doesn't exists in GeeksforGeeks Portal
4

Output

Given https://www.geeksforgeeks.org doesn't exists in GeeksforGeeks Portal

Method 3: Using mkdir() function: The mkdir() creates a new directory with the specified pathname.

Syntax:

mkdir(path, mode, recursive, context)

Parameters:

  • path: It is a mandatory parameter that specifies the path.
  • mode: It is an optional parameter that specifies permission. The mode parameter consists of four numbers and by default, the mode is 0777.
    • The first number is always zero.
    • The second number specifies permissions for the owner.
    • The third number specifies permissions for the owner’s user group.
    • The fourth number specifies permissions for everybody else.
  • recursive: It is an optional parameter that can be used to set recursive mode.
  • context: It is an optional parameter that specifies the behavior of the stream.
    • Return Value: It returns true on success or false on failure. 

      Example:

      PHP




      <?PHP

      The file path doesn't exists in GeeksforGeeks directory

      1
      5

      1
      6

      1
      7

      1
      8
      The file path doesn't exists in GeeksforGeeks directory
      6<?PHP0<?PHP1

      The file path doesn't exists in GeeksforGeeks directory

      Given https://www.geeksforgeeks.org doesn't exists in GeeksforGeeks Portal
      4

      Output:

      1

      Example: This example checks the file exists or not and if file doesn’t exist then create a new file using mkdir() function.

      How to check if PHP function exists?

      The function_exists() is an inbuilt function in PHP. The function_exists() function is useful in case if we want to check whether a function() exists or not in the PHP script. It is used to check for both built-in functions as well as user-defined functions.

      How to check if a key exists or not in an object PHP?

      PHP array_key_exists() Function The array_key_exists() function checks an array for a specified key, and returns true if the key exists and false if the key does not exist.

      How to check if a value exists in an object in PHP?

      The is_object() function checks whether a variable is an object. This function returns true (1) if the variable is an object, otherwise it returns false/nothing.

      How to check if a parameter exists in PHP?

      To check if a URL parameter exists in PHP, you can use the isset function. This function returns a boolean value indicating whether a variable with the specified name has been set and is not NULL .