-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathupload.php
108 lines (91 loc) · 3.14 KB
/
upload.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
<?php
// INCLUDES
include 'inc.php';
if (isset($_POST['submit'])) {
if (checkLogin()) {
if ($_FILES['file']['name'] == '') {
$message = new Message('Error: No file selected.', 1);
$error = TRUE;
}
if (!$error) {
// Connect to database
dbConnect("radef894_imgrate");
// Get names
$file_name = $_FILES['file']['name'];
$file_tmp_name = $_FILES['file']['tmp_name'];
// Check extension
$extension = str_replace('.', '', substr($file_name, -4));
if ($extension == 'jpg' || $extension == 'jpeg') {
$newImage = imagecreatefromjpeg($file_tmp_name);
}
elseif ($extension == 'png') {
$newImage = imagecreatefrompng($file_tmp_name);
}
elseif ($extension == 'gif') {
$newImage = imagecreatefromgif($file_tmp_name);
}
else {
$message = new Message('Error: File type not supported. Upload process terminated.', 1);
$error = TRUE;
}
if (!$error) {
// Resize
list($width, $height) = getimagesize($file_tmp_name);
$newHeight = 150;
$newWidth = $width * ($newHeight / $height);
$tmpImage = imagecreatetruecolor($newWidth, $newHeight);
imagecopyresampled($tmpImage, $newImage, 0, 0, 0, 0, $newWidth, $newHeight, $width, $height);
// Give file new name
$now = time();
$suffix = '-imagerate.' . $extension;
while (file_exists('images/' . $now . $suffix)) {
$now++;
}
$newFilename = $now . $suffix;
$newFilePath = 'images/' . $newFilename;
imagejpeg($tmpImage, $newFilePath, 80);
imagedestroy($newImage);
imagedestroy($tmpImage);
mysql_query("INSERT INTO `tbl_images` (`img`) VALUES ('$newFilename')") or die(mysql_error());
// Set message
$message = new Message('Image uploaded successfully!', 0);
}
}
}
else {
$message = msgNotAuthorized();
}
}
?>
<?php include 'inc.head.php'; ?>
<title>Upload image | ImgRate</title>
</head>
<body>
<div class="container-fluid upload text-center">
<div class="row-fluid">
<div class="span12">
<header>
<?php include 'inc.nav.php'; ?>
</header>
<?php include 'inc.message.php'; ?>
<article>
<h1 class="title">Upload</h1>
<p>Upload an image to be rated. Supported file formats are jpg, jpeg, png and gif.</p>
<form action="upload.php" method="post" enctype="multipart/form-data">
<input type="file" name="file" id="file" style="display:none">
<div class="input-append">
<input type="text" id="fileText">
<a class="btn" onclick="$('input#file').click();">Browse</a>
</div>
<div class="control-group">
<div class="controls">
<button type="submit" name="submit" class="btn btn-primary">Upload</button>
</div>
</div>
</form>
</article>
</div>
</div>
<?php include 'inc.footer.php'; ?>
</body>
</html>