-
Notifications
You must be signed in to change notification settings - Fork 0
/
index.php
201 lines (152 loc) · 4.98 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
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
193
194
195
196
197
198
199
200
201
<?php
// Autoload dependencies installed via composer .
require_once __DIR__ . '/vendor/autoload.php';
use Aws\Exception\MultipartUploadException;
use Aws\S3\MultipartUploader;
use Symfony\Component\Dotenv\Dotenv;
use Monolog\Level;
use Monolog\Logger;
use Monolog\Handler\StreamHandler;
use Nick\PhpStreams\AwsS3Client;
// Load env custom variables.
$dotenv = new Dotenv();
$dotenv->load(__DIR__.'/.env');
$upload_dir = __DIR__ . '/uploads/';
// Log channel (local_dev), to track all logs/errors.
$log = new Logger('local_dev');
$log->pushHandler(new StreamHandler($upload_dir .'dev.log', Level::Warning));
// Global error handler for unhandled exceptions.
set_exception_handler(function (\Throwable $e) use ($log){
$log->error($e->getMessage());
echo $e;
});
/**
* Decodes posted file data.
* @param $data
*
* @return false|string
*/
function decode_chunk( $data ) {
$data = explode( ';base64,', $data );
if ( ! is_array( $data ) || ! isset( $data[1] ) ) {
return false;
}
$data = base64_decode( $data[1] );
if ( ! $data ) {
return false;
}
return $data;
}
$post_data = json_decode(file_get_contents('php://input'), true);
$headers = getallheaders();
if (isset( $post_data['file']) && !isset($post_data['move_uploaded'])) {
$message = 'test message!!';
$file_data = decode_chunk( $post_data['file_data']);
$file_path = $upload_dir . $post_data['file'];
$file_name = $post_data['file'];
file_put_contents( $file_path, $file_data, FILE_APPEND );
ob_start();
var_dump("|||||||||||||||||||||||||||||||");
var_dump($_POST);
var_dump("|||||||||||||||||||||||||||||||");
error_log(ob_get_clean(), 4);
$saved = TRUE;
$data = array("Saved" => $saved);
header('Content-Type: application/json');
echo json_encode($data);
exit();
}
elseif (isset($post_data['move_uploaded'] )) {
$s3Client = new AwsS3Client();
$s3Client = $s3Client->s3Client;
$bucket = $_ENV['AWS_S3_BUCKET'];
// Start of multipart uploads
$key = $post_data['file'];
$file_path = $upload_dir . $key;
// Use multipart upload
$source = $file_path;
$uploader = new MultipartUploader($s3Client, $source, [
'bucket' => $bucket,
'key' => $key,
'before_initiate' => function (\Aws\Command $command) {
// $command is a CreateMultipartUpload operation
$command['CacheControl'] = 'max-age=3600';
},
'before_upload' => function (\Aws\Command $command) {
// manual gc.
gc_collect_cycles();
// $command is an UploadPart operation
$command['RequestPayer'] = 'requester';
},
'before_complete' => function (\Aws\Command $command) {
// $command is a CompleteMultipartUpload operation
$command['RequestPayer'] = 'requester';
},
]);
//Recover from errors
do {
try {
$result = $uploader->promise()->wait();
} catch (MultipartUploadException $e) {
$uploader = new MultipartUploader($s3Client, $source, [
'state' => $e->getState(),
]);
}
} while (!isset($result));
//Abort a multipart upload if failed
try {
$result = $uploader->promise()->wait();
// Delete the file
echo "Upload complete: {$result['ObjectURL']}\n";
} catch (MultipartUploadException $e) {
// State contains the "Bucket", "Key", and "UploadId"
$params = $e->getState()->getId();
$result = $s3Client->abortMultipartUpload($params);
echo $e->getMessage() . "\n";
}
// End of multipart uploads
unlink($file_path);
$data = array("Saved" => true);
header('Content-Type: application/json');
echo json_encode($data);
exit();
}
?>
<!doctype html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport"
content="width=device-width, user-scalable=no, initial-scale=1.0, maximum-scale=1.0, minimum-scale=1.0">
<meta http-equiv="X-UA-Compatible" content="ie=edge">
<link rel="stylesheet" href="dist/main.css">
<title>A Web exp.!</title>
</head>
<body class="content-fluid">
<header>
</header>
<div>
<hr>
<hr>
<hr>
<form action="" method="post" enctype="multipart/form-data">
<h3 class="text-center mb-5">Uploading large files in PHP, from client's browser to an s3 bucket!</h3>
<p>Select a file to upload below.</p>
<p id="file-upload-progress">
<label for="file-upload-progress">File upload progress: </label>
<progress id="file-upload-progress" value="0" max="100">0%</progress>
</p>
<input id="file-upload-input" type="file" name="file_upload_input" /><br><br>
<input id="file-upload-submit" name="file-upload-submit" class="button button-primary" type="submit" value="Upload file" />
</form>
<hr>
<hr>
<hr>
<script src="dist/main.bundle.js"></script>
</div>
<footer>
<a href="https://flaircore.com/about" target="_blank">Copyright@2023 FlairCore.com</a>
</footer>
</body>
</html>
<?php