-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathupload.php
More file actions
28 lines (20 loc) · 733 Bytes
/
upload.php
File metadata and controls
28 lines (20 loc) · 733 Bytes
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
<?php
// get base64 image data
$base = $_POST['base64'];
$data = $base;
$regex = '~(?<=\["|",")[^"]*~';
preg_match_all($regex, $data, $matches);
$imgs = array_shift($matches);
for($i=0; $i< count($imgs); $i++) {
// get image extension
$pos = strpos($imgs[$i], ';');
$ext = explode(':image/', substr($imgs[$i], 0, $pos))[1];
if($ext == 'jpeg') { $ext = 'jpg'; }
// decode base64 data
$base_to_php = preg_replace('#^data:image/\w+;base64,#i', '', $imgs[$i]);
$data = base64_decode($base_to_php);
$filepath = uniqid(rand(), true) . '.' . $ext;
file_put_contents($filepath, $data);
}
echo "Uploaded successfully!";
?>