質問編集履歴

1

コードの追記です。

2016/05/06 12:50

投稿

nazo_o
nazo_o

スコア99

test CHANGED
File without changes
test CHANGED
@@ -15,3 +15,169 @@
15
15
 
16
16
 
17
17
  GDライブラリ、というものがないことが原因でしょうか?
18
+
19
+
20
+
21
+
22
+
23
+ index.php
24
+
25
+
26
+
27
+ <?php
28
+
29
+
30
+
31
+ ini_set('display_errors', 1);
32
+
33
+ define('MAX_FILE_SIZE', 1 * 1024 * 1024); // 1MB
34
+
35
+ define('THUMBNAIL_WIDTH', 400);
36
+
37
+ define('IMAGES_DIR', __DIR__ . '/images');
38
+
39
+ define('THUMBNAIL_DIR', __DIR__ . '/thumbs');
40
+
41
+
42
+
43
+ if (!function_exists('imagecreatetruecolor')) {
44
+
45
+ echo 'GD not installed';
46
+
47
+ exit;
48
+
49
+ }
50
+
51
+
52
+
53
+ function h($s) {
54
+
55
+ return htmlspecialchars($s, ENT_QUOTES, 'UTF-8');
56
+
57
+ }
58
+
59
+
60
+
61
+ require 'ImageUploader.php';
62
+
63
+
64
+
65
+ $uploader = new \MyApp\ImageUploader();
66
+
67
+
68
+
69
+ if ($_SERVER['REQUEST_METHOD'] === 'POST') {
70
+
71
+ $uploader->upload();
72
+
73
+ }
74
+
75
+
76
+
77
+ ?>
78
+
79
+ <!DOCTYPE html>
80
+
81
+ <html lang="ja">
82
+
83
+ <head>
84
+
85
+ <meta charset="utf-8">
86
+
87
+ <title>Image Uploader</title>
88
+
89
+ <style>
90
+
91
+ body {
92
+
93
+ text-align: center;
94
+
95
+ font-family: Arial, sans-serif;
96
+
97
+ }
98
+
99
+ </style>
100
+
101
+ </head>
102
+
103
+ <body>
104
+
105
+
106
+
107
+ <form action="" method="post" enctype="multipart/form-data">
108
+
109
+ <input type="hidden" name="MAX_FILE_SIZE" value="<?php h(MAX_FILE_SIZE); ?>">
110
+
111
+ <input type="file" name="image">
112
+
113
+ <input type="submit" value="upload">
114
+
115
+ </form>
116
+
117
+
118
+
119
+ </body>
120
+
121
+ </html>
122
+
123
+
124
+
125
+
126
+
127
+ ImageUploader.php
128
+
129
+
130
+
131
+ <?php
132
+
133
+
134
+
135
+ namespace MyApp;
136
+
137
+
138
+
139
+ class ImageUploader {
140
+
141
+
142
+
143
+ public function upload() {
144
+
145
+ try {
146
+
147
+ // error check
148
+
149
+ $this->_validateUpload();
150
+
151
+
152
+
153
+ // type check
154
+
155
+ // save
156
+
157
+ // create thumbnail
158
+
159
+ } catch (\Exception $e) {
160
+
161
+ echo $e->getMessage();
162
+
163
+ exit;
164
+
165
+ }
166
+
167
+ // redirect
168
+
169
+ header('Location: http://' . $_SERVER['HTTP_HOST']);
170
+
171
+ exit;
172
+
173
+ }
174
+
175
+
176
+
177
+ private function _validateUpload() {
178
+
179
+
180
+
181
+ }
182
+
183
+ }