Home > Article > Backend Development > What is the difference between php opendir and fopen
Difference: The fopen() function is used to open a file or URL; and the opendir() function is used to open a directory handle. If the fopen() function does not find the specified file, it will automatically create the file; if the opendir() function does not find the specified directory, it will directly throw an error.
The operating environment of this tutorial: windows7 system, PHP7.1 version, DELL G3 computer
fopen() function is used to open a file or URL; the opendir() function is used to open a directory handle.
Although opening a directory and opening a file both perform opening operations, the functions used are different, and the processing results for not finding the specified file are also different. fopen() function will automatically create the file if it does not find the specified file. However, the opendir() function that opens the directory is not so diligent and cheerful. It will directly throw an error.
The syntax format of the opendir() function is as follows:
opendir(string $path[, resource $context])
Among them, the parameter $path is the directory path to be opened, and $context is an optional parameter. Use To set the context of a directory handle, $context is a set of options that modify directory flow behavior.
The opendir() function returns the resource of the directory handle if the execution is successful, and returns FALSE if it fails. If the parameter $path is not a valid directory or the directory cannot be opened due to permission restrictions or file system errors, the opendir() function returns FALSE and generates a PHP error message of level E_WARNING. You can add @
symbols in front of opendir() to suppress the output of error messages.
Example: Use the opendir() function to open the specified directory
<?php $dir = './test/'; if(is_dir($dir)){ $info = opendir($dir); var_dump($info); } ?>
The running results are as follows:
resource(3) of type (stream)
In the example we use an is_dir() function, which is used to determine The given argument is not a directory.
Recommended learning: "PHP Video Tutorial"
The above is the detailed content of What is the difference between php opendir and fopen. For more information, please follow other related articles on the PHP Chinese website!