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 directory0 The file path doesn't exists in GeeksforGeeks directory1The file path doesn't exists in GeeksforGeeks directory2The file path doesn't exists in GeeksforGeeks directory3

The file path doesn't exists in GeeksforGeeks directory4 

The file path doesn't exists in GeeksforGeeks directory5 The file path doesn't exists in GeeksforGeeks directory6The file path doesn't exists in GeeksforGeeks directory7The file path doesn't exists in GeeksforGeeks directory6The file path doesn't exists in GeeksforGeeks directory0is_dir( $file )0

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

is_dir( $file )5

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

is_dir( $file )1is_dir( $file )2 Given //www.geeksforgeeks.org doesn't exists in GeeksforGeeks Portal0The file path doesn't exists in GeeksforGeeks directory3

is_dir( $file )5

The file path doesn't exists in GeeksforGeeks directory4 

Given //www.geeksforgeeks.org doesn't exists in GeeksforGeeks Portal4

OutputThe 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 //www.geeksforgeeks.org doesn't exists in GeeksforGeeks Portal5

Given //www.geeksforgeeks.org doesn't exists in GeeksforGeeks Portal6 

Given //www.geeksforgeeks.org doesn't exists in GeeksforGeeks Portal7 The file path doesn't exists in GeeksforGeeks directory1Given //www.geeksforgeeks.org doesn't exists in GeeksforGeeks Portal9The file path doesn't exists in GeeksforGeeks directory3

The file path doesn't exists in GeeksforGeeks directory4 

Given //www.geeksforgeeks.org doesn't exists in GeeksforGeeks Portal6 

mkdir(path, mode, recursive, context)3

The file path doesn't exists in GeeksforGeeks directory5 The file path doesn't exists in GeeksforGeeks directory6mkdir(path, mode, recursive, context)6The file path doesn't exists in GeeksforGeeks directory6Given //www.geeksforgeeks.org doesn't exists in GeeksforGeeks Portal7mkdir(path, mode, recursive, context)9

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

is_dir( $file )6

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

The file path doesn't exists in GeeksforGeeks directory4 

Given //www.geeksforgeeks.org doesn't exists in GeeksforGeeks Portal4

OutputGiven //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 directory4 

      15

      16

      17

      18The file path doesn't exists in GeeksforGeeks directory6<?PHP0<?PHP1

      The file path doesn't exists in GeeksforGeeks directory4 

      Given //www.geeksforgeeks.org doesn't exists in GeeksforGeeks Portal4

      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 .

Postingan terbaru

LIHAT SEMUA