-
Notifications
You must be signed in to change notification settings - Fork 0
/
testUpload.php
192 lines (167 loc) · 6.29 KB
/
testUpload.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
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
<html>
<meta name="viewport" content="width=device-width, initial-scale=1">
<title>Scouting App</title>
<link rel="stylesheet" type="text/css" href="Style/scoutingStyle.css">
<body>
<h1><center>Bomb Botz Scouting App</center></h1>
</body>
<p></p>
<?php
# Reference autoload (assuming you're using Composer)
require_once('vendor/autoload.php');
use MicrosoftAzure\Storage\Blob\Models\CreateBlockBlobOptions;
use MicrosoftAzure\Storage\Blob\Models\ListBlobsOptions;
use MicrosoftAzure\Storage\Blob\BlobRestProxy;
#phpinfo();
$file = $_FILES["fileToUpload"]["name"];
$tmpFile = $_FILES["fileToUpload"]["tmp_name"];
$targetFile = basename($file);
$uploadOk = 1;
$imageFileType = strtolower(pathinfo($targetFile,PATHINFO_EXTENSION));
echo "File: $file, Temp File: $tmpFile, Target File: $targetFile.<br />";
// Check if image file is a actual image or fake image
if(isset($_POST["submit"])) {
$check = getimagesize($_FILES["fileToUpload"]["tmp_name"]);
$mime = $check["mime"];
if($check !== false) {
echo "File is an image - $mime.<br />";
}
else {
echo "File is not an image.<br />";
$uploadOk = 0;
}
// Check file size < 15MB
$fileSize = $_FILES["fileToUpload"]["size"];
if ($fileSize > 15728640) {
echo "Sorry, your file is too large.<br />";
$uploadOk = 0;
}
// Allow certain file formats
$allowTypes = array('jpg','png','jpeg','gif');
if(!in_array($imageFileType, $allowTypes)){
echo "Sorry, only JPG, JPEG, PNG & GIF files are allowed.<br />";
$uploadOk = 0;
}
// Compress file
if ($uploadOk !== 0) {
// Compress size and upload image
$destinationFile = "/tmp/" . $file;
$compressedImage = compressImage($tmpFile, $destinationFile, 75);
if ($compressedImage) {
echo "Image compressed successfully: $compressedImage.<br />";
}
else {
echo "Image compressed failed!<br />";
}
}
// Check if $uploadOk is set to 0 by an error
if ($uploadOk == 0) {
echo "Sorry, your file was not uploaded.<br />";
}
// if everything is ok, try to upload file
else {
$storageAccountName = getenv("StorageAccountName");
$containerName = getenv("StorageContainer");
$accessKey = getenv("StorageAccessKey");
echo "Account Name: $storageAccountName, Container Name: $containerName.<br />";
# Use functions to upload file
$fileNameOnStorage = "2025/1234/" . $file;
echo "Calling Function storageAddFile.<br />";
storageAddFile($containerName, $compressedImage, $fileNameOnStorage, $mime, $storageAccountName, $accessKey);
echo "Remove the temporary file from the directory.<br />";
unlink($compressedImage);
}
}
# Function to add a file to storage
function storageAddFile($containerName, $tmpFile, $fileNameOnStorage, $mime, $storageAccountName, $accessKey) {
echo "In Function storageAddFile.<br />";
# Setup Azure Storage connection
$connectionString = "DefaultEndpointsProtocol=https;AccountName=$storageAccountName;AccountKey=$accessKey";
echo "Connection String: $connectionString.<br />";
try {
$blobClient = BlobRestProxy::createBlobService($connectionString);
}
catch (Exception $e) {
echo "Failed create Blob Service: " . $e . "<br />";
}
# Open the file
$handle = fopen($tmpFile, "r");
if ($handle) {
echo "Opened file '" . $tmpFile . "' for upload to storage." . "<br />";
$options = new CreateBlockBlobOptions();
# Identify MIME type
try {
$options->setContentType($mime);
} catch (Exception $e) {
echo "Failed to read MIME from '" . $tmpFile . "': " . $e . "<br />";
}
# Upload the blob
try {
if ($mime) {
$cacheTime = getCacheTimeByMimeType($mime);
if ($cacheTime) {
$options->setCacheControl("public, max-age=" . $cacheTime);
}
}
$blobClient->createBlockBlob($containerName, $fileNameOnStorage, $handle, $options);
} catch (Exception $e) {
echo "Failed to upload file '" . $tmpFile . "' to storage: " . $e . "<br />";
}
/*
# Remove the fClose because it returns an error. Filestream must be closed in createBlockBlob call
echo "Closing file '" . $tmpFile . "'." . "<br />";
fclose($handle);
*/
#Get List of Blobs
$key = '2025/1234/';
$blobListOptions = new ListBlobsOptions();
$blobListOptions->setPrefix($key);
$blobList = $blobClient->listBlobs($containerName, $blobListOptions);
foreach($blobList->getBlobs() as $key => $blob) {
echo "Blob ".$key.": \t".$blob->getName()."\t(".$blob->getUrl().")<br />";
echo '<img class="image'.$key.'" src="'.$blob->getUrl().'" style="max-width: 75%;"><br />';
# Test deleting Blob from storage
if ($blob->getName() == "2025/1234/6217.jpg") {
$blobClient->deleteBlob($containerName, $blob->getName());
}
}
return true;
} else {
echo "Failed to open file '" . $tmpFile . "' for upload to storage." . "<br />";
return false;
}
}
# Get cache time by MIME type
function getCacheTimeByMimeType($mime) {
$mime = strtolower($mime);
$types = array(
"application/json" => 604800, // 7 days
// Add more MIME types and cache times as needed
);
return $types[$mime] ?? null;
}
function compressImage($source, $destination, $quality) {
// Get image info
$imgInfo = getimagesize($source);
$mime = $imgInfo['mime'];
// Create a new image from file
switch($mime){
case 'image/jpeg':
$image = imagecreatefromjpeg($source);
break;
case 'image/png':
$image = imagecreatefrompng($source);
break;
case 'image/gif':
$image = imagecreatefromgif($source);
break;
default:
$image = imagecreatefromjpeg($source);
}
// Save image
imagejpeg($image, $destination, $quality);
// Return compressed image
return $destination;
}
?>
</html>