Open
Description
to store and delete files I wrote a simple php file that reads the folder where to store the file and the file/files, giving back the name of the saved files or an empty array if something failed. Same process to delete them.
save
<?php
/*
folder structure:
root
|-api.php => backend file
|-images/ => all saved images
|-tools/ => images saving handlers (save, delete..)
.htassess rewrites all requests not containing 'api.php', 'images' and 'tools' to api.php/$received_path
for this reason it is needed to go 1 directory up to save the image in the correct directory
*/
function sendResponse($status = 200, $body = '', $content_type = 'application/json; charset=utf-8'){
$status_header = 'HTTP/1.1 ' . $status ;
header($status_header);
header('Content-type: ' . $content_type);
echo $body;
}
header("Content-Type: application/json");
header("Access-Control-Allow-Origin: *");
header("Access-Control-Allow-Methods: PUT, GET, POST, OPTIONS");
header("Access-Control-Allow-Headers: Origin, X-Requested-With, Content-Type, Accept");
//REMEMBER TO sudo chcon -t httpd_sys_rw_content_t **FOLDER** -R
$postdata = file_get_contents('php://input');
$request = json_decode($postdata,true);
$folder = $request['path'];//example "images/mapIcons/" - relative path received
$folder_relativePath = '../'.$folder; // - 1 level up to find 'image' directory
$response = array();
foreach ($request['files'] as $file) {
$img = explode(";base64,", $file);
$img_aux = explode("image/", $img[0]);
$image_type = $img_aux[1];
$img_base64 = base64_decode($img[1]);
$uniqfn = uniqid().'.'.$image_type;
$image_relativePath = $folder_relativePath . $uniqfn; // saving path
$image = $folder . $uniqfn; //relative path given as response
$r = file_put_contents($image_relativePath, $img_base64);
if($r) {
array_push($response,array('filePath'=>$image));
}
}
sendResponse(200,json_encode($response));
?>
delete
<?php
/*
folder structure:
root
|-api.php => backend file
|-images/ => all saved images
|-tools/ => images saving handlers (save, delete..)
.htassess rewrites all requests not containing 'api.php', 'images' and 'tools' to api.php/$received_path
for this reason it is needed to go 1 directory up to save the image in the correct directory
*/
function sendResponse($status = 200, $body = '', $content_type = 'application/json; charset=utf-8'){
$status_header = 'HTTP/1.1 ' . $status ;
header($status_header);
header('Content-type: ' . $content_type);
echo $body;
}
header("Content-Type: application/json");
header("Access-Control-Allow-Origin: *");
header("Access-Control-Allow-Methods: PUT, GET, POST, OPTIONS");
header("Access-Control-Allow-Headers: Origin, X-Requested-With, Content-Type, Accept");
$postdata = file_get_contents('php://input');
$request = json_decode($postdata,true);
//REMEMBER TO sudo chcon -t httpd_sys_rw_content_t **FOLDER** -R
$response = array();
foreach ($request['files'] as $file) {
foreach (glob($file . '*') as $filename) {
if(unlink(realpath('../'.$filename))){ //1 directory up @#@#@#@#@#@#@#@#@#@#@
array_push($response,array($filename));
};
}
}
sendResponse(200,json_encode($response)); //'{"file_path":"'.$image.'"}')
?>
this is the content of the .htaccess
file or the apache config file
RewriteEngine On
RewriteBase /
RewriteCond %{REQUEST_URI} save [NC]
RewriteRule ^([^/]+)(.*)?$ tools/saveImages.php/$1$2 [L]
RewriteCond %{REQUEST_URI} delete [NC]
RewriteRule ^([^/]+)(.*)?$ delete/deleteImages.php/$1$2 [L]
RewriteCond %{REQUEST_URI} !api\.php [NC] //if none of the previous condition, if REQUEST_URI does't contain api.php redirect to api.php - this is meant to mask api.php from the endpoint string
RewriteRule ^([^/]+)(.*)?$ api.php/$1$2 [L]
sorry for my primitive php..
so before storing the record in the db I save the file, get the path, and send the post request to store the record.
Would be nice to dedicate an endpoint to it and have it annoverated in the openapi, won't it?