首頁
編程語言
C語言|JAVA編程
Python編程
網頁編程
ASP編程|PHP編程
JSP編程
數據庫
MYSQL數據庫|SqlServer數據庫
Oracle數據庫|DB2數據庫
您现在的位置: 程式碼之家 >> 編程語言 >  >> 網頁程式設計 >> PHP 程式設計 >> PHP 相關知識

PHP 文章編輯時自動清理被刪除的圖片文件

     編輯:PHP 相關知識

業務場景

使用富文本編輯器(如 SinaEditor)發布文章時,圖片會自動上傳至服務器。用戶編輯文章刪除某些圖片後,數據庫中的 `` 標籤雖被移除,但服務器上的實體圖片文件仍殘留,佔用空間且難以維護。需在文章更新時,比對新舊內容,刪除不再引用的圖片。

核心思路

  1. 從數據庫讀取舊文章內容,提取所有圖片文件名 → $oldImgs
  2. 從用戶提交的新內容提取圖片文件名 → $newImgs
  3. 計算差集 $deleted = array_diff($oldImgs, $newImgs)
  4. 遍歷 $deleted 刪除物理文件

關鍵代碼實現

1. 從 HTML 內容提取圖片文件名

/**
 * 從文章 HTML 中提取圖片文件名數組
 * 假設圖片 URL 格式:https://domain/.../201111291322589013.jpg
 */
function getImgsInArticle(string $content): array|false {
    $matches = [];
    // 匹配 http 開頭,以 jpg/gif/png 結尾的 URL
    preg_match_all('/https?:\/\/[^\s\'"]+?\.(?:jpg|gif|png)/i', $content, $matches);
    $urls = $matches[0] ?? [];
    if (empty($urls)) return false;

    $filenames = [];
    foreach ($urls as $url) {
        $filenames[] = basename($url); // 取文件名
    }
    return $filenames;
}

2. 計算差集並刪除文件

$oldContent = $oldArticle['content']; // 舊文章 HTML
$newContent = $_POST['content'];      // 新提交 HTML

$oldImgs = getImgsInArticle($oldContent) ?? [];
$newImgs = getImgsInArticle($newContent) ?? [];

$deleted = array_diff($oldImgs, $newImgs);
$deleted = array_values($deleted); // 重建索引

foreach ($deleted as $filename) {
    $filePath = ARTICLE_IMG_DIR . '/' . $filename;
    if (is_file($filePath)) {
        @unlink($filePath); // 靜默刪除,避免權限錯誤中斷流程
    }
}

3. 刪除文章時同步清理圖片

function deleteArticle(int $articleId): bool {
    // 1. 讀取文章內容
    $article = $db->get("SELECT content FROM articles WHERE id = ?", $articleId);
    if (!$article) return false;

    // 2. 提取並刪除圖片
    $imgs = getImgsInArticle($article['content']) ?? [];
    foreach ($imgs as $img) {
        @unlink(ARTICLE_IMG_DIR . '/' . $img);
    }

    // 3. 刪除數據庫記錄
    return $db->execute("DELETE FROM articles WHERE id = ?", $articleId);
}

正則表達式優化建議

原文使用 `/http[^d]*[d]+[.](jpg|gif|png)/` 存在以下問題:

  • `[^d]` 匹配非字母 'd',非預期行為
  • 未支援 https
  • 未處理 URL 中可能包含的查詢參數(如 `?v=1`)

推薦使用:

'/https?:\/\/[^\s\'"]+?\.(?:jpg|jpeg|gif|png|webp)(?:\?[^\s\'"]*)?/i'

可正確匹配帶參數的圖片 URL,並支援更多格式。

目錄路徑處理技巧

// 獲取項目根目錄下的 uploads/article 絕對路徑
define('ARTICLE_IMG_DIR', __DIR__ . '/../uploads/article');

// 或動態構建
$baseDir = realpath(__DIR__ . '/../../public/uploads/article');

總結

通過「提取文件名 → 計算差集 → 物理刪除」三步,可輕鬆實現文章編輯/刪除時的圖片自動清理,避免垃圾文件堆積。關鍵在於正則的準確性與路徑的正確拼接。

  1. 上一篇文章:
  2. 下一篇文章:
Copyright © 程式碼之家 All Rights Reserved