Home  >  Article  >  Backend Development  >  How to check if txt file exists in php

How to check if txt file exists in php

藏色散人
藏色散人Original
2021-03-15 09:24:282111browse

How to query whether a txt file exists in php: first create a PHP sample file; then define the path of the TXT file that needs to be queried; and finally judge by "if(file_exists($filename)) {...}" txt file exists.

How to check if txt file exists in php

The operating environment of this article: windows7 system, PHP7.1 version, DELL G3 computer

file_exists - Check whether the file or directory exists

Description

file_exists ( string $filename ) : bool

Check whether the file or directory exists.

Parameters

filename

The path to the file or directory.

In Windows, use //computername/share/filename or \\computername\share\filename to check shared files on the network.

Return value

Returns true if the file or directory specified by filename exists, otherwise returns false.

Note:

This function will return false for symlinks pointing to non-existing files.

Note:

The check is done using the real UID/GID instead of the effective one.

Note: Because PHP’s integer type is a signed integer and many platforms use 32-bit integers, for files above 2GB , some file system functions may return unexpected results.

[Recommended study: "PHP Video Tutorial"]

Example

Example #1 Test whether a file exists

<?php
$filename = &#39;/path/to/foo.txt&#39;;
if (file_exists($filename)) {
    echo "The file $filename exists";
} else {
    echo "The file $filename does not exist";
}
?>

Error/Exception

Throw E_WARNING warning on failure.

Comments

Note: The result of this function will be cached. See clearstatcache() for more details.

Tips

Since PHP 5.0.0, this function is also used in some URL wrappers. See Supported Protocols and Wrapper Protocols for a list of wrappers that support the functionality of the stat() family of functions.

The above is the detailed content of How to check if txt file exists in php. For more information, please follow other related articles on the PHP Chinese website!

Statement:
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Previous article:php remove html tag styleNext article:php remove html tag style