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

PHP 隨機顯示圖片函數實作:指定資料夾輪播展示

     編輯:PHP 相關知識

本文提供一個實用的 PHP 函數,從指定資料夾隨機挑選一張圖片並直接輸出圖片二進位內容,適用於部落格側邊欄隨機照片、廣告輪播、驗證碼圖片等場景。

完整代碼 (img.php)

<?php
/**
 * 隨機顯示圖片
 * 用法:
 *   <img src="img.php">
 *   <img src="img.php?folder=images2/">
 */
function showRandomImage() {
    // 1. 取得資料夾參數,預設為 /images/
    $folder = isset($_GET['folder']) ? $_GET['folder'] : '/images/';
    
    // 安全性:防止目錄遍歷攻擊
    $folder = str_replace(['../', '..\\'], '', $folder);
    $folder = trim($folder, '/\\');
    if ($folder === '') $folder = 'images';

    // 2. 組合實體路徑
    $basePath = $_SERVER['DOCUMENT_ROOT'] . '/' . $folder;
    if (!is_dir($basePath)) {
        header('HTTP/1.1 404 Not Found');
        exit('資料夾不存在');
    }

    // 3. 掃描圖片檔案
    $allowedExt = ['gif', 'jpg', 'jpeg', 'png', 'webp'];
    $files = [];
    $handle = opendir($basePath);
    if ($handle) {
        while (false !== ($file = readdir($handle))) {
            if ($file === '.' || $file === '..') continue;
            $ext = strtolower(pathinfo($file, PATHINFO_EXTENSION));
            if (in_array($ext, $allowedExt, true)) {
                $files[] = $file;
            }
        }
        closedir($handle);
    }

    if (empty($files)) {
        header('HTTP/1.1 404 Not Found');
        exit('資料夾無圖片');
    }

    // 4. 隨機選取
    $randomFile = $files[array_rand($files)];
    $filePath = $basePath . '/' . $randomFile;

    // 5. 根據副檔名輸出正確 Content-Type
    $ext = strtolower(pathinfo($randomFile, PATHINFO_EXTENSION));
    $mimeTypes = [
        'gif'  => 'image/gif',
        'jpg'  => 'image/jpeg',
        'jpeg' => 'image/jpeg',
        'png'  => 'image/png',
        'webp' => 'image/webp',
    ];
    header('Content-Type: ' . ($mimeTypes[$ext] ?? 'application/octet-stream'));
    
    // 建議加上快取標頭(視需求調整)
    header('Cache-Control: public, max-age=3600');
    header('Expires: ' . gmdate('D, d M Y H:i:s', time() + 3600) . ' GMT');

    // 6. 輸出圖片二進位內容
    readfile($filePath);
    exit;
}

showRandomImage();
?>

HTML 使用範例

<!-- 預設資料夾 /images/ -->
<img src="img.php" alt="隨機圖片">

<!-- 指定資料夾 -->
<img src="img.php?folder=uploads/banners/" alt="隨機橫幅">

<!-- 作為 CSS 背景(需搭配 JS 或接受動態變化) -->
<div style="width:300px; height:200px; background:url('img.php?folder=bg/') center/cover no-repeat;"></div>

進階強化建議

  • 效能優化:掃描目錄較耗資源,建議將檔案列表快取至 Redis/Memcached/APCu,或定時任務產生 JSON 清單供前端 JS 隨機挑選(減少 PHP 輸出圖片的開銷)。
  • 權重隨機:在檔名加入前綴如 10_pic.jpg 表示權重 10,解析前綴計算加權隨機。
  • 防盜鏈:檢查 $_SERVER['HTTP_REFERER'] 僅允許自家域名嵌入。
  • 支援子目錄:遞迴掃描 RecursiveDirectoryIterator。
  • 圖片處理:搭配 GD/Imagick 即時縮圖、加浮水印、轉 WebP。

安全性提醒

  1. 路徑遍歷防護:代碼已過濾 ../,但建議進一步限定允許的資料夾白名單陣列。
  2. MIME 偵測:readfile() 直接輸出,若上傳處理不當可能輸出惡意檔案。生產環境應驗證檔案真實類型(finfo_file() 或 getimagesize())。
  3. 錯誤處理:避免輸出 PHP 警告訊息破壞圖片二進位流,建議關閉顯示錯誤或使用 @ 抑制。
  1. 上一篇文章:
  2. 下一篇文章:
Copyright © 程式碼之家 All Rights Reserved