Home >Backend Development >PHP Tutorial >PHP function introduction: file_exists() function
Introduction to PHP functions: file_exists() function, specific code examples are required
In PHP programming, we often need to determine whether a file exists. At this time, we can use PHP The built-in file_exists() function. This article will introduce how to use the file_exists() function and some precautions.
1. Introduction to file_exists() function
The file_exists() function is used to check whether a file or directory exists. If it exists, it returns true, otherwise it returns false. This function can accept a parameter, which can be a file name or directory name, and supports the inspection of local files and remote files.
2. Function syntax
file_exists(file)
Parameter description: file - file name or directory name, if it is a remote file, it needs to be http:// or ftp:// starts.
Return value: Returns true if the file or directory exists, otherwise returns false.
3. Sample code
The following code demonstrates how to use the file_exists() function.
//Check whether the local file exists
$file='./test.txt';
if(file_exists($file)){
echo 'File exists';
}else{
echo 'File does not exist';
}
//Check whether the remote file exists
$file='http://www.example.com/test. txt';
if(file_exists($file)){
echo 'The file exists';
}else{
echo 'The file does not exist';
}
//Check whether the directory exists
$dir='./example';
if(file_exists($dir)){
echo 'The directory exists';
}else{
echo ' The directory does not exist';
}
4. Notes
In short, the file_exists() function is used very frequently in PHP programming. It can easily determine whether files and directories exist, thereby reducing code complexity and improving coding efficiency.
The above is the detailed content of PHP function introduction: file_exists() function. For more information, please follow other related articles on the PHP Chinese website!