받는곳
<?php
class Config
{
/**
* Database host
* @var string
*/
const DB_HOST = 'localhost';
/**
* Database name
* @var string
*/
const DB_NAME = '';
/**
* Database user
* @var string
*/
const DB_USER = '유저임';
/**
* Database password
* @var string
*/
const DB_PASSWORD = '비번임';
/**
* Show or hide error messages on screen
* @var boolean
*/
const SHOW_ERRORS = true;
protected static function getDB()
{
static $db = null;
if ($db === null) {
$dsn = 'mysql:host=' . Config::DB_HOST . ';dbname=' . Config::DB_NAME . ';charset=utf8';
$db = new PDO($dsn, Config::DB_USER, Config::DB_PASSWORD);
// Throw an Exception when an error occurs
$db->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION);
}
return $db;
}
}
class reciverCon extends Config
{
/**
* @return 유저 IP가 반환됩니다.
*/
public function GetIPaddress()
{
if (isset($_SERVER['HTTP_X_FORWARDED_FOR'])) {
$_SERVER['REMOTE_ADDR'] = $_SERVER['HTTP_X_FORWARDED_FOR'];
}
return $_SERVER['REMOTE_ADDR'];
}
/**
* DB에 로그 쌓기용
*/
public function StackLogs($path=null,$name=null,$ipAdress=null,$action=null)
{
$path = $path;
$name = $name;
$ipAdress = $ipAdress;
$action = $action;
$nowDate = date('Y-m-d H:i:s');
$db = static::getDB();
$path = $db->query("INSERT INTO 디비임.테이블임 SET
path = '$path'
,fileName = '$name'
,ipAdress = '$ipAdress'
,action = '$action'
,createTime = '$nowDate'
");
}
/**
* 경로에 디렉토리가 없을경우 생성해줌
*/
public function MakeDir($data=null)
{
$path = $data;
$dirPath = dirname($path);
if (!is_dir($dirPath)) {
mkdir($dirPath, 0777, true);
}
}
/**
* @param $path = 로그파일에 남길 경로
* 경로에 있는 파일삭제 더 이상 남은 파일이 없다면 폴더까지 삭제
*/
public function DeleteFile($data=null,$path=null)
{
try
{
$file = $data;
$fileName = basename($file);
$filePath = dirname($file);
$logPath = $path;
if (file_exists($file))
{
unlink($file);
echo 'File deleted successfully';
// Call the StackLogs method to log the delete action
$this->StackLogs($logPath, $fileName, $this->GetIPaddress(), 'delete');
// Check if the directory is empty
if (count(scandir($filePath)) == 2) {
// If the directory is empty, delete it
rmdir($filePath);
echo ' and Directory deleted successfully';
}
}
else
{
throw new Exception('File does not exist');
}
}
catch (Exception $e)
{
echo 'Error deleting file: ' . $e->getMessage();
}
}
public function DeleteDir($data=null)
{
$dir = '/path/to/directory';
$files = glob($dir . '/*'); // get all file names
foreach($files as $file){ // iterate files
if(is_file($file))
unlink($file); // delete file
}
rmdir($dir); // delete directory
return 'Directory deleted successfully';
}
public function GenerateRandomString($length = 32)
{
$characters = '0123456789abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ';
$charactersLength = strlen($characters);
$randomString = '';
for ($i = 0; $i < $length; $i++) {
$randomString .= $characters[rand(0, $charactersLength - 1)];
}
return $randomString;
}
}
class reciverMo extends Config
{
/**
* @param $data = DB와 비교할 ip
* @return $data와 맞는 ip를 배열로 리턴
*/
public static function IPadressCheck($data=null)
{
$ip = $data;
$db = static::getDB();
$ipCheck = $db->query("SELECT
ipAdress
FROM 디비임.AllowIP
WHERE ipAdress = '$ip'
");
$result = $ipCheck->fetch(PDO::FETCH_ASSOC);
return $result;
}
}
$reciverCon = new reciverCon();
$GetIPaddress = $reciverCon->GetIPaddress();
$reciverMo = new reciverMo();
$IPadressCheck = $reciverMo->IPadressCheck($GetIPaddress);
if(in_array($GetIPaddress,$IPadressCheck))
{
$filePath = $_POST['filePath'].'/';
$fileName = $_POST['fileName'];
$action = $_POST['action'];
$defaultPath = '/var/www/html/uploads/';
$targetFilePath = $defaultPath.$filePath.$fileName;
switch($action)
{
case 'upload':
$fileType = $_POST['fileType'];
$imageType = array('jpg','jpeg','png');
if(in_array($fileType,$imageType))
{
$config["generate_image_file"] = true;
$config["image_max_size"] = 1000;
$config["new_width"] = 1000;
$config["destination_folder"] = $defaultPath.$filePath;
$config["upload_url"] = '';
$config["quality"] = 75;
$config["random_file_name"] = true;
$config["file_data"]=$_FILES["file"];
include("resize.class.php");
$im=new ImageResize($config);
try{
$responses=$im->resize();
$StackLogs = $reciverCon->StackLogs($filePath,$responses['images'][0],$GetIPaddress,$action);
echo $responses['images'][0];
}
catch(Exception $e)
{
echo 'Error uploading image: ' . $e->getMessage();
}
}
else
{
if(file_exists($targetFilePath))
{
$fileName = date('ymdHis').'_'.$fileName;
$targetFilePath = $defaultPath.$filePath.$fileName;
}
$MakeDir = $reciverCon->makeDir($targetFilePath);
if(move_uploaded_file($_FILES['file']['tmp_name'], $targetFilePath))
{
$StackLogs = $reciverCon->StackLogs($filePath,$fileName,$GetIPaddress,$action);
echo $fileName;
}
else
{
echo 'Error uploading file';
}
}
break;
case 'delete':
// $defaultPath = '/var/www/html/';
// $targetFilePath = $defaultPath.$filePath.$fileName;
$reciverCon->DeleteFile($targetFilePath,$filePath);
echo $reciverCon;
break;
case 'view':
$tempPath = '/var/www/html/temp/';
$viewName = $_POST['viewName'];
$nowDate = date('d');
$randomStr = $reciverCon->GenerateRandomString();
$savePath = $tempPath.$nowDate.'/'.$randomStr.'/'.$viewName;
$MakeDir = $reciverCon->makeDir($savePath);
if(copy($targetFilePath,$savePath))
{
$StackLogs = $reciverCon->StackLogs($filePath,$fileName,$GetIPaddress,$action);
echo substr($savePath, 14);
}
else
{
echo 'An error occurred while displaying the file.';
}
break;
}
}
else
{
$StackLogs = $reciverCon->StackLogs($filePath,$fileName,$GetIPaddress,$action);
echo 'Unacceptable ip address.';
}
보내는곳
<?php
namespace App\Controllers;
use \Core\View;
use \Core\Modules;
use PDO;
use CURLFile;
class ImageServerCon extends \Core\Controller
{
/**
* 이미지 서버
*/
public function UploadFileDo($data=null)
{
if(isset($_FILES["file"]) && isset($_POST['filePath']))
{
$allowTypes = array('pdf', 'doc', 'docx', 'jpg', 'png', 'jpeg');
$fileName = $_FILES['file']['name'];
$fileType = pathinfo($fileName, PATHINFO_EXTENSION);
if(in_array($fileType,$allowTypes))
{
if ($_FILES['file']['size'] < 5242880)
{
$url = "보내는곳";
$tempFile = $_FILES['file']['tmp_name'];
$filePath = $_POST['filePath'];
$file = new CURLFile($tempFile);
$ch = curl_init();
curl_setopt($ch,CURLOPT_URL, $url);
curl_setopt($ch,CURLOPT_POST, true);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
curl_setopt($ch, CURLOPT_POSTFIELDS, [
'file' => $file,
'fileName' => $fileName,
'filePath' => $filePath,
'fileType' => $fileType,
'action' => 'upload'
]);
$processedImage = curl_exec($ch);
curl_close($ch);
echo $processedImage;
}
else
{
echo 'Capacity is over 5 megabytes.';
}
}
else
{
echo 'The file type is not valid.';
}
}
else
{
echo 'File does not exist or file path does not exist.';
}
}
public function DeleteFileDo($data=null)
{
if(isset($_POST['filePath']) && isset($_POST['file']))
{
$url = '보내는곳';
$filePath = $_POST['filePath'];
$fileName = $_POST['file'];
$curl = curl_init();
curl_setopt_array($curl, array(
CURLOPT_URL => $url,
CURLOPT_RETURNTRANSFER => true,
CURLOPT_ENCODING => '',
CURLOPT_MAXREDIRS => 10,
CURLOPT_TIMEOUT => 0,
CURLOPT_FOLLOWLOCATION => true,
CURLOPT_HTTP_VERSION => CURL_HTTP_VERSION_1_1,
CURLOPT_CUSTOMREQUEST => 'POST',
CURLOPT_POSTFIELDS => array('filePath' => $filePath,'fileName' => $fileName, 'action' => 'delete'),
));
$response = curl_exec($curl);
curl_close($curl);
echo $response;
}
}
public function ViewFileDo($data=null)
{
if(isset($_POST["file"]) && isset($_POST['filePath']) && isset($_POST['viewName']))
{
$url = '보내는곳';
$file = $_POST['file'];
$filePath = $_POST['filePath'];
$viewName = $_POST['viewName'];
$curl = curl_init();
curl_setopt_array($curl, array(
CURLOPT_URL => $url,
CURLOPT_RETURNTRANSFER => true,
CURLOPT_ENCODING => '',
CURLOPT_MAXREDIRS => 10,
CURLOPT_TIMEOUT => 0,
CURLOPT_FOLLOWLOCATION => true,
CURLOPT_HTTP_VERSION => CURL_HTTP_VERSION_1_1,
CURLOPT_CUSTOMREQUEST => 'POST',
CURLOPT_POSTFIELDS => array('filePath' => $filePath,'fileName' => $file, 'viewName' => $viewName, 'action' => 'view'),
));
$response = curl_exec($curl);
curl_close($curl);
echo $response;
}
}
}
?>