:::

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%3Ftbsn%3D53%26tbdsn%3D1874

計數器

今天: 578578578
昨天: 2455245524552455
總計: 6115275611527561152756115275611527561152756115275