php,mysqlについて初心者のです。
タイトルにもある通り現在phpとmysqlを使って画像アップロードページを作成しているのですがうまくいきません。
直接mysqlにバイナリデータを登録するのはよろしくないということだったのでサーバー上にフォルダーを作りそこに画像を保存してそのパスをデータとして格納するということにしました。
現段階でDBにはパスを登録できている段階なのですがそれを次は写真アルバムみたいに一覧表示したいと考えてプログラムを書いたのですが表示されません。
このサイトをみて参考にさせて頂きました。
[PHP]ファイルアップロードサンプル(PHP → DB → HTML)
何卒よろしくお願いします。
php
1<?php 2 3require 'common.php'; 4 5 6function file_upload() 7{ 8 // POSTではないとき何もしない 9 if (filter_input(INPUT_SERVER, 'REQUEST_METHOD') !== 'POST') 10 { 11 return; 12 } 13 14 // タイトル 15 $title = filter_input(INPUT_POST, 'title'); 16 if ('' === $title) { 17 throw new Exception('タイトルは入力必須です。'); 18 } 19 20 // アップロードファイル 21 $upfile = $_FILES['upfile']; 22 23 24 if ($upfile['error'] > 0) { 25 throw new Exception('ファイルアップロードに失敗しました。'); 26 } 27 28 $tmp_name = $upfile['tmp_name']; 29 30 // ファイルタイプチェック 31 $finfo = finfo_open(FILEINFO_MIME_TYPE); 32 $mimetype = finfo_file($finfo, $tmp_name); 33 34 // 許可するMIMETYPE 35 $allowed_types = [ 36 'jpg' => 'image/jpeg' 37 , 'png' => 'image/png' 38 , 'gif' => 'image/gif' 39 ]; 40 if (!in_array($mimetype, $allowed_types)) { 41 throw new Exception('許可されていないファイルタイプです。'); 42 } 43 44 // ファイル名(ハッシュ値でファイル名を決定するため、同一ファイルは同盟で上書きされる) 45 $filename = sha1_file($tmp_name); 46 47 // 拡張子 48 $ext = array_search($mimetype, $allowed_types); 49 50 // 保存作ファイルパス 51 $destination = sprintf('%s/%s.%s' 52 , 'upfiles' 53 , $filename 54 , $ext 55 ); 56 57 echo $destination; 58 59 // アップロードディレクトリに移動 60 if (!move_uploaded_file($tmp_name, $destination)) 61 { 62 throw new Exception('ファイルの保存に失敗しました。'); 63 } 64 65 66 67 // データベースに登録 68 $sql = 'INSERT INTO `images` (`id`, `title`, `img_path`) VALUES (NULL, :title, :img_path) '; 69 $arr = []; 70 $arr[':title'] = $title; 71 $arr[':img_path'] = $destination; 72 echo $title ; 73 $lastInsertId = insert($sql, $arr); 74 75 // 成功時にページを移動する 76 header(sprintf('Location: image.php?id=%d', $lastInsertId)); 77} 78 79try { 80 // ファイルアップロード 81 file_upload(); 82} catch (Exception $e) { 83 $error = $e->getMessage(); 84} 85?> 86<!DOCTYPE HTML> 87<html lang="ja"> 88 <head> 89 <meta charset="UTF-8"> 90 <title></title> 91 <style type="text/css"> 92 .error { 93 color: red; 94 } 95 </style> 96 </head> 97 <body> 98 <div id="wrap"> 99 <?php if (isset($error)) : ?> 100 <p class="error"><?= h($error); ?></p> 101 <?php endif; ?> 102 <form action="" method="post" enctype="multipart/form-data"> 103 <p> 104 <label for="title">タイトル</label> 105 <input type="text" name="title" id="title" /> 106 </p> 107 <p> 108 <label for="upfile">画像ファイル</label> 109 <input type="file" name="upfile" id="upfile" /> 110 </p> 111 <p> 112 <button type="submit">送信</button> 113 </p> 114 </form> 115 </div> 116 </body> 117</html>
php
1<?php 2 3require 'common.php'; 4 5try 6{ 7 8 $id = filter_input(INPUT_GET, 'id'); 9 10 echo $id; 11 // データベースからレコードを取得 12 13 $sql = "SELECT id, title, img_path FROM images WHERE id = :id"; 14 $arr = []; 15 $arr[':id'] = $id; 16 $rows = select($sql, $arr); 17 $row = reset($rows); 18 19} catch (Exception $e) 20{ 21 $error = $e->getMessage(); 22} 23?> 24<!DOCTYPE HTML> 25<html lang="ja"> 26 <head> 27 <meta charset="UTF-8"> 28 <title></title> 29 <style type="text/css"> 30 .error { 31 color: red; 32 } 33 </style> 34 </head> 35 <body> 36 <div id="wrap"> 37 <?php if (isset($error)) : ?> 38 <p class="error"><?= h($error); ?></p> 39 <?php endif; ?> 40 41 <p><?= h($row['title']); ?></p> 42 <p> 43 <img src="<?= h($row['img_path']); ?>" alt="<?= h($row['title']); ?>" /> 44 </p> 45 </div> 46 </body> 47</html>
php
1<?php 2 3 4 5 6function connect_db() 7{ 8 $dsn = 'mysql:localhost=xxxx;dbname=xxxx;charset=utf8'; 9 $username = 'xxxx'; 10 $password = 'xxxx'; 11 $options = 12 [ 13 PDO::ATTR_ERRMODE => PDO::ERRMODE_EXCEPTION 14 , PDO::ATTR_DEFAULT_FETCH_MODE => PDO::FETCH_ASSOC 15 ]; 16 return new PDO($dsn, $username, $password, $options); 17} 18 19 20function insert($sql, $arr = []) 21{ 22 $pdo = connect_db(); 23 $stmt = $pdo->prepare($sql); 24 $stmt->execute($arr); 25 return $pdo->lastInsertId(); 26} 27 28 29function select($sql, $arr = []) 30{ 31 $pdo = connect_db(); 32 $stmt = $pdo->prepare($sql); 33 $stmt->execute($arr); 34 return $stmt->fetchAll(); 35} 36 37 38function h($string) 39{ 40 return htmlspecialchars($string, ENT_QUOTES, 'utf-8'); 41}

バッドをするには、ログインかつ
こちらの条件を満たす必要があります。
2018/02/07 08:01
2018/02/07 08:06
2018/02/07 09:22
2018/02/07 09:26