Commit b3f5b714 authored by Rich Gerdes's avatar Rich Gerdes

Init working version of site.

parents
Header set Access-Control-Allow-Origin *
Header add Access-Control-Allow-Headers "origin, x-requested-with, content-type"
Header add Access-Control-Allow-Methods "PUT, GET, POST, DELETE, OPTIONS"
# Simple Image CDN
A simple php program to recieve and manage uploaded files.
# About
This program was written by [Rich Gerdes](rich@richgerdes.com).
# Usage
Files can be sent to this server. The uploaded files are validated, hashed, and
stored on the server. The server validates that the uploaded files are valid
images, via file extention and metadata. Once validates, the filename is hashed
with MD5, and the uploaded file name is build from the hash and the original
file extention. If the file hash is unique, then the file is considered a
duplicate upload, and is not saved.
<?php
// Sample use file for uploaded files via php.
/*
* On upload of multiple files, submit them to the cdn for storage.
*
* In order to upload, the files need to be hased the file is then attached to
* a curl request and uploaded to the cdn. Once uploaded, the files will be
* verified, and saved. The resulting api, will return `result` = `success` if
* the file was valid and either saved or matched an already uploaded file.
*/
$error_count = 0;
foreach ($_FILES['files']['name'] as $f => $name) {
// Hash the file, and get name and extention.
$check = getimagesize($_FILES["files"]["tmp_name"][$f]);
$hash = md5(file_get_contents($_FILES["files"]["tmp_name"][$f]));
$file = basename($_FILES["files"]["name"][$f]);
$ext = pathinfo($file, PATHINFO_EXTENSION);
// Construct filename for attaching to the curl request.
$filestr = "";
if(function_exists('curl_file_create')){
$filestr = curl_file_create($_FILES["files"]["tmp_name"][$f], $check["mime"], $_FILES["files"]["name"][$f]);
}else{
$filestr = "@".$_FILES["files"]["tmp_name"][$f].";type=".$check["mime"].";filename=" . $file;
}
// Prepare the rest of the curl query.
$params = array('hash' => $hash, 'secrets' => "<UPLOADSECRET>", 'file' => $filestr);
$target_url = "<SIMPLE IMAGE CDN URL>";
// Build and send the curl request.
$ch = curl_init();
curl_setopt($ch, CURLOPT_URL,$target_url);
curl_setopt($ch, CURLOPT_POST,1);
curl_setopt($ch, CURLOPT_POSTFIELDS, $params);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
$result=curl_exec ($ch);
curl_close ($ch);
// If their is valid response, decode the json response.
if(isset($result)){
$res = json_decode($result, true);
if(isset($res) && $res["RESULT"] === "SUCCESS"){
// On success handle the uploaded here
// ...
}else{
// Track error count.
$error_count++;
}
}
}
<?php
// Base API Result data.
$results = array(
"RESULT" => "ERROR",
"ERROR" => "INVALID PARAMETERS",
);
// Upload secret for basic security.
$UPLOAD_SECRET = "<UPLOADSECRET>";
// Check for valid upload and that the secret is set.
if(isset($_FILES["file"])
&& isset($_POST["secrets"])
&& $_POST["secrets"] === $UPLOAD_SECRET) {
if($_FILES["file"]["error"] > 2){
// Check that file uploaded properly. Error > 2 is general.
$results["ERROR"] = "UPLOAD ERROR";
}else if($_FILES["file"]["error"] !== 0){
// Check that file uploaded properly. Error 1 indicates oversize file.
$results["ERROR"] = "LARGE FILE";
}else{
// Hash uploaded file, validate md5, and build filename.
$file = basename($_FILES["file"]["name"]);
$ext = strtolower(pathinfo($file, PATHINFO_EXTENSION));
$hash = md5_file($_FILES["file"]["tmp_name"]);
$target_filename = $hash.".".$ext;
// Setup target dir and upload path.
$target_dir = "./";
$target_file = $target_dir . $target_filename;
// If the file is not an image getimagesize() returns false.
$check = getimagesize($_FILES["file"]["tmp_name"]);
// Build return data array for file.
$imagedata = array(
"ORIGINAL" => basename($_FILES["file"]["name"]),
"UPLOADED" => basename($_FILES["file"]["tmp_name"]),
"EXTENTION" => $ext,
"DESTINATION" => $target_filename,
);
// If we get valid mage data, return the data in the image block.
if($check){
$imagedata["META"] = $check;
}
// Since the file was successfully uploaded, set has and meta data.
$results["HASH"] = $hash;
$results["DATA"] = $imagedata;
// Check for valid file, file extention, and save file.
if($check === false) {
$results["ERROR"] = "NON IMAGE";
}else if($ext != "jpg" && $ext != "png" && $ext != "jpeg" && $ext != "gif" ) {
$results["ERROR"] = "INVALID TYPE";
}else if($hash != $_POST["hash"]){
$results["ERROR"] = "HASH MISMATCH";
}else if (file_exists($target_file)) {
// If target file exists, then its a duplicate. This is allowed.
$results["RESULT"] = "SUCCESS"; // Set Success, so default error is removed.
$results["SUCCESS"] = "FILE ALREADY UPLAODED";
}else if (move_uploaded_file($_FILES["file"]["tmp_name"], $target_file)) {
// If no copy, then save the file.
$results["RESULT"] = "SUCCESS"; // Set Success, so default error is removed.
$results["SUCCESS"] = "SUCCESS";
}else{
// Return io error if the file fails to be copied. Check storage.
$results["ERROR"] = "IO ERROR";
}
}
}
// If the result is now success, remove the default error message.
if ($results["RESULT"] !=== "ERROR") {
unset($results["ERROR"]);
}
// Return the datablock to the user as json.
echo json_encode($results);
?>
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment