<?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;
}