| 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/Table.php |
<?php
$dsn = 'mysql:host='.getenv('DB_HOST').';dbname='.getenv('DB_DATABASE');
$username = getenv('DB_USERNAME');
$password = getenv('DB_PASSWORD');
function convert_img_to_webp($file,$targetDir){
// Get a list of all image files in the source directory
$sourceFile = pathinfo($file, PATHINFO_FILENAME);
$outputFile = $targetDir . $sourceFile . '.webp';
// Load the image using GD library
$image = @imagecreatefromstring(file_get_contents($file));
if ($image === false) {
// Handle the error, log, or skip the current image
// continue; // Skip to the next iteration of the loop
echo 'file not created <br>';
$outputFile = $file;
}else{
imagewebp($image, $outputFile);
imagedestroy($image);
}
return $outputFile;
// Free up memory
}
try {
// Create a PDO instance
$pdo = new PDO($dsn, $username, $password);
$pdo->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION);
// Define the SQL query
$query = "SELECT id, date, image_repo FROM new_property";
// Prepare and execute the query
$stmt = $pdo->prepare($query);
$stmt->execute();
// Fetch the results as an associative array
$results = $stmt->fetchAll(PDO::FETCH_ASSOC);
foreach ($results as $row) {
$createdDate = new DateTime($row['date']);
$path = [];
$path = json_decode($row['image_repo']);
$recordId = $row['id'];
// Create a directory structure based on the image's creation date
$year = $createdDate->format('Y');
$month = $createdDate->format('m');
$day = $createdDate->format('d');
$targetDir = 'uploads/' . $year . '/' . $month . '/' . $day . '/';
// Create the directory structure if it doesn't exist
if (!is_dir($targetDir)) {
mkdir($targetDir, 0777, true);
}
// Define the source and target paths
if(isset($path) && is_array($path) && !empty($path)){
foreach($path as $k => $val){
$sourcePath = 'files/' . $val;
$targetPath = $targetDir . basename($val);
if(!is_dir($targetPath) && file_exists($targetPath) ){
unlink($targetPath);
}
if(move_uploaded_file($sourcePath, $targetPath)){
echo 'Moved file: ' . $sourcePath . ' to ' . $targetPath . '<br>';
echo pathinfo($sourcePath, PATHINFO_EXTENSION);
if( pathinfo($sourcePath, PATHINFO_EXTENSION) != 'webp'){
$targetPath = convert_img_to_webp($sourcePath,$targetDir);
}
$max = $pdo->prepare('SELECT MAX(id) FROM `image`');
$max->execute();
$maxId = $max->fetchAll(PDO::FETCH_ASSOC);
$id = ($maxId[0]['MAX(id)'] != "" ? (int)$maxId[0]['MAX(id)'] + 1 : 1);
// Update the record in the database with the new path
$updateQuery = "INSERT INTO `image`(`id`, `f_id`, `path`, `default`, `is`, `updated_by`, `date`) VALUES (?,?,?,?,?,?,?)";
$updateStmt = $pdo->prepare($updateQuery);
$updateStmt->execute([$id,$recordId,$targetPath, ($k == 0 ? 1 : NULL),'property','admin',$row['date']]);
// unlink($sourcePath);
echo 'Updated record with new path.<br>';
}else{
if (!is_dir($targetPath) && file_exists($sourcePath)) {
// Attempt to move the file
if (rename($sourcePath, $targetPath)) {
echo 'File moved successfully.';
$max = $pdo->prepare('SELECT MAX(id) FROM `image`');
$max->execute();
$maxId = $max->fetchAll(PDO::FETCH_ASSOC);
$id = ($maxId[0]['MAX(id)'] != "" ? (int)$maxId[0]['MAX(id)'] + 1 : 1);
// Update the record in the database with the new path
$updateQuery = "INSERT INTO `image`(`id`, `f_id`, `path`, `default`, `is`, `updated_by`, `date`) VALUES (?,?,?,?,?,?,?)";
$updateStmt = $pdo->prepare($updateQuery);
$updateStmt->execute([$id,$recordId,$targetPath, ($k == 0 ? 1 : NULL),'property','admin',$row['date']]);
// unlink($sourcePath);
echo ' Updated record with new path.<br>';
} else {
echo ' Error moving file.';
}
} else {
echo $sourcePath.' Source file does not exist. <br>';
}
}
// } else {
// echo 'Image Not Found: ' . $sourcePath . '<br>';
// }
}
}
}
} catch (PDOException $e) {
// Handle any connection or query errors
echo 'Connection failed: ' . $e->getMessage();
}
?>