header('Content-Type: image/jpeg;');
が実行されるよりも前の部分で何らかの出力がされているからでしょう。
#PHP ファイルアップロードサンプル
#データベース定義
sql
1 CREATE TABLE ` images ` (
2 ` id ` int ( 11 ) unsigned NOT NULL AUTO_INCREMENT ,
3 ` title ` varchar ( 32 ) DEFAULT NULL ,
4 ` path ` varchar ( 255 ) DEFAULT NULL ,
5 PRIMARY KEY ( ` id ` )
6 ) ENGINE = InnoDB DEFAULT CHARSET = utf8mb4 ;
##ディレクトリ構造
##common.php
php
1 <?php
3 5
7 9 10
11 function connect_db ( )
12 {
13 $dsn = 'mysql:host=localhost;dbname=sample;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 }
23 25 26 27 28
29 function insert ( $sql , $arr = [ ] )
30 {
31 $pdo = connect_db ( ) ;
32 $stmt = $pdo -> prepare ( $sql ) ;
33 $stmt -> execute ( $arr ) ;
34 return $pdo -> lastInsertId ( ) ;
35 }
37 39 40 41 42
43 function select ( $sql , $arr = [ ] )
44 {
45 $pdo = connect_db ( ) ;
46 $stmt = $pdo -> prepare ( $sql ) ;
47 $stmt -> execute ( $arr ) ;
48 return $stmt -> fetchAll ( ) ;
49 }
51 53 54 55
56 function h ( $string )
57 {
58 return htmlspecialchars ( $string , ENT_QUOTES , 'utf-8' ) ;
59 }
##index.php
php
1 <?php
2 3 * index.php
4
5 6 * 共通関数読み込み
7
8 require 'common.php' ;
9
10 11 * file_upload
12
13 function 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 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
49 if ( ! in_array ( $mimetype , $allowed_types ) ) {
50 throw new Exception ( '許可されていないファイルタイプです。' ) ;
51 }
52
53 // ファイル名
54 $filename = sha1_file ( $tmp_name ) ;
55
56 // 拡張子
57 $ext = array_search ( $mimetype , $allowed_types ) ;
58
59 // 保存作ファイルパス
60 $destination = sprintf ( '%s/%s.%s'
61 , 'upfiles'
62 , $filename
63 , $ext
64 ) ;
65
66 // アップロードディレクトリに移動
67 if ( ! move_uploaded_file ( $tmp_name , $destination ) ) {
68 throw new Exception ( 'ファイルの保存に失敗しました。' ) ;
69 }
70
71 // Exif 情報の削除
72 $imagick = new Imagick ( $destination ) ;
73 $imagick -> stripimage ( ) ;
74 $imagick -> writeimage ( $destination ) ;
75
76 // データベースに登録
77 $sql = 'INSERT INTO `images` (`id`, `title`, `path`) VALUES (NULL, :title, :path) ' ;
78 $arr = [ ] ;
79 $arr [ ':title' ] = $title ;
80 $arr [ ':path' ] = $destination ;
81 $lastInsertId = insert ( $sql , $arr ) ;
82
83 // 成功時にページを移動する
84 header ( sprintf ( 'Location: image.php?id=%d' , $lastInsertId ) ) ;
85 }
86
87 try {
88 // ファイルアップロード
89 file_upload ( ) ;
90 } catch ( Exception $e ) {
91 $error = $e -> getMessage ( ) ;
92 }
93 ?>
94 <! DOCTYPE HTML >
95 < html lang = " ja " >
96 < head >
97 < meta charset = " UTF-8 " >
98 < title > </ title >
99 < style type = " text/css " >
100 .error {
101 color : red ;
102 }
103 </ style >
104 </ head >
105 < body >
106 < div id = " wrap " >
107 <?php if ( isset ( $error ) ) : ?>
108 < p class = " error " > <?= h ( $error ) ; ?> </ p >
109 <?php endif ; ?>
110 < form action = " " method = " post " enctype = " multipart/form-data " >
111 < p >
112 < label for = " title " > タイトル </ label >
113 < input type = " text " name = " title " id = " title " />
114 </ p >
115 < p >
116 < label for = " upfile " > 画像ファイル </ label >
117 < input type = " file " name = " upfile " id = " upfile " />
118 </ p >
119 < p >
120 < button type = " submit " > 送信 </ button >
121 </ p >
122 </ form >
123 </ div >
124 </ body >
125 </ html >
##image.php
php
1 <?php
2 3 * image.php
4
5 require 'common.php' ;
6
7 $id = filter_input ( INPUT_GET , 'id' ) ;
8
9 // データベースからレコードを取得
10 $sql = 'SELECT `id`, `title`, `path` FROM `images` WHERE `id` = :id' ;
11 $arr = [ ] ;
12 $arr [ ':id' ] = $id ;
13 $rows = select ( $sql , $arr ) ;
14 $row = reset ( $rows ) ;
15 ?>
16 <! DOCTYPE HTML >
17 < html lang = " ja " >
18 < head >
19 < meta charset = " UTF-8 " >
20 < title > </ title >
21 </ head >
22 < body >
23 < div id = " wrap " >
24 < p > <?= h ( $row [ 'title' ] ) ; ?> </ p >
25 < p >
26 < img src = " <?= h ( $row [ 'path' ] ) ; ?> " alt = " <?= h ( $row [ 'title' ] ) ; ?> " />
27 </ p >
28 </ div >
29 </ body >
30 </ html >
Exif情報を削除するコードを追記しました。
バッドをするには、ログインかつ
こちらの条件を満たす必要があります。
2017/03/04 10:18
退会済みユーザー
2017/03/04 10:23
2017/03/04 14:48
退会済みユーザー
2017/03/04 15:47 編集
退会済みユーザー
2017/03/04 17:11 編集
2017/03/04 22:48