Delete files in PHP


On the web, it is very common to work with files that the user sends through forms. On many occasions, for our own web application to maintain the files that have been uploaded, it will be necessary delete files from PHP.

The first thing we must do is make sure that our application has permissions to be able to delete that file. You can see here how to change the permissions of a file in PHP.

Once the file in question has the necessary permissions, we can proceed to delete it from PHP. The function we will use is PHP unlink. Let's see its use.

< ? php
     // File that we are going to delete
     $ file = 'myfile.jpg';
     // We proceed to delete
     if (!unlink($ file)) {
          // Failed to delete.
          threw out 'Failed to delete';
     } else {
          // File deleted
          threw out 'File deleted';
     }
? >

With this simple script we can delete files from PHP.

Have you done something similar? Leave us a comment

Previous Change file permissions with PHP
Next Why is the Php logo an elephant?

No Comment

Leave a reply

Your email address will not be published. Required fields are marked *