forked from OpenShiftInAction/image-uploader
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathindex.php
119 lines (113 loc) · 2.87 KB
/
index.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
<html>
<head>
<?php
$author = getenv('AUTHOR');
$storage_type = getenv('STORAGE_TYPE');
echo "<title>Image Library Demo Application by " . $author . "</title>";
?>
<style>
body {
background-color: lightgrey;
font-family: arial;
}
#body-wrapper {
background-color: white;
width: 80%;
margin-left: auto;
margin-right: auto;
margin-top: 40px;
border-radius: 25px;
padding: 25px;
padding-bottom: 200px;
}
h1 {
font-family: arial;
}
img {
padding: 10px;
}
.wrapper {
border-radius: 25px;
border: 1px solid #333;
padding: 25px;
width: 40%;
margin: 20px;
}
#upload-form {
background-color: #ddd;
}
#results-box {
width: 40%;
color: red;
font-color: #fff;
font-weight: bold;
}
</style>
</head>
<body>
<div id="body-wrapper">
<?php
echo "
<h1>Image Library with " . $storage_type . " storage backend.</h1>
"
?>
<div class="wrapper" id="upload-form">
<form action="<?php echo $_SERVER['PHP_SELF']; ?>" method="post" enctype="multipart/form-data">
Select image to upload:
<p><input type="file" name="fileToUpload" id="fileToUpload"></p>
<p><input type="submit" value="Upload Image" name="submit"></p>
</form>
</div>
<?php
$target_dir = "uploads/";
if ($_SERVER['REQUEST_METHOD'] == 'POST') {
$target_file = $target_dir . strtolower(basename($_FILES["fileToUpload"]["name"]));
$uploadOk = 1;
$imageFileType = pathinfo($target_file,PATHINFO_EXTENSION);
// Check if image file is a actual image or fake image
echo "<div class='wrapper' id='results-box'>";
if(isset($_POST["submit"])) {
echo "<h3>Image Scan Results:</h3>";
$check = getimagesize($_FILES["fileToUpload"]["tmp_name"]);
if($check !== false) {
echo "<p>File is an image - " . $check["mime"] . ".</p>";
$uploadOk = 1;
} else {
echo "<p>File is not an image.</p>";
$uploadOk = 0;
}
}
// Check if file already exists
if (file_exists($target_file)) {
echo "<p>Sorry, file already exists.</p>";
$uploadOk = 0;
}
// Check file size
if ($_FILES["fileToUpload"]["size"] > 500000) {
echo "<p>Sorry, your file is too large.</p>";
$uploadOk = 0;
}
// Allow certain file formats
if($imageFileType != "jpg" && $imageFileType != "png" && $imageFileType != "jpeg"
&& $imageFileType != "gif" ) {
echo "<p>Sorry, only JPG, JPEG, PNG & GIF files are allowed.</p>";
$uploadOk = 0;
}
else {
if (move_uploaded_file($_FILES["fileToUpload"]["tmp_name"], $target_file)) {
echo "<p>". basename( $_FILES["fileToUpload"]["name"]). " has been uploaded.</p>";
} else {
echo "<p>Sorry, there was an error uploading your file.</p>";
}
}
echo "</div>";
}
echo "<h3>Uploaded Files</h3>";
$files = array_diff(scandir($target_dir), array('.', '..','uploads','.trashcan'));
foreach ($files as $f) {
echo "<a href='uploads/$f'><img src='uploads/$f' width='100px' height='100px'/></a>";
}
?>
</div>
</body>
</html>