-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathserver.php
More file actions
executable file
·116 lines (83 loc) · 3.03 KB
/
Copy pathserver.php
File metadata and controls
executable file
·116 lines (83 loc) · 3.03 KB
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
<?php
header("Access-Control-Allow-Origin: *");
// Permite la ejecucion de los metodos
header("Access-Control-Allow-Methods: GET, POST, PUT, DELETE");
switch ($_SERVER['REQUEST_METHOD']) {
case 'GET':
$directorio = opendir("./upload"); //ruta actual
$contador = 0;
while ($archivo = readdir($directorio)) //obtenemos un archivo y luego otro sucesivamente
{
if (!is_dir($archivo))//verificamos si es o no un directorio
{
$listado[$contador] = $archivo;
}
$contador++;
}
$listado = array_values($listado);
if(count($listado)!=0) {
print_json(200, true, $listado);
} else {
print_json(200, "No existen elementos", null);
}
break;
case 'POST':
$content = file_get_contents("php://input");
$array = json_decode($content, true);
$validar = validate($array['encodedImage']);
if($validar==1) {
$imagen = save_base64_image($array['encodedImage'], "upload/IMG_" . strtoupper(md5($array['encodedImage'])));
if($imagen!=null) {
print_json(200, "Completado", $imagen);
} else {
print_json(200, "Este archivo ya existe", null);
}
} else {
print_json(200, "Extension invalida", null);
}
break;
default:
print_json(405, 'Metodo no permitido', null);
break;
}
function validate($string_base64) {
$array = explode(",", $string_base64);
if( ($array[0] == "data:image/jpeg;base64") || ($array[0] == "data:image/gif;base64") || ($array[0] == "data:image/png;base64") ) {
return 1;
} else {
return 0;
}
}
function save_base64_image($base64_image_string, $output_file_without_extentnion, $path_with_end_slash="" ) {
//usage: if( substr( $img_src, 0, 5 ) === "data:" ) { $filename=save_base64_image($base64_image_string, $output_file_without_extentnion, getcwd() . "/application/assets/pins/$user_id/"); }
//
//data is like: data:image/png;base64,asdfasdfasdf
$splited = explode(',', substr( $base64_image_string , 5 ) , 2);
$mime=$splited[0];
$data=$splited[1];
$mime_split_without_base64=explode(';', $mime,2);
$mime_split=explode('/', $mime_split_without_base64[0],2);
if(count($mime_split)==2)
{
$extension=$mime_split[1];
if($extension=='jpeg')$extension='jpg';
//if($extension=='javascript')$extension='js';
//if($extension=='text')$extension='txt';
$output_file_with_extentnion.=$output_file_without_extentnion.'.'.$extension;
}
if(file_exists($output_file_with_extentnion)) {
return null;
} else {
file_put_contents( $path_with_end_slash . $output_file_with_extentnion, base64_decode($data) );
return $output_file_with_extentnion;
}
}
function print_json($status, $mensaje, $data) {
header("HTTP/1.1 $status $mensaje");
header("Content-Type: application/json; charset=UTF-8");
$response['statusCode'] = $status;
$response['statusMessage'] = $mensaje;
$response['data'] = $data;
echo json_encode($response, JSON_PRETTY_PRINT);
}
?>