Xoá ảnh webp trên hosting bằng code PHP

Đuôi file .webp

Bạn hãy tạo 1 file có tên là delete-webp.php nằm tại thư mục gốc chứa website

Sau đó mở file delete-webp.php lên vào dán code dưới đây vào.

<?php
// Target folder
$directory = __DIR__ . '/wp-content/uploads';

// Create a RecursiveIterator to recursively go through all folders
$iterator = new RecursiveDirectoryIterator($directory);

// Make the iterator recursive
$recursiveIterator = new RecursiveIteratorIterator($iterator);

// Loop through all files in the directory and its subdirectories
foreach ($recursiveIterator as $file) {
    // Check if the file is a .webp file
    if (strtolower(pathinfo($file, PATHINFO_EXTENSION)) === 'webp') {
        // Attempt to delete the file and check the result
        if (unlink($file)) {
            echo "Deleted: " . $file . PHP_EOL;
        } else {
            echo "Failed to delete: " . $file . PHP_EOL;
        }
    }
}
?>

Sau đó lưu lại và truy cập vào website theo link sau: https://your-domain.com/delete-webp.php

Đuôi file .bk.jpg

Bonus cho bạn thêm code xoá ảnh do 1 ông nào đó tối ưu web kiểu gì đó mà tạo ra loại cấu trúc ảnh như sau:

ten-hinh-anh-so-1.bk.jpg ten-hinh-anh-so-2.bk.jpg

mình tiếp nhận 1 web và tổng tên ảnh họ thay tên như này hơn 3.5GB (bạn sợ chưa)

Nếu bạn xui gặp ca này thì có thể dùng code dưới đây xử tử nó luôn cho nhẹ host.

Tạo file delete-bk-images.php ở thư mục gốc và cho code này vào

<?php
// Target folder
$directory = __DIR__ . '/wp-content/uploads';

// Create a RecursiveIterator to recursively go through all folders
$iterator = new RecursiveDirectoryIterator($directory);

// Make the iterator recursive
$recursiveIterator = new RecursiveIteratorIterator($iterator);

// Loop through all files in the directory and its subdirectories
foreach ($recursiveIterator as $file) {
    // Check if the file ends with .bk.jpg
    if (preg_match('/\.bk\.jpg$/i', $file)) {
        // Attempt to delete the file and check the result
        if (unlink($file)) {
            echo "Deleted: " . $file . PHP_EOL;
        } else {
            echo "Failed to delete: " . $file . PHP_EOL;
        }
    }
}
?>

Sau đó lưu lại và truy cập vào website theo link sau: https://your-domain.com/delete-bk-images.php

Last updated