:::

7-7 讓修改時可以加入圖片

  1. 由於新增、修改,都會用到圖片上傳,所以,我們把圖片上傳部份獨立出一個函式,如 uploads() ,該函式最重要的除了上傳檔案外,還要傳回所有檔案的 json 內容:
    // 處理多檔案上傳
    function uploads($id)
    {
        global $uploadDir;
        if (!empty($_FILES['media'])) {
    
            // 建立存放圖檔的目錄
            $articleDir = $uploadDir . $id . '/';
            if (!file_exists($articleDir)) {
                mkdir($articleDir, 0777, true);
            }
    
            $fileData = array();
    
            foreach ($_FILES['media']['name'] as $index => $fileName) {
                $fileTmpName = $_FILES['media']['tmp_name'][$index];
                $fileDestination = $articleDir . $fileName;
    
                // 將檔案移動到文章檔案目錄
                move_uploaded_file($fileTmpName, $fileDestination);
    
                // 將檔案路徑存入陣列
                $fileData[$fileName] = $fileDestination;
            }
    
            // 取出原有的檔案資料陣列
            $news = show($id);
            $allFiles = $fileData ? array_merge($news['files'], $fileData) : $news['files'];
    
            // 將檔案相關資訊轉換為JSON格式
            $media = !empty($allFiles) ? json_encode($allFiles, 256) : '[]';
            return $media;
        }
    }

     

    1. 此外,我們也要把原有的檔案讀出,然後再用 array_merge() 將兩個陣列結合起來,變成新的 $allFiles 檔案陣列,再做成新的 json 以加入檔案紀錄。

    2. 若是沒有新的檔案,則維持原有的 $news['files'] 內容

  2. 新增儲存的部份,只要直接呼叫之,並接收 uploads() 傳回檔案的 json 內容即可
    // 處理表單提交
    function store()
    {
        global $pdo, $uploadDir;
        if ($_SERVER['REQUEST_METHOD'] === 'POST') {
            // 取得表單的值
            ...略...
    
            try {
                // 準備SQL語句
                ...略...
    
                if ($rowCount > 0) {
                    // 取得插入的 id 值
                    $lastInsertId = $pdo->lastInsertId();
    
                    // 處理多檔案上傳
                    $media = uploads($lastInsertId);
    
                    // 更新該文章的 media 欄位值
                    if ($media) {
                        $updateSql = "UPDATE news SET media = ? WHERE id = ?";
                        $updateStmt = $pdo->prepare($updateSql);
                        $updateStmt->execute([$media, $lastInsertId]);
                    }
    
                    return $lastInsertId;
                } else {
                    echo "資料插入失敗!";
                }
            } catch (PDOException $e) {
                echo "資料庫連接錯誤:" . $e->getMessage();
            }
        }
    }

     

  3. 接著,一樣修改 update() 更新部份,得讓新圖檔可以加進去才行:
    // 更新文章
    function update($id)
    {
        global $pdo;
    
        if ($_SERVER['REQUEST_METHOD'] === 'POST') {
            // 取得表單值
            $title = $_POST['title'];
            $content = $_POST['content'];
            $author = $_POST['author'];
            $date = $_POST['date'];
            $category = $_POST['category'];
            $media = uploads($id);
    
            try {
                // 準備SQL語句
                $sql = "UPDATE `news` SET `title` = ?, `content` = ?, `author` = ?, `date` = ?, `category` = ?, `media` = ? WHERE `id` = ?";
    
                // 預備語句
                $stmt = $pdo->prepare($sql);
    
                // 綁定參數並執行語句
                $stmt->execute([$title, $content, $author, $date, $category, $media, $id]);
            } catch (PDOException $e) {
                echo "更新失敗:" . $e->getMessage();
            }
        }
        return $id;
    }

     


:::

書籍目錄

展開 | 闔起

https%3A%2F%2Fcampus-xoops.tn.edu.tw%2Fmodules%2Ftad_book3%2Fpage.php%3Ftbsn%3D52%26tbdsn%3D1834

計數器

今天: 4385438543854385
昨天: 3438343834383438
總計: 7394714739471473947147394714739471473947147394714