How To Delete File From Folder And Database In PHP

Hello Programmer!

When we perform CRUD operation in PHP, sometimes we have to play with files such as images, pdfs, etc...


delete-file-from-folder-in-php


Many programmers know how to upload files in PHP and they can easily store the record in the database and move files in the folder.

But when it comes to deleting files from a folder it becomes challenging.

Deleting files from the database is easy because all you have to do is pass the query. The query will erase a record from the database but the file still exists in your folder and require space.

But don't worry removing files from your folder is very easy.

There is an inbuilt function in PHP called unlink().

UNLINK() Function In PHP

Let me explain this function shortly and sweetly.

Generally, this function is used to unlinking files from the directories.

How To Use UNLINK() Function

To use the unlink() function you have to pass the image path in this function.

Suppose your image on the D:/image/1.jpg path.

And you want to remove this file with PHP code.

In this case, you should use the unlink() function.

=> unlink('d:/image/1.jpg'); 

As defined above will remove your file from the folder. And remove the file from the database you can pass the query.

This is a static image path but you can pass variable names to remove specific files

So, that's all! unlink() function will help you to remove files from folders/directories.

Conclusion

So, In this article, we saw how to delete or remove files from folder or directories with PHP code.

Removing files from directors is very simple you just have to use the inbuilt function in PHP.

Thank you for landing on this article, I hope you get fruitful information!







Post a Comment

Previous Post Next Post