Deleting files with PHP's unlink function

Table of contents:

Deleting files with PHP's unlink function
Deleting files with PHP's unlink function
Anonim

In the operation of a web resource, it is convenient to use temporary files to store data during processing. Once the work is completed, there is no need to store such files. The site may provide the visitor with the ability to download files. The data may be uploaded by mistake or sent and deleted from the hosting after uploading. In these and many other cases, PHP offers the unlink() function to remove information from the file system provided by the host for the web resource.

Hosting terms and filenames

Creating a hosted file may execute PHP code due to the need for permanent or temporary data storage. Files can be downloaded through the pages of the site. In any case, the name of the file and the path to it matter.

As a rule, data is placed in specially allocated folders and cannot be placed at the site code location. Before deleting a file, it must be checked what exactly will be deleted.

Hostings, as usual, are Apache or Windows servers. For safe and stable operation of a web resource, it is advisable not only to control, but also to form the name of each file. Trust the visitor to download files with arbitrarynames are not allowed.

Unique file name
Unique file name

To preserve the original name, you can create a special name matching table. Each uploaded file gets a new name, but the exact original name is kept. When creating a file, the site "chooses" the name of the file. Under these conditions, using PHP's unlink() function is safe and secure.

Delete file

The file system provided to the web resource starts at position $_SERVER['DOCUMENT_ROOT']. Each folder, starting from the root of the site, has its own access rights. It is not customary to install blocking and protection that harm the site.

If any operation is prohibited, it will simply not be executed. The site has worked and will continue to work.

It is not necessary to check the actual existence of the file before executing the PHP unlink() function. If something is wrong, an E_WARNING level error is generated. By escaping the function call with the "@" symbol, you can ignore this error. If you want to write professional code, you should check the path, file name and its actual presence before deleting.

Practical example
Practical example

Situations where PHP's unlink function doesn't work are hard to imagine. The only important thing to consider is the extremely close attitude of any hosting to the case of letters and Cyrillic.

To avoid problems, both filenames and file deletion should be kept under responsible control.

Popular topic

Editor's choice

  • How to block a person in "Contact"? Protect your page from ill-wishers
    How to block a person in "Contact"? Protect your page from ill-wishers

    The popularity of social networks is increasing every day. This is due to the fact that such an instrument of mass communication has entered everyday life and firmly established itself in it, as one of the most convenient ways of communication. Almost every advanced person has his own page on the social network, where he posts his photos, posts posts and, most importantly, communicates with friends and makes new acquaintances

  • Why doesn't "VK" work? The VKontakte website is not working: what to do?
    Why doesn't "VK" work? The VKontakte website is not working: what to do?

    Social media addiction is considered one of the most serious cyber diseases. Every day, tens of millions of users around the globe visit their personal accounts, chatting with friends, discussing the latest events in the world. Social networks have become such a dense part of our lives that it seems unusual to spend a day without looking at them even for a second

  • What is button accordion? Learn slang online
    What is button accordion? Learn slang online

    The Internet is a territory with its own rules, language, style of communication, and, more recently, laws. It so happened that many new users may be left at a loss after communicating with hardened Internet - "hacks". We are talking, first of all, about the use of slang by the latter in their language, which is far from accessible to everyone who opened the page of any forum for the first time

  • Hashtag - what is it? How to use hashtags
    Hashtag - what is it? How to use hashtags

    The Internet is constantly progressing, and every day new features appear in it that make working on it more convenient. They are associated, as a rule, with the optimization and structuring of information, since there is such a large amount of it on the World Wide Web that it is sometimes difficult to find the necessary material. For example, in the recent past, so-called hashtags appeared

  • Why is the internet slow? The most important reasons
    Why is the internet slow? The most important reasons

    In the last couple of years, the Internet has become an important part of the life of almost every person. Using the World Wide Web, we can find out the weather, the latest world news, download the necessary programs, play online games, etc. When the Internet is working well, doing the above things is very nice, but what if all of a sudden web pages start to load for a very long time, and online games slow down?