-
Notifications
You must be signed in to change notification settings - Fork 0
/
uploader.php
150 lines (119 loc) · 3.74 KB
/
uploader.php
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
<?php if ($_SERVER["HTTP_REFERER"] == "http://".$_SERVER["HTTP_HOST"].dirname($_SERVER['PHP_SELF'])."/uploader.php"):?>
<?php
ini_set("display_errors", 1);
error_reporting(E_ALL);
$dsn = 'mysql:dbname=Sunseer_BBS; host=localhost; charset=utf8';
$user = 'masa';
$passwd = 'masa';
$id = $_POST['id'];
$image = $_FILES['image']['name'];
$up_err = $_FILES['image']['error'];
$src = $_FILES['image']['tmp_name'];
$type = $_FILES['image']['type'];
$dir = "Image/".$image;
$image_size = getimagesize($_FILES['image']['tmp_name']);
define("MAX_SIZE",1*1024*1024);
define("THUMBNAIL_WIDTH",400);
define("IMAGES_DIR",__DIR__."/Image");
define("THUMBNAIL_DIR",__DIR__."/Thumbs".'/'.$image);
echo THUMBNAIL_DIR;
if ($_FILES['image']['size']<MAX_SIZE){
$image_width = $image_size[0];
$image_height = $image_size[1];
// THUMBNAIL_WITDH →Use of undefined constant THUMBNAIL_WITDH - assumed 'THUMBNAIL_WITDH' (this will throw an Error in a future version of PHP)
if ($image_width > 400 ) {
//:TODO $type におけるエラーなので。あとで治す
// switch ($type) {
// case IMAGETYPE_GIF;
// $src_Image = imagecreatefromgif($src);
// break;
// case IMAGETYPE_PNG;
// $src_Image = imagecreatefrompng($src);
// break;
// case IMAGETYPE_JPEG;
// $src_Image = imagecreatefromjpeg($src);
// break;
// default:
// echo "判別・作成できません";
// break;
// }
$src_Image = imagecreatefromjpeg($src);
$thumbwidth = 100;
$thumbheight = round($image_height * 100/$image_width);
$thumbImage = imagecreatetruecolor(100,$thumbheight);
imagecopyresampled($thumbImage,$src_Image,0,0,0,0,$thumbwidth,$thumbheight,$image_width,$image_height);
// switch($type){
// case IMAGETYPE_GIF;
// imagegif($thumbImage,THUMBNAIL_DIR.'/'.$image);
// // ファイルの出力
// case IMAGETYPE_PNG;
// imagepng($thumbImage,THUMBNAIL_DIR.'/'.$image);
// case IMAGETYPE_JPEG;
// imagejpeg($thumbImage,THUMBNAIL_DIR.'/'.$image);
// //
// break;
// default:
// echo "判別できません";
// break;
// }
imagejpeg($thumbImage,__DIR__."/Thumbs".'/'.$image);
$thumb_db ="Thumbs".'/'.$image;
try {
$db = new PDO($dsn,$user,$passwd);
$db->setAttribute(PDO::ATTR_ERRMODE,PDO::ERRMODE_EXCEPTION);
$sql2 = "UPDATE db_BBS SET icon='$thumb_db' WHERE id='$id'";
if(!$db->query($sql2)){
echo "sql処理に問題あり";
}
} catch (\PDOException $e) {
echo $e->getMessage();
}
}
}else {
echo "画像ファイルのサイズが大きすぎます";
if ($up_err == UPLOAD_ERR_OK) {
if (move_uploaded_file($src,$dir)) {
try {
$sql = "UPDATE db_BBS SET icon='$dir' WHERE id='$id'";
// $stat = $db->query()
if(!$db->query($sql)){
echo "sql処理に問題あり";
}
} catch (\PDOException $e) {
print $e->getMessage();
}
}else{
echo "アップロード処理に失敗しました";
}
}else {
echo "エラー:{$up_err}<br>";
}
}
var_dump($_FILES);
// print __DIR__;
// $savePath = IMAGES_DIR."/{$src}";
?>
<!DOCTYPE html>
<html lang="ja">
<head>
<meta charset="utf-8">
<title>upload image</title>
</head>
<body>
<h1>images</h1>
<img src="<?php echo "Image/".$image; ?>">
<h1>thumbnail</h1>
<img src="<?php echo $thumb_db; ?>">
<!--:TODO なぜか php echo THUMBNAIL_DIR; ?> では表示されない? path miss ? or -->
<br>
<br>
<?php
echo $thumb_db;
echo " <a href=".$_SERVER['HTTP_REFERER'].">前に戻る</a>" ;
?>
<!--正規でない場合 -->
<?php else: ?>
<h3>あなたは正規の方法でこのウェブサイトにアクセスしていません。</h3>
<br>
<a href="#" onclick="javascript:window.history.back(-1);return false;">戻る</a>
<?php endif ;?>