SQLを使用してフィアルアップロードができる掲示板を作成しています。
掲示板自体は別のページに飛ばしています。
現在、こちらのページを参考に作成しています。リンク内容
画像投稿とコメント、投稿者を同時にアップロードすることはできました。(下記のプログラムとは別です。)
動画をアップロードするにはどのようにすれば良いのでしょうか。
また、コメントのみの投稿ができるようにするにはどうすれば良いのでしょうか。
<?php session_start(); if (!isset($_SESSION["ACCOUNT"])) { header("Location: logout.php"); exit; } function h($str) { return htmlspecialchars($str, ENT_QUOTES, 'UTF-8'); } header('Content-Type: text/html; charset=utf-8'); $dbh->beginTransaction(); try { // データベースに接続 $pdo = new PDO( 'mysql:host=localhost;dbname=データベース名;charset=utf8','root','root',[PDO::ATTR_EMULATE_PREPARES => false,PDO::ATTR_ERRMODE => PDO::ERRMODE_EXCEPTION,PDO::ATTR_DEFAULT_FETCH_MODE => PDO::FETCH_ASSOC,]); $file_nm = $_FILES['upfile']['name']; $tmp_ary = explode('.', $file_nm); $extension = $tmp_ary[count($tmp_ary)-1]; echo($extension);//ここで拡張子を表示している /* ファイルアップロードがあったとき */ if(isset($_FILES['upfile']['error']) && is_int($_FILES['upfile']['error'])) { // バッファリングを開始 ob_start(); //トランザクション処理を開始 try { if ($extension == 'jpg' || $extesion == 'JPG') { //jpg JPG の時 // サムネイルをバッファに出力 $create = str_replace('/', 'createfrom', $info['mime']); $output = str_replace('/', '', $info['mime']); if ($info[0] >= $info[1]) { $dst_w = 120; $dst_h = ceil(120 * $info[1] / max($info[0], 1)); } else { $dst_w = ceil(120 * $info[0] / max($info[1], 1)); $dst_h = 120; } if (!$src = @$create($_FILES['upfile']['tmp_name'])) { throw new RuntimeException('画像リソースの生成に失敗しました', 500); } $dst = imagecreatetruecolor($dst_w, $dst_h); imagecopyresampled($dst, $src, 0, 0, 0, 0, $dst_w, $dst_h, $info[0], $info[1]); $output($dst); imagedestroy($src); imagedestroy($dst); $stmt = $pdo->prepare('INSERT INTO image(name,type,raw_data,thumb_data,date,account) VALUES(?,?,?,?,?,?)'); //コミットする前 $stmt->execute([$_FILES['upfile']['name'],$info[2],file_get_contents($_FILES['upfile']['tmp_name']),ob_get_clean(),(new DateTime('now', new DateTimeZone('Asia/Tokyo')))->format('Y-m-d H:i:s'),$_POST['account'],]); } else if($extension == 'mov'){//動画投稿があった時 $stmt = $pdo->prepare('INSERT INTO image(name,type,raw_data,thumb_data,date) VALUES(?,?,?,?,?)'); //コミットする前 $stmt->execute([$_FILES['upfile']['name'],$info[2],file_get_contents($_FILES['upfile']['tmp_name']),ob_get_clean(),(new DateTime('now', new DateTimeZone('Asia/Tokyo')))->format('Y-m-d H:i:s'),$_POST['account'],]); } $msgs[] = ['green', '投稿は正常にアップロードされました']; } catch (RuntimeException $e) { while (ob_get_level()) { ob_end_clean(); // バッファをクリア } http_response_code($e instanceof PDOException ? 500 : $e->getCode()); $msgs[] = ['red', $e->getMessage()]; } } /* ID指定があったとき */ elseif (isset($_GET['id'])) { try { $stmt = $pdo->prepare('SELECT type, raw_data FROM image WHERE id = ? LIMIT 1'); $stmt->bindValue(1, $_GET['id'], PDO::PARAM_INT); $stmt->execute(); if (!$row = $stmt->fetch()) { throw new RuntimeException('該当する画像・動画は存在しません', 404); } header('X-Content-Type-Options: nosniff'); header('Content-Type: ' . image_type_to_mime_type($row['type'])); echo $row['raw_data']; exit; } catch (RuntimeException $e) { http_response_code($e instanceof PDOException ? 500 : $e->getCode()); $msgs[] = ['red', $e->getMessage()]; } } else if(isset($_FILES['upfile']['error']) && is_int($_FILES['upfile']['error'])){//ファイルアップロードがない場合 $stmt = $pdo->prepare('INSERT INTO image(account,comment) VALUES(?,?)'); //コミットする前 $stmt->execute($_POST['account'],$_POST['comment']); } $dbh->commit(); } catch (PDOException $e) { $dbh->rollBack(); http_response_code(500); $msgs[] = ['red', $e->getMessage()]; } ?> <!DOCTYPE html> <html> <head> <title>ファイルアップロード</title> <style><![CDATA[ fieldset { margin: 10px; } legend { font-size: 12pt; } img { border: none; float: left; } ]]></style> </head> <body> <form enctype="multipart/form-data" method="post" action=""> <fieldset> <legend>投稿フォーム</legend> 名前:<br /> <?php $user = htmlspecialchars($_SESSION["ACCOUNT"], ENT_QUOTES)?> <input type="text" name="account" size="30" value="<?php echo (htmlspecialchars($_SESSION["ACCOUNT"], ENT_QUOTES)); ?>" /><br /> コメント:<br /> <textarea name="comment" cols="30" rows="5"></textarea><br /> <legend>ファイルを選択</legend> <input type="file" name="upfile" /><br /> <input type="submit" value="送信" /> </fieldset> </form> <?php if (!empty($msgs)): ?> <fieldset> <legend>メッセージ</legend> <?php foreach ($msgs as $msg): ?> <ul> <li style="color:<?=h($msg[0])?>;"><?=h($msg[1])?></li> <li> <input type="button" onclick="location.href='new_keijiban.php'"value="掲示板へ"></li> </ul> <?php endforeach; ?> </fieldset> <?php endif; ?> </body> </html>
回答2件
あなたの回答
tips
プレビュー
バッドをするには、ログインかつ
こちらの条件を満たす必要があります。
2017/03/28 13:56
2017/03/28 15:49
2017/04/01 13:52