| Linux in-mum-web1499.main-hosting.eu 5.14.0-503.40.1.el9_5.x86_64 #1 SMP PREEMPT_DYNAMIC Mon May 5 06:06:04 EDT 2025 x86_64 Path : /home/u901718425/domains/cdn.urbanpillar.in/public_html/ |
| Current File : /home/u901718425/domains/cdn.urbanpillar.in/public_html/uploadNewsfeed.php |
<?php
include_once "conn.php";
$pool = new ConnectionPool();
$connection1 = $pool->getConnection();
if (!$connection1) {
die("Connection failed");
}
$title ='';
if ($_POST) {
$res = array();
// Handle id and pricelable
$id = '';
if (isset($_POST['id'])) {
$id = $_POST['id'];
unset($_POST['id']);
}
if (isset($_FILES['image']) && $_FILES['image']['error'] == UPLOAD_ERR_OK) {
$_POST['image'] = uploadImage($_FILES,'');
}
if(isset( $_POST['image'] ) && $_POST['image'] ==""){
unset($_POST['image']);
}
date_default_timezone_set("Asia/Calcutta");
$date = date("Y-m-d H:i:s");
$_POST['date'] = $date;
$cols = array();
foreach ($_POST as $key => $val) {
$cols[] = strtolower($key) . "= :$key";
}
$set = implode(", ", $cols);
try {
if ($id != "") {
$query = "UPDATE `newsfeed` SET $set WHERE id = :id";
$stmt = $connection1->prepare($query);
$stmt->bindValue(':id', $id, PDO::PARAM_INT);
work($_POST['updated_by'], $id ,'Newsfeed Updated');
} else {
$column = implode(", ", array_keys($_POST));
$value = array_map(function ($val) {
return ":" . str_replace(' ', '', $val);
}, array_keys($_POST));
$value = implode(", ", $value);
$query = "SELECT MAX(`id`) FROM `newsfeed`;
SET @max_id = (SELECT MAX(`id`) FROM `newsfeed`);
INSERT INTO `newsfeed` (`id`, $column ) VALUES (@max_id + 1, $value)";
$stmt = $connection1->prepare($query);
}
foreach ($_POST as $key => $val) {
$stmt->bindValue(":$key", $val);
}
$result = $stmt->execute();
if($result){
$res[0] = 'success';
$res['result'] = $result;
if ($id == "") {
$res['id'] = $stmt->fetch(PDO::FETCH_ASSOC)['MAX(`id`)'] + 1;
$id = $res['id'];
work($_POST['updated_by'],$id,'New Newsfeed Addedd');
}
}
} catch (PDOException $e) {
$res[0] = 'error';
$res[1] = $e->getMessage();
} finally {
$pool->releaseConnection($connection1);
}
echo json_encode($res);
}
function convert_img_to_webp($file, $targetDir) {
$sourceFile = pathinfo($file, PATHINFO_FILENAME);
$outputFile = $targetDir . $sourceFile . '.webp';
$image = @imagecreatefromstring(file_get_contents($file));
if ($image !== false) {
imagewebp($image, $outputFile);
imagedestroy($image);
return $outputFile;
}
return false;
}
function uploadImage($file,$title = '') {
$filename = $_FILES['image']['name'];
$tempPath = $_FILES['image']['tmp_name'];
$targetDir = 'uploads/newsfeed/' . date('Y/m/d') . '/';
$targetPath = $targetDir . 'urbanpillar_' . $title . "_" . rand() . "_" . basename($filename);
if (!is_dir($targetDir)) {
mkdir($targetDir, 0777, true);
}
move_uploaded_file($tempPath, $targetPath);
if (pathinfo($filename, PATHINFO_EXTENSION) != 'webp') {
$targetPath = convert_img_to_webp($targetPath, $targetDir);
}
return $targetPath;
}
?>