回答です
This commit is contained in:
62
answer/Ex06_image_display.php
Normal file
62
answer/Ex06_image_display.php
Normal file
@@ -0,0 +1,62 @@
|
||||
<?php
|
||||
// Ex07.php
|
||||
|
||||
$dsn = "mysql:host=localhost;dbname=employee_db;charset=utf8";
|
||||
$username = "rezouser";
|
||||
$password = "Rezo_0000"; // 各環境に合わせて設定してください
|
||||
|
||||
try {
|
||||
if (!isset($_GET['id'])) {
|
||||
throw new Exception("画像IDが指定されていません。");
|
||||
}
|
||||
|
||||
$imageId = (int)$_GET['id'];
|
||||
|
||||
$pdo = new PDO($dsn, $username, $password);
|
||||
$pdo->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION);
|
||||
|
||||
$sql = "SELECT filename, mime, image FROM images WHERE id = :id";
|
||||
$stmt = $pdo->prepare($sql);
|
||||
$stmt->bindParam(':id', $imageId, PDO::PARAM_INT);
|
||||
$stmt->execute();
|
||||
|
||||
// bindColumn() を使用して各カラムの値を変数にバインド
|
||||
$stmt->bindColumn('filename', $fetchedFilename);
|
||||
$stmt->bindColumn('mime', $fetchedMime);
|
||||
$stmt->bindColumn('image', $fetchedImage, PDO::PARAM_LOB);
|
||||
|
||||
if (!$stmt->fetch(PDO::FETCH_BOUND)) {
|
||||
throw new Exception("画像情報が取得できませんでした。");
|
||||
}
|
||||
|
||||
if (is_resource($fetchedImage)) {
|
||||
$fetchedImage = stream_get_contents($fetchedImage);
|
||||
}
|
||||
$base64Image = base64_encode($fetchedImage);
|
||||
} catch (Exception $e) {
|
||||
echo "エラー: " . $e->getMessage();
|
||||
exit;
|
||||
} finally {
|
||||
if (isset($pdo)) {
|
||||
$pdo = null;
|
||||
}
|
||||
}
|
||||
?>
|
||||
<!DOCTYPE html>
|
||||
<html>
|
||||
|
||||
<head>
|
||||
<meta charset="UTF-8">
|
||||
<title>登録された画像の表示</title>
|
||||
<link href="common/css/bootstrap.min.css" rel="stylesheet">
|
||||
</head>
|
||||
|
||||
<body>
|
||||
<div class="container">
|
||||
<h1><?php echo htmlspecialchars($fetchedFilename); ?></h1>
|
||||
<img src="data:<?php echo htmlspecialchars($fetchedMime); ?>;base64,<?php echo $base64Image; ?>"
|
||||
alt="<?php echo htmlspecialchars($fetchedFilename); ?>" class="img-responsive" />
|
||||
</div>
|
||||
</body>
|
||||
|
||||
</html>
|
||||
Reference in New Issue
Block a user