ローカル環境: Vagrant
CentOS7
apache 2.4.6
php 7.1
Mysql 5.7
ディレクトリ構造
/var/www/html/upfiles/
┣ upfile/
┣ common.php
┣ image.php
┗ index.php
参考記事 https://qiita.com/ShibuyaKosuke/items/dbe45cbf43bd819bf6cc
common.php
1<?php 2 3/** 4 * common.php 5 */ 6 7/** 8 * connect_db 9 * @return \PDO 10 */ 11function connect_db() 12{ 13 $dsn = 'mysql:host=localhost;dbname=paisen1_lovecat;charset=utf8'; 14 $username = 'root'; 15 $password = 'password'; 16 $options = [ 17 PDO::ATTR_ERRMODE => PDO::ERRMODE_EXCEPTION 18 , PDO::ATTR_DEFAULT_FETCH_MODE => PDO::FETCH_ASSOC 19 ]; 20 return new PDO($dsn, $username, $password, $options); 21} 22 23/** 24 * insert 25 * @param string $sql 26 * @param array $arr 27 * @return int lastInsertId 28 */ 29function insert($sql, $arr = []) 30{ 31 $pdo = connect_db(); 32 $stmt = $pdo->prepare($sql); 33 $stmt->execute($arr); 34 return $pdo->lastInsertId(); 35} 36 37/** 38 * select 39 * @param string $sql 40 * @param array $arr 41 * @return array $rows 42 */ 43function select($sql, $arr = []) 44{ 45 $pdo = connect_db(); 46 $stmt = $pdo->prepare($sql); 47 $stmt->execute($arr); 48 return $stmt->fetchAll(); 49} 50 51/** 52 * htmlspecialchars 53 * @param string $string 54 * @return $string 55 */ 56function h($string) 57{ 58 return htmlspecialchars($string, ENT_QUOTES, 'utf-8'); 59} 60
image.php
1<?php 2/** 3 * image.php 4 */ 5require 'common.php'; 6 7try { 8 $id = filter_input(INPUT_GET, 'id'); 9 10 // データベースからレコードを取得 11 $sql = 'SELECT `id`, `title`, `path` FROM `images` WHERE `id` = :id'; 12 $arr = []; 13 $arr[':id'] = $id; 14 $rows = select($sql, $arr); 15 $row = reset($rows); 16} catch (Exception $e) { 17 $error = $e->getMessage(); 18} 19?> 20<!DOCTYPE HTML> 21<html lang="ja"> 22 <head> 23 <meta charset="UTF-8"> 24 <title></title> 25 <style type="text/css"> 26 .error { 27 color: red; 28 } 29 </style> 30 </head> 31 <body> 32 <div id="wrap"> 33 <?php if (isset($error)) : ?> 34 <p class="error"><?= h($error); ?></p> 35 <?php endif; ?> 36 37 <p><?= h($row['title']); ?></p> 38 <p> 39 <img src="<?= h($row['path']); ?>" alt="<?= h($row['title']); ?>" /> 40 </p> 41 </div> 42 </body> 43</html> 44
index.php
1<?php 2/** 3 * index.php 4 */ 5/** 6 * 共通関数読み込み 7 */ 8require 'common.php'; 9 10/** 11 * file_upload 12 */ 13function file_upload() 14{ 15 // POSTではないとき何もしない 16 if (filter_input(INPUT_SERVER, 'REQUEST_METHOD') !== 'POST') { 17 return; 18 } 19 20 // タイトル 21 $title = filter_input(INPUT_POST, 'title'); 22 if ('' === $title) { 23 throw new Exception('タイトルは入力必須です。'); 24 } 25 26 // アップロードファイル 27 $upfile = $_FILES['upfile']; 28 29 /** 30 * @see http://php.net/manual/ja/features.file-upload.post-method.php 31 */ 32 if ($upfile['error'] > 0) { 33 throw new Exception('ファイルアップロードに失敗しました。'); 34 } 35 36 $tmp_name = $upfile['tmp_name']; 37 38 // ファイルタイプチェック 39 $finfo = finfo_open(FILEINFO_MIME_TYPE); 40 $mimetype = finfo_file($finfo, $tmp_name); 41 42 // 許可するMIMETYPE 43 $allowed_types = [ 44 'jpg' => 'image/jpeg' 45 , 'png' => 'image/png' 46 , 'gif' => 'image/gif' 47 ]; 48 if (!in_array($mimetype, $allowed_types)) { 49 throw new Exception('許可されていないファイルタイプです。'); 50 } 51 52 // ファイル名(ハッシュ値でファイル名を決定するため、同一ファイルは同盟で上書きされる) 53 $filename = sha1_file($tmp_name); 54 55 // 拡張子 56 $ext = array_search($mimetype, $allowed_types); 57 58 // 保存作ファイルパス 59 $destination = sprintf('%s/%s.%s' 60 , 'upfiles' 61 , $filename 62 , $ext 63 ); 64 65 // アップロードディレクトリに移動 66 if (!move_uploaded_file($tmp_name, $destination)) { 67 throw new Exception('ファイルの保存に失敗しました。'); 68 } 69 70 // Exif 情報の削除 71 $imagick = new Imagick($destination); 72 $imagick->stripimage(); 73 $imagick->writeimage($destination); 74 75 // データベースに登録 76 $sql = 'INSERT INTO `images` (`id`, `title`, `path`) VALUES (NULL, :title, :path) '; 77 $arr = []; 78 $arr[':title'] = $title; 79 $arr[':path'] = $destination; 80 $lastInsertId = insert($sql, $arr); 81 82 // 成功時にページを移動する 83 header(sprintf('Location: image.php?id=%d', $lastInsertId)); 84} 85 86try { 87 // ファイルアップロード 88 file_upload(); 89} catch (Exception $e) { 90 $error = $e->getMessage(); 91} 92?> 93<!DOCTYPE HTML> 94<html lang="ja"> 95 <head> 96 <meta charset="UTF-8"> 97 <title></title> 98 <style type="text/css"> 99 .error { 100 color: red; 101 } 102 </style> 103 </head> 104 <body> 105 <div id="wrap"> 106 <?php if (isset($error)) : ?> 107 <p class="error"><?= h($error); ?></p> 108 <?php endif; ?> 109 <form action="" method="post" enctype="multipart/form-data"> 110 <p> 111 <label for="title">タイトル</label> 112 <input type="text" name="title" id="title" /> 113 </p> 114 <p> 115 <label for="upfile">画像ファイル</label> 116 <input type="file" name="upfile" id="upfile" /> 117 </p> 118 <p> 119 <button type="submit">送信</button> 120 </p> 121 </form> 122 </div> 123 </body> 124</html> 125
「upfile」の権限を
chown apache:apache upfiles/
chmod 777 upfiles/
とし、Apacheを再起動。
しかし、
「Warning: move_uploaded_file(upfiles/e0c4915a4546188b69b4163554e2b79969dad8ce.jpg): failed to open stream: No such file or directory in /var/www/html/image_up/index.php on line 66
Warning: move_uploaded_file(): Unable to move '/tmp/phpjo5TUP' to 'upfiles/e0c4915a4546188b69b4163554e2b79969dad8ce.jpg' in /var/www/html/image_up/index.php on line 66
ファイルの保存に失敗しました。」
というエラーになります。
/var/log/httpd/error_logを見ると、
[Sat Dec 08 07:52:23.002441 2018] [php7:warn] [pid 15501] [client 192.168.33.1:52095] PHP Warning: move_uploaded_file(): Unable to move '/tmp/phpjo5TUP' to 'upfiles/e0c4915a4546188b69b4163554e2b79969dad8ce.jpg' in /var/www/html/image_up/index.php on line 66, referer: http://192.168.33.10/image_up/
(途中で親ディレクトリの名前を「image_up」から「upfile」に変更しました。わかりにくくてすいません)
とあります。
権限はこんな感じです。
drwxrwxr-x. 2 apache apache 6 Dec 8 09:42 images
-rwxrwxr-x. 1 apache vagrant 2006 Dec 8 11:37 ImageUploader.php
-rwxrwxr-x. 1 apache vagrant 991 Dec 8 11:21 index.php
drwxrwxr-x. 2 apache apache 6 Dec 8 10:09 thumbs
「httpd.conf」の問題でしょうか?
解決策をお願いします。
回答1件
あなたの回答
tips
プレビュー