teratail header banner
teratail header banner
質問するログイン新規登録

回答編集履歴

3

修正

2017/03/05 05:39

投稿

退会済みユーザー
answer CHANGED
@@ -38,7 +38,7 @@
38
38
  {
39
39
  $dsn = 'mysql:host=localhost;dbname=sample;charset=utf8';
40
40
  $username = 'root';
41
- $password = '3m3tssrr';
41
+ $password = 'password';
42
42
  $options = [
43
43
  PDO::ATTR_ERRMODE => PDO::ERRMODE_EXCEPTION
44
44
  , PDO::ATTR_DEFAULT_FETCH_MODE => PDO::FETCH_ASSOC

2

EXIF情報を削除するコードを追加

2017/03/05 05:39

投稿

退会済みユーザー
answer CHANGED
@@ -158,6 +158,11 @@
158
158
  throw new Exception('ファイルの保存に失敗しました。');
159
159
  }
160
160
 
161
+ // Exif 情報の削除
162
+ $imagick = new Imagick($destination);
163
+ $imagick->stripimage();
164
+ $imagick->writeimage($destination);
165
+
161
166
  // データベースに登録
162
167
  $sql = 'INSERT INTO `images` (`id`, `title`, `path`) VALUES (NULL, :title, :path) ';
163
168
  $arr = [];
@@ -244,3 +249,6 @@
244
249
  </body>
245
250
  </html>
246
251
  ```
252
+ ---
253
+
254
+ Exif情報を削除するコードを追記しました。

1

サンプル追記

2017/03/05 02:47

投稿

退会済みユーザー
answer CHANGED
@@ -1,3 +1,246 @@
1
1
  `header('Content-Type: image/jpeg;');`
2
2
 
3
- が実行されるよりも前の部分で何らかの出力がされているからでしょう。
3
+ が実行されるよりも前の部分で何らかの出力がされているからでしょう。
4
+
5
+ ---
6
+
7
+ #PHP ファイルアップロードサンプル
8
+
9
+ #データベース定義
10
+
11
+ ```sql
12
+ CREATE TABLE `images` (
13
+ `id` int(11) unsigned NOT NULL AUTO_INCREMENT,
14
+ `title` varchar(32) DEFAULT NULL,
15
+ `path` varchar(255) DEFAULT NULL,
16
+ PRIMARY KEY (`id`)
17
+ ) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4;
18
+ ```
19
+
20
+ ##ディレクトリ構造
21
+
22
+ ![イメージ説明](e4b1d38e0f242b1f66ffed1f2212074b.png)
23
+
24
+ ##common.php
25
+
26
+ ```php
27
+ <?php
28
+
29
+ /**
30
+ * common.php
31
+ */
32
+
33
+ /**
34
+ * connect_db
35
+ * @return \PDO
36
+ */
37
+ function connect_db()
38
+ {
39
+ $dsn = 'mysql:host=localhost;dbname=sample;charset=utf8';
40
+ $username = 'root';
41
+ $password = '3m3tssrr';
42
+ $options = [
43
+ PDO::ATTR_ERRMODE => PDO::ERRMODE_EXCEPTION
44
+ , PDO::ATTR_DEFAULT_FETCH_MODE => PDO::FETCH_ASSOC
45
+ ];
46
+ return new PDO($dsn, $username, $password, $options);
47
+ }
48
+
49
+ /**
50
+ * insert
51
+ * @param string $sql
52
+ * @param array $arr
53
+ * @return int lastInsertId
54
+ */
55
+ function insert($sql, $arr = [])
56
+ {
57
+ $pdo = connect_db();
58
+ $stmt = $pdo->prepare($sql);
59
+ $stmt->execute($arr);
60
+ return $pdo->lastInsertId();
61
+ }
62
+
63
+ /**
64
+ * select
65
+ * @param string $sql
66
+ * @param array $arr
67
+ * @return array $rows
68
+ */
69
+ function select($sql, $arr = [])
70
+ {
71
+ $pdo = connect_db();
72
+ $stmt = $pdo->prepare($sql);
73
+ $stmt->execute($arr);
74
+ return $stmt->fetchAll();
75
+ }
76
+
77
+ /**
78
+ * htmlspecialchars
79
+ * @param string $string
80
+ * @return $string
81
+ */
82
+ function h($string)
83
+ {
84
+ return htmlspecialchars($string, ENT_QUOTES, 'utf-8');
85
+ }
86
+ ```
87
+
88
+ ##index.php
89
+
90
+ ```php
91
+ <?php
92
+ /**
93
+ * index.php
94
+ */
95
+ /**
96
+ * 共通関数読み込み
97
+ */
98
+ require 'common.php';
99
+
100
+ /**
101
+ * file_upload
102
+ */
103
+ function file_upload()
104
+ {
105
+ // POSTではないとき何もしない
106
+ if (filter_input(INPUT_SERVER, 'REQUEST_METHOD') !== 'POST') {
107
+ return;
108
+ }
109
+
110
+ // タイトル
111
+ $title = filter_input(INPUT_POST, 'title');
112
+ if ('' === $title) {
113
+ throw new Exception('タイトルは入力必須です。');
114
+ }
115
+
116
+ // アップロードファイル
117
+ $upfile = $_FILES['upfile'];
118
+
119
+ /**
120
+ * @see http://php.net/manual/ja/features.file-upload.post-method.php
121
+ */
122
+ if ($upfile['error'] > 0) {
123
+ throw new Exception('ファイルアップロードに失敗しました。');
124
+ }
125
+
126
+ $tmp_name = $upfile['tmp_name'];
127
+
128
+ // ファイルタイプチェック
129
+ $finfo = finfo_open(FILEINFO_MIME_TYPE);
130
+ $mimetype = finfo_file($finfo, $tmp_name);
131
+
132
+ // 許可するMIMETYPE
133
+ $allowed_types = [
134
+ 'jpg' => 'image/jpeg'
135
+ , 'png' => 'image/png'
136
+ , 'gif' => 'image/gif'
137
+ ];
138
+
139
+ if (!in_array($mimetype, $allowed_types)) {
140
+ throw new Exception('許可されていないファイルタイプです。');
141
+ }
142
+
143
+ // ファイル名
144
+ $filename = sha1_file($tmp_name);
145
+
146
+ // 拡張子
147
+ $ext = array_search($mimetype, $allowed_types);
148
+
149
+ // 保存作ファイルパス
150
+ $destination = sprintf('%s/%s.%s'
151
+ , 'upfiles'
152
+ , $filename
153
+ , $ext
154
+ );
155
+
156
+ // アップロードディレクトリに移動
157
+ if (!move_uploaded_file($tmp_name, $destination)) {
158
+ throw new Exception('ファイルの保存に失敗しました。');
159
+ }
160
+
161
+ // データベースに登録
162
+ $sql = 'INSERT INTO `images` (`id`, `title`, `path`) VALUES (NULL, :title, :path) ';
163
+ $arr = [];
164
+ $arr[':title'] = $title;
165
+ $arr[':path'] = $destination;
166
+ $lastInsertId = insert($sql, $arr);
167
+
168
+ // 成功時にページを移動する
169
+ header(sprintf('Location: image.php?id=%d', $lastInsertId));
170
+ }
171
+
172
+ try {
173
+ // ファイルアップロード
174
+ file_upload();
175
+ } catch (Exception $e) {
176
+ $error = $e->getMessage();
177
+ }
178
+ ?>
179
+ <!DOCTYPE HTML>
180
+ <html lang="ja">
181
+ <head>
182
+ <meta charset="UTF-8">
183
+ <title></title>
184
+ <style type="text/css">
185
+ .error {
186
+ color: red;
187
+ }
188
+ </style>
189
+ </head>
190
+ <body>
191
+ <div id="wrap">
192
+ <?php if (isset($error)) : ?>
193
+ <p class="error"><?= h($error); ?></p>
194
+ <?php endif; ?>
195
+ <form action="" method="post" enctype="multipart/form-data">
196
+ <p>
197
+ <label for="title">タイトル</label>
198
+ <input type="text" name="title" id="title" />
199
+ </p>
200
+ <p>
201
+ <label for="upfile">画像ファイル</label>
202
+ <input type="file" name="upfile" id="upfile" />
203
+ </p>
204
+ <p>
205
+ <button type="submit">送信</button>
206
+ </p>
207
+ </form>
208
+ </div>
209
+ </body>
210
+ </html>
211
+ ```
212
+
213
+ ##image.php
214
+
215
+ ```php
216
+ <?php
217
+ /**
218
+ * image.php
219
+ */
220
+ require 'common.php';
221
+
222
+ $id = filter_input(INPUT_GET, 'id');
223
+
224
+ // データベースからレコードを取得
225
+ $sql = 'SELECT `id`, `title`, `path` FROM `images` WHERE `id` = :id';
226
+ $arr = [];
227
+ $arr[':id'] = $id;
228
+ $rows = select($sql, $arr);
229
+ $row = reset($rows);
230
+ ?>
231
+ <!DOCTYPE HTML>
232
+ <html lang="ja">
233
+ <head>
234
+ <meta charset="UTF-8">
235
+ <title></title>
236
+ </head>
237
+ <body>
238
+ <div id="wrap">
239
+ <p><?= h($row['title']); ?></p>
240
+ <p>
241
+ <img src="<?= h($row['path']); ?>" alt="<?= h($row['title']); ?>" />
242
+ </p>
243
+ </div>
244
+ </body>
245
+ </html>
246
+ ```