:::

4. function.php 共同函式檔

<?php
// 顯示一篇文章
function show($id)
{
    global $db;

    // 讀取資料庫
    $sql = "SELECT * FROM `articles` WHERE `id`= '$id' ";
    $sth = $db->prepare($sql);
    $sth->execute();
    $news          = $sth->fetch(PDO::FETCH_ASSOC);
    $news['files'] = get_files($id);

    return $news;
}

// 取得圖片
function get_files($id)
{
    // 取得相關圖檔或影片
    $dir = _PATH . "/uploads/{$id}/";

    // Open a known directory, and proceed to read its contents
    $files = [];
    if (is_dir($dir)) {
        if ($dh = opendir($dir)) {
            while (($file = readdir($dh)) !== false) {
                if (filetype($dir . $file) != 'dir') {
                    // 抓取副檔名
                    $ext          = pathinfo($file, PATHINFO_EXTENSION);
                    $files[$file] = ($ext == 'mp4') ? str_replace('.mp4', '.jpg', $file) : $file;
                }
            }
            closedir($dh);
        }
    }
    return $files;
}

 


:::

書籍目錄

展開 | 闔起

快速登入


http%3A%2F%2Fcampus-xoops.tn.edu.tw%2Fmodules%2Ftad_book3%2Fpage.php%3Ftbdsn%3D1874%26tbsn%3D53

計數器

今天: 5876587658765876
昨天: 8625862586258625
總計: 8036483803648380364838036483803648380364838036483