回答編集履歴
3
修正
test
CHANGED
@@ -78,7 +78,7 @@
|
|
78
78
|
|
79
79
|
$username = 'root';
|
80
80
|
|
81
|
-
$password = '
|
81
|
+
$password = 'password';
|
82
82
|
|
83
83
|
$options = [
|
84
84
|
|
2
EXIF情報を削除するコードを追加
test
CHANGED
@@ -318,6 +318,16 @@
|
|
318
318
|
|
319
319
|
|
320
320
|
|
321
|
+
// Exif 情報の削除
|
322
|
+
|
323
|
+
$imagick = new Imagick($destination);
|
324
|
+
|
325
|
+
$imagick->stripimage();
|
326
|
+
|
327
|
+
$imagick->writeimage($destination);
|
328
|
+
|
329
|
+
|
330
|
+
|
321
331
|
// データベースに登録
|
322
332
|
|
323
333
|
$sql = 'INSERT INTO `images` (`id`, `title`, `path`) VALUES (NULL, :title, :path) ';
|
@@ -490,4 +500,10 @@
|
|
490
500
|
|
491
501
|
```
|
492
502
|
|
493
|
-
|
503
|
+
---
|
504
|
+
|
505
|
+
|
506
|
+
|
507
|
+
Exif情報を削除するコードを追記しました。
|
508
|
+
|
509
|
+
|
1
サンプル追記
test
CHANGED
@@ -3,3 +3,491 @@
|
|
3
3
|
|
4
4
|
|
5
5
|
が実行されるよりも前の部分で何らかの出力がされているからでしょう。
|
6
|
+
|
7
|
+
|
8
|
+
|
9
|
+
---
|
10
|
+
|
11
|
+
|
12
|
+
|
13
|
+
#PHP ファイルアップロードサンプル
|
14
|
+
|
15
|
+
|
16
|
+
|
17
|
+
#データベース定義
|
18
|
+
|
19
|
+
|
20
|
+
|
21
|
+
```sql
|
22
|
+
|
23
|
+
CREATE TABLE `images` (
|
24
|
+
|
25
|
+
`id` int(11) unsigned NOT NULL AUTO_INCREMENT,
|
26
|
+
|
27
|
+
`title` varchar(32) DEFAULT NULL,
|
28
|
+
|
29
|
+
`path` varchar(255) DEFAULT NULL,
|
30
|
+
|
31
|
+
PRIMARY KEY (`id`)
|
32
|
+
|
33
|
+
) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4;
|
34
|
+
|
35
|
+
```
|
36
|
+
|
37
|
+
|
38
|
+
|
39
|
+
##ディレクトリ構造
|
40
|
+
|
41
|
+
|
42
|
+
|
43
|
+
![イメージ説明](e4b1d38e0f242b1f66ffed1f2212074b.png)
|
44
|
+
|
45
|
+
|
46
|
+
|
47
|
+
##common.php
|
48
|
+
|
49
|
+
|
50
|
+
|
51
|
+
```php
|
52
|
+
|
53
|
+
<?php
|
54
|
+
|
55
|
+
|
56
|
+
|
57
|
+
/**
|
58
|
+
|
59
|
+
* common.php
|
60
|
+
|
61
|
+
*/
|
62
|
+
|
63
|
+
|
64
|
+
|
65
|
+
/**
|
66
|
+
|
67
|
+
* connect_db
|
68
|
+
|
69
|
+
* @return \PDO
|
70
|
+
|
71
|
+
*/
|
72
|
+
|
73
|
+
function connect_db()
|
74
|
+
|
75
|
+
{
|
76
|
+
|
77
|
+
$dsn = 'mysql:host=localhost;dbname=sample;charset=utf8';
|
78
|
+
|
79
|
+
$username = 'root';
|
80
|
+
|
81
|
+
$password = '3m3tssrr';
|
82
|
+
|
83
|
+
$options = [
|
84
|
+
|
85
|
+
PDO::ATTR_ERRMODE => PDO::ERRMODE_EXCEPTION
|
86
|
+
|
87
|
+
, PDO::ATTR_DEFAULT_FETCH_MODE => PDO::FETCH_ASSOC
|
88
|
+
|
89
|
+
];
|
90
|
+
|
91
|
+
return new PDO($dsn, $username, $password, $options);
|
92
|
+
|
93
|
+
}
|
94
|
+
|
95
|
+
|
96
|
+
|
97
|
+
/**
|
98
|
+
|
99
|
+
* insert
|
100
|
+
|
101
|
+
* @param string $sql
|
102
|
+
|
103
|
+
* @param array $arr
|
104
|
+
|
105
|
+
* @return int lastInsertId
|
106
|
+
|
107
|
+
*/
|
108
|
+
|
109
|
+
function insert($sql, $arr = [])
|
110
|
+
|
111
|
+
{
|
112
|
+
|
113
|
+
$pdo = connect_db();
|
114
|
+
|
115
|
+
$stmt = $pdo->prepare($sql);
|
116
|
+
|
117
|
+
$stmt->execute($arr);
|
118
|
+
|
119
|
+
return $pdo->lastInsertId();
|
120
|
+
|
121
|
+
}
|
122
|
+
|
123
|
+
|
124
|
+
|
125
|
+
/**
|
126
|
+
|
127
|
+
* select
|
128
|
+
|
129
|
+
* @param string $sql
|
130
|
+
|
131
|
+
* @param array $arr
|
132
|
+
|
133
|
+
* @return array $rows
|
134
|
+
|
135
|
+
*/
|
136
|
+
|
137
|
+
function select($sql, $arr = [])
|
138
|
+
|
139
|
+
{
|
140
|
+
|
141
|
+
$pdo = connect_db();
|
142
|
+
|
143
|
+
$stmt = $pdo->prepare($sql);
|
144
|
+
|
145
|
+
$stmt->execute($arr);
|
146
|
+
|
147
|
+
return $stmt->fetchAll();
|
148
|
+
|
149
|
+
}
|
150
|
+
|
151
|
+
|
152
|
+
|
153
|
+
/**
|
154
|
+
|
155
|
+
* htmlspecialchars
|
156
|
+
|
157
|
+
* @param string $string
|
158
|
+
|
159
|
+
* @return $string
|
160
|
+
|
161
|
+
*/
|
162
|
+
|
163
|
+
function h($string)
|
164
|
+
|
165
|
+
{
|
166
|
+
|
167
|
+
return htmlspecialchars($string, ENT_QUOTES, 'utf-8');
|
168
|
+
|
169
|
+
}
|
170
|
+
|
171
|
+
```
|
172
|
+
|
173
|
+
|
174
|
+
|
175
|
+
##index.php
|
176
|
+
|
177
|
+
|
178
|
+
|
179
|
+
```php
|
180
|
+
|
181
|
+
<?php
|
182
|
+
|
183
|
+
/**
|
184
|
+
|
185
|
+
* index.php
|
186
|
+
|
187
|
+
*/
|
188
|
+
|
189
|
+
/**
|
190
|
+
|
191
|
+
* 共通関数読み込み
|
192
|
+
|
193
|
+
*/
|
194
|
+
|
195
|
+
require 'common.php';
|
196
|
+
|
197
|
+
|
198
|
+
|
199
|
+
/**
|
200
|
+
|
201
|
+
* file_upload
|
202
|
+
|
203
|
+
*/
|
204
|
+
|
205
|
+
function file_upload()
|
206
|
+
|
207
|
+
{
|
208
|
+
|
209
|
+
// POSTではないとき何もしない
|
210
|
+
|
211
|
+
if (filter_input(INPUT_SERVER, 'REQUEST_METHOD') !== 'POST') {
|
212
|
+
|
213
|
+
return;
|
214
|
+
|
215
|
+
}
|
216
|
+
|
217
|
+
|
218
|
+
|
219
|
+
// タイトル
|
220
|
+
|
221
|
+
$title = filter_input(INPUT_POST, 'title');
|
222
|
+
|
223
|
+
if ('' === $title) {
|
224
|
+
|
225
|
+
throw new Exception('タイトルは入力必須です。');
|
226
|
+
|
227
|
+
}
|
228
|
+
|
229
|
+
|
230
|
+
|
231
|
+
// アップロードファイル
|
232
|
+
|
233
|
+
$upfile = $_FILES['upfile'];
|
234
|
+
|
235
|
+
|
236
|
+
|
237
|
+
/**
|
238
|
+
|
239
|
+
* @see http://php.net/manual/ja/features.file-upload.post-method.php
|
240
|
+
|
241
|
+
*/
|
242
|
+
|
243
|
+
if ($upfile['error'] > 0) {
|
244
|
+
|
245
|
+
throw new Exception('ファイルアップロードに失敗しました。');
|
246
|
+
|
247
|
+
}
|
248
|
+
|
249
|
+
|
250
|
+
|
251
|
+
$tmp_name = $upfile['tmp_name'];
|
252
|
+
|
253
|
+
|
254
|
+
|
255
|
+
// ファイルタイプチェック
|
256
|
+
|
257
|
+
$finfo = finfo_open(FILEINFO_MIME_TYPE);
|
258
|
+
|
259
|
+
$mimetype = finfo_file($finfo, $tmp_name);
|
260
|
+
|
261
|
+
|
262
|
+
|
263
|
+
// 許可するMIMETYPE
|
264
|
+
|
265
|
+
$allowed_types = [
|
266
|
+
|
267
|
+
'jpg' => 'image/jpeg'
|
268
|
+
|
269
|
+
, 'png' => 'image/png'
|
270
|
+
|
271
|
+
, 'gif' => 'image/gif'
|
272
|
+
|
273
|
+
];
|
274
|
+
|
275
|
+
|
276
|
+
|
277
|
+
if (!in_array($mimetype, $allowed_types)) {
|
278
|
+
|
279
|
+
throw new Exception('許可されていないファイルタイプです。');
|
280
|
+
|
281
|
+
}
|
282
|
+
|
283
|
+
|
284
|
+
|
285
|
+
// ファイル名
|
286
|
+
|
287
|
+
$filename = sha1_file($tmp_name);
|
288
|
+
|
289
|
+
|
290
|
+
|
291
|
+
// 拡張子
|
292
|
+
|
293
|
+
$ext = array_search($mimetype, $allowed_types);
|
294
|
+
|
295
|
+
|
296
|
+
|
297
|
+
// 保存作ファイルパス
|
298
|
+
|
299
|
+
$destination = sprintf('%s/%s.%s'
|
300
|
+
|
301
|
+
, 'upfiles'
|
302
|
+
|
303
|
+
, $filename
|
304
|
+
|
305
|
+
, $ext
|
306
|
+
|
307
|
+
);
|
308
|
+
|
309
|
+
|
310
|
+
|
311
|
+
// アップロードディレクトリに移動
|
312
|
+
|
313
|
+
if (!move_uploaded_file($tmp_name, $destination)) {
|
314
|
+
|
315
|
+
throw new Exception('ファイルの保存に失敗しました。');
|
316
|
+
|
317
|
+
}
|
318
|
+
|
319
|
+
|
320
|
+
|
321
|
+
// データベースに登録
|
322
|
+
|
323
|
+
$sql = 'INSERT INTO `images` (`id`, `title`, `path`) VALUES (NULL, :title, :path) ';
|
324
|
+
|
325
|
+
$arr = [];
|
326
|
+
|
327
|
+
$arr[':title'] = $title;
|
328
|
+
|
329
|
+
$arr[':path'] = $destination;
|
330
|
+
|
331
|
+
$lastInsertId = insert($sql, $arr);
|
332
|
+
|
333
|
+
|
334
|
+
|
335
|
+
// 成功時にページを移動する
|
336
|
+
|
337
|
+
header(sprintf('Location: image.php?id=%d', $lastInsertId));
|
338
|
+
|
339
|
+
}
|
340
|
+
|
341
|
+
|
342
|
+
|
343
|
+
try {
|
344
|
+
|
345
|
+
// ファイルアップロード
|
346
|
+
|
347
|
+
file_upload();
|
348
|
+
|
349
|
+
} catch (Exception $e) {
|
350
|
+
|
351
|
+
$error = $e->getMessage();
|
352
|
+
|
353
|
+
}
|
354
|
+
|
355
|
+
?>
|
356
|
+
|
357
|
+
<!DOCTYPE HTML>
|
358
|
+
|
359
|
+
<html lang="ja">
|
360
|
+
|
361
|
+
<head>
|
362
|
+
|
363
|
+
<meta charset="UTF-8">
|
364
|
+
|
365
|
+
<title></title>
|
366
|
+
|
367
|
+
<style type="text/css">
|
368
|
+
|
369
|
+
.error {
|
370
|
+
|
371
|
+
color: red;
|
372
|
+
|
373
|
+
}
|
374
|
+
|
375
|
+
</style>
|
376
|
+
|
377
|
+
</head>
|
378
|
+
|
379
|
+
<body>
|
380
|
+
|
381
|
+
<div id="wrap">
|
382
|
+
|
383
|
+
<?php if (isset($error)) : ?>
|
384
|
+
|
385
|
+
<p class="error"><?= h($error); ?></p>
|
386
|
+
|
387
|
+
<?php endif; ?>
|
388
|
+
|
389
|
+
<form action="" method="post" enctype="multipart/form-data">
|
390
|
+
|
391
|
+
<p>
|
392
|
+
|
393
|
+
<label for="title">タイトル</label>
|
394
|
+
|
395
|
+
<input type="text" name="title" id="title" />
|
396
|
+
|
397
|
+
</p>
|
398
|
+
|
399
|
+
<p>
|
400
|
+
|
401
|
+
<label for="upfile">画像ファイル</label>
|
402
|
+
|
403
|
+
<input type="file" name="upfile" id="upfile" />
|
404
|
+
|
405
|
+
</p>
|
406
|
+
|
407
|
+
<p>
|
408
|
+
|
409
|
+
<button type="submit">送信</button>
|
410
|
+
|
411
|
+
</p>
|
412
|
+
|
413
|
+
</form>
|
414
|
+
|
415
|
+
</div>
|
416
|
+
|
417
|
+
</body>
|
418
|
+
|
419
|
+
</html>
|
420
|
+
|
421
|
+
```
|
422
|
+
|
423
|
+
|
424
|
+
|
425
|
+
##image.php
|
426
|
+
|
427
|
+
|
428
|
+
|
429
|
+
```php
|
430
|
+
|
431
|
+
<?php
|
432
|
+
|
433
|
+
/**
|
434
|
+
|
435
|
+
* image.php
|
436
|
+
|
437
|
+
*/
|
438
|
+
|
439
|
+
require 'common.php';
|
440
|
+
|
441
|
+
|
442
|
+
|
443
|
+
$id = filter_input(INPUT_GET, 'id');
|
444
|
+
|
445
|
+
|
446
|
+
|
447
|
+
// データベースからレコードを取得
|
448
|
+
|
449
|
+
$sql = 'SELECT `id`, `title`, `path` FROM `images` WHERE `id` = :id';
|
450
|
+
|
451
|
+
$arr = [];
|
452
|
+
|
453
|
+
$arr[':id'] = $id;
|
454
|
+
|
455
|
+
$rows = select($sql, $arr);
|
456
|
+
|
457
|
+
$row = reset($rows);
|
458
|
+
|
459
|
+
?>
|
460
|
+
|
461
|
+
<!DOCTYPE HTML>
|
462
|
+
|
463
|
+
<html lang="ja">
|
464
|
+
|
465
|
+
<head>
|
466
|
+
|
467
|
+
<meta charset="UTF-8">
|
468
|
+
|
469
|
+
<title></title>
|
470
|
+
|
471
|
+
</head>
|
472
|
+
|
473
|
+
<body>
|
474
|
+
|
475
|
+
<div id="wrap">
|
476
|
+
|
477
|
+
<p><?= h($row['title']); ?></p>
|
478
|
+
|
479
|
+
<p>
|
480
|
+
|
481
|
+
<img src="<?= h($row['path']); ?>" alt="<?= h($row['title']); ?>" />
|
482
|
+
|
483
|
+
</p>
|
484
|
+
|
485
|
+
</div>
|
486
|
+
|
487
|
+
</body>
|
488
|
+
|
489
|
+
</html>
|
490
|
+
|
491
|
+
```
|
492
|
+
|
493
|
+
|