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. Show Methods:
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
The file path doesn't exists in GeeksforGeeks directory0 The file path doesn't exists in GeeksforGeeks directory1 The file path doesn't exists in GeeksforGeeks directory2 The 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 directory6 The file path doesn't exists in GeeksforGeeks directory7 The file path doesn't exists in GeeksforGeeks directory6 The file path doesn't exists in GeeksforGeeks directory0 is_dir( $file )0 is_dir( $file )1 is_dir( $file )2 is_dir( $file )3 The file path doesn't exists in GeeksforGeeks directory3 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 Portal0 The file path doesn't exists in GeeksforGeeks directory3 is_dir( $file )5 The file path doesn't exists in GeeksforGeeks directory4 Given https://www.geeksforgeeks.org doesn't exists in GeeksforGeeks Portal4 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: PHPGiven https://www.geeksforgeeks.org doesn't exists in GeeksforGeeks Portal5 Given https://www.geeksforgeeks.org doesn't exists in GeeksforGeeks Portal6 Given https://www.geeksforgeeks.org doesn't exists in GeeksforGeeks Portal7 The file path doesn't exists in GeeksforGeeks directory1 Given https://www.geeksforgeeks.org doesn't exists in GeeksforGeeks Portal9 The file path doesn't exists in GeeksforGeeks directory3 The file path doesn't exists in GeeksforGeeks directory4 Given https://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 directory6 mkdir(path, mode, recursive, context)6 The file path doesn't exists in GeeksforGeeks directory6 Given https://www.geeksforgeeks.org doesn't exists in GeeksforGeeks Portal7 mkdir(path, mode, recursive, context)9 is_dir( $file )1 is_dir( $file )2 The file path doesn't exists in GeeksforGeeks directory6 13 14 is_dir( $file )6 is_dir( $file )1 is_dir( $file )2 The file path doesn't exists in GeeksforGeeks directory6 19 14 The file path doesn't exists in GeeksforGeeks directory4 Given https://www.geeksforgeeks.org doesn't exists in GeeksforGeeks Portal4 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:
Return Value: It returns true on success or false on failure. Example: PHP
The file path doesn't exists in GeeksforGeeks directory4 15 16 17 18 The file path doesn't exists in GeeksforGeeks directory6 <?PHP 0<?PHP 1The file path doesn't exists in GeeksforGeeks directory4 Given https://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 .
|