Php is there a file. Syntax and usage of the file_exists function. Deleting a non-empty 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.

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 mkdir functions. By default, the maximum privileges are assigned – 0777.

Permissions are assigned as 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

You can delete an empty folder in PHP function rmdir. 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

Removing a non-empty directory is done by sequentially removing attached files in folder 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. For this purpose it is used file_exists function.

In addition, you need to make sure that the specified object is a folder and not a file - 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";