Assuming you have a folder with A LOT of files reading them all and then deleting in two steps is not that performing. I believe the most performing way to delete files is to just use a system command.
See readdir and unlink. Posted a general purpose file and folder handling class for copy, move, delete, calculate size, etc. For me, the solution with readdir was best and worked like a charm. With glob , the function was failing with some scenarios. I've built a really simple package called "Pusheh". Using it, you can clear a directory or remove a directory completely Github link. It's available on Packagist , also. If you're interested, see the wiki. Stack Overflow for Teams — Collaborate and share knowledge with a private group.
Create a free Team What is Teams? Collectives on Stack Overflow. Learn more. Deleting all files from a folder using PHP? Ask Question. Asked 11 years ago. Active 1 month ago. Viewed k times. Improve this question. It's a good thing this question was answered down below before it was marked as duplicate. The answers below are way better than the linked answered question.
Plus the question is different, this question asks to empty a directory, not delete. Yeah, this is a different question that drew different answers. It should not be marked as a duplicate.
Add a comment. Active Oldest Votes. Improve this answer. Daniel Ruf 7, 10 10 gold badges 56 56 silver badges bronze badges. Tags: File or Directory. February 29, October 20, December 10, February 24, April 6, January 3, Is it possible to make it delete contents that are older than X time! Thank you. Delete files older than x days or after x amount of time in PHP. Our process goes like this - you have to loop through the files one by one from a given folder, check if it is a file and delete file with the help of unlink function.
Here is the simple php function I have written to delete all files from a directory. Once we confirm it as a file we delete it with unlink method. The process will be repeated until no file is left in the specific folder. Say you don't want to remove all files, just the pdf files alone from a folder.
Then here's the script for the task. This is another useful variation of the script. Suppose you want to delete older files say of at least 90 days old. You have to simply check if the last modified date and time of the file is greater than 90 days and delete if it is.
To check if files are older than 90 days, we have used filemtime to get last modified timestamp of a given file, took its difference from current timestamp and checked it against 90 days count.
0コメント