admin管理员组文章数量:1122832
I'm writing a maintenance script for my own sites and one part is to clear out all .webp files, since my host now handles WebP conversion on the fly. I could easily traverse through all the sub-folders in the /uploads/ directory, however there's usually other folders in there created by plugins. Is there a way to get a list of only the year/month folders that WordPress created?
I know I can just validate the folder names to check if they're a 4 digit year, I'd just rather do this the "proper" way if it's available.
I'm writing a maintenance script for my own sites and one part is to clear out all .webp files, since my host now handles WebP conversion on the fly. I could easily traverse through all the sub-folders in the /uploads/ directory, however there's usually other folders in there created by plugins. Is there a way to get a list of only the year/month folders that WordPress created?
I know I can just validate the folder names to check if they're a 4 digit year, I'd just rather do this the "proper" way if it's available.
Share Improve this question asked Apr 2, 2024 at 10:13 GavinGavin 4147 silver badges21 bronze badges1 Answer
Reset to default 0You can use this WP function wp_get_upload_dir
to know the uploads directory path.
Please note different website can choose not to use "year/month" fashion folder structure so using this function we will be advisable.
And then for finding all webp file you can use FileSearchIterator
of PHP
$fileScanner = new FileSearchIterator( UPLOAD_BASE_PATH, true,
array('webp'));
foreach($fileScanner as $file)
{
unlink(UPLOAD_BASE_PATH.$file);
}
Note: Code is not tested but hope this should work
本文标签: Get all WordPresscreated sub folders in uploads directory
版权声明:本文标题:Get all WordPress-created sub folders in uploads directory 内容由网友自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:http://www.betaflare.com/web/1736312023a1934951.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。
发表评论