Checking if there is a php file. Checking the existence of a file using a URL link. Assigning rights when creating a folder

There are times when you need to check whether a specified file exists or not, for example, in order to subsequently perform some actions on the file.

I also encountered this issue when developing the module. And I found two options for solving the problem.

Checking the existence of a file using a URL link

In PHP there is a function " fopen", which can be used to open the specified URL.

What are we doing? We try to open the file, and if we succeed, then the file exists, otherwise, the file does not exist.

Implementation:

But what if we have not one file, but several, so to speak, an array of links? This is exactly the task that stood before me from the very beginning. And the solution to this problem is as follows:

In this case, we get a list of only those files that exist.

Checking the existence of a local file

The word “local” means that the script and files for verification are located on the same server. If you have a fairly large array of links, this option is the best for solving the problem, since we are not making a request to a third-party server, but scanning the specified directories.

This method uses the “file_exists” function, and, by analogy with the previous option, simply replaces part of the script:

And the same for the link array:

What's it worth note? The fact that this method is convenient for running files located within our file system. Therefore, it is advisable to indicate all links as relative ones.

By the way, when making one of the orders, it was with this method that I was able to scan about 135,000 files in just a couple of seconds.

The widespread use of databases has not made the conventional file system irrelevant. Writing and reading files still occupy a significant place in programming.

Algorithms for checking the presence of a file allow you to avoid errors when executing code. The PHP file_exists function offers a simple solution for checking the existence of a file or directory.

Syntax and usage of the file_exists function

The result of the function is true or false. The only parameter is the file name and path to it. The function result is cached because if PHP file_exists does not work, but the file actually exists, then this is an algorithm error.

By using the clearstatcache() function, you can avoid many pitfalls in examining the state of an accessible file system. But be aware that on a non-existent file, PHP file_exists will return false until the file you are looking for is created, and then will return true even when it has already been deleted.

The correct combination of the clearstatcache() function and file system-related functions (for example, is_writable(), is_readable(), is_executable(), is_file(), is_dir() and others) allows you to avoid “hidden” script execution errors.

Caching significantly improves system performance, but in some cases important files can create a truly unreliable result and cause a serious, hard-to-detect runtime error.

PHP function parameter file_exists

PHP can be installed on different computing platforms, and therefore the path and file naming may be different.

The documentation states that PHP checks based on UID/GID rather than effective identifiers. Developing an algorithm using PHP file_exists, you should pay attention not only to the correct slashes (forward or backward), the encoding of the path to the file and the name of the file itself, but also check for the presence of the required case, correct characters, access rights and other circumstances.

A negative result may be affected by the encoding of the script file and may require conversion of the character string retrieved from the database.

Using the function in practice

Areas of use PHP scripts differ significantly. This is not to say that PHP file_exists is used solely for storage system information, data files, objects or dynamically generated images.

There are frequent cases of using streaming generation of large volumes of temporary information that are not effectively placed in a database immediately. Information from different visitors can flow to the site, and only after preliminary processing for a certain period of time necessary information must be placed in database tables.

Reading system files may cause caching due to multiple page refreshes or incorrect visitor actions. There are quite a lot of situations in reality, but correct use functions it allows you to write safe and reliable code.

There are times when you need to check whether a specified file exists or not, for example, in order to subsequently perform some actions on the file.

I also encountered this issue when developing the module. And I found two options for solving the problem.

Checking the existence of a file using a URL link

In PHP there is a function " fopen", which can be used to open the specified URL.

What are we doing? We try to open the file, and if we succeed, then the file exists, otherwise, the file does not exist.

Implementation:

But what if we have not one file, but several, so to speak, an array of links? This is exactly the task that stood before me from the very beginning. And the solution to this problem is as follows:

In this case, we get a list of only those files that exist.

Checking the existence of a local file

The word “local” means that the script and files for verification are located on the same server. If you have a fairly large array of links, this option is the best for solving the problem, since we are not making a request to a third-party server, but scanning the specified directories.

This method uses the “file_exists” function, and, by analogy with the previous option, simply replaces part of the script:

And the same for the link array:

What's it worth note? The fact that this method is convenient for running files located within our file system. Therefore, it is advisable to indicate all links as relative ones.

By the way, when making one of the orders, it was with this method that I was able to scan about 135,000 files in just a couple of seconds.

The required parameter for this function is pathname, which specifies the path to the directory to be created.

mkdir( "newfolder" );

If you specify the folder in this way, it will be created in the same directory from which the PHP script was launched. If you need to create the directory in a different location, you can specify relative path to the folder to be created or specify the full path from the site root directory.

mkdir( "../newfolder" ); // one level down

mkdir("/folder1/folder2/newfolder" ); // full path

In the last example prerequisite there will be the existence of subdirectories "folder1" and "folder2". If they are not there, the function in this form will not be able to create the folder and will return an error:

Warning: mkdir() : No such file or directory in …

If successful, the function returns True. If the pack was not created, False is returned.

if (mkdir("newfolder"))
echo "Folder created successfully";
else
echo "Folder not created";

But you should not use this function without checking for the presence of a folder, since the server will still display an error that the folder could not be created.

Assigning rights when creating a folder

For assigning rights to the folder being created corresponds to the second optional parameter of the mkdir function. By default, the maximum privileges are assigned – 0777.

Permissions are assigned using an octal value with a mandatory leading zero. Apart from the first zero, the numbers represent access levels for the owner, for the owner's group, for everyone else.

0 – access denied;

1 – read access;

2 – write access;

4 – execution access.

Most often, rights are specified as a composite amount, for example:

7 – full access (1+2+4);

5 – reading and execution (1+4).

mkdir( "newfolder" , 0777); // full access for everyone

Creating multiple nested subdirectories

You can create several subfolders at once by simply specifying another optional Boolean parameter – recursive.

mkdir("folder1/folder2/newfolder" , 0777, True ); // full access for everyone

In this case, if there are no folders "folder1" and "folder2", the function will create both them and the folder "newfolder". If no other problems arise, no error messages will be displayed and the function will return True.

Deleting a folder

An empty folder in PHP can be deleted using the rmdir function. The dirname parameter also specifies the full or relative path to the directory to be deleted:

rmdir( "myfolder" );

rmdir("folder1/folder2/myfolder" );

In each of these cases, only the "myfolder" folder is deleted. If there is no folder or the path is specified incorrectly, an error will be displayed:

Warning: rmdir(myfolder) : No such file or directory in …

Deleting a non-empty folder

Deleting a non-empty directory is done by sequentially deleting the subfiles in the folder with the unlink function, and then deleting empty folder function rmdir. You can use a function like this to do this:

function my_delete_dir($mypath)(
$dir = opendir($mypath);
while (($file = readdir($dir)))(
if (is_file($mypath."/" .$file))
unlink($mypath. "/" .$file);
elseif (is_dir($mypath."/" .$file) && ($file != "." ) && ($file != ".." ))
my_delete_dir($mypath."/" .$file);
}
closedir($dir);
rmdir($mypath);
}

my_delete_dir("myfolder" ); // function call

Checking the existence of a directory

Before most operations with directories, it is worth checking whether they exist. The file_exists function is used for this.

In addition, you need to make sure that the specified object is a folder and not a file - the is_dir function. The folder to be scanned is specified by a relative or full path.

if (file_exists("myfolder"))
echo "The specified folder exists";
else
echo "The specified folder does not exist";

if (is_dir("myfolder"))
echo "Specified object folder";
else
echo "The specified object is not a folder";