Currently in use:' . $methord . '';
echo ' Set Background Using File   ';
echo ' Set Background Using URL';
echo ' ';
echo '';
echo '
';
if (isset($_POST['upload'])) {
// Handle the uploaded file
// Check if the form was submitted
$selectedFiles = ['logo.png', 'index.php', 'iimg.json', 'filenames.json', 'binding_dark.webp', 'bg.jpg', 'api.php', 'favicon.ico', 'logo_ne.png', '.htaccess']; // Example array of selected files
$folderPath = './img_custom/Img/'; // Replace with the actual folder path
$files = scandir($folderPath);
foreach ($files as $file) {
if ($file !== '.' && $file !== '..') {
$filePath = $folderPath . $file;
// Check if the file is selected
if (in_array($file, $selectedFiles)) {
// File is selected, do nothing
} else {
// Delete the file
unlink($filePath);
}
}
}
if (isset($_FILES['image'])) {
$file = $_FILES['image'];
$fileType = $file['type'];
$fileTemp = $file['tmp_name'];
// Validate the file type
$allowedTypes = ['image/jpeg', 'image/png', 'image/gif'];
if (in_array($fileType, $allowedTypes)) {
// Define the path to store the uploaded image
$uploadPath = './img_custom/Img/';
$fileName = uniqid() . '.' . pathinfo($file['name'], PATHINFO_EXTENSION);
$destination = $uploadPath . $fileName;
// Move the uploaded file to the destination
if (move_uploaded_file($fileTemp, $destination)) {
echo "";
$jsonFilePath = './img/fundo/image_filenames.json';
$jsonData = json_encode([["ImageName" => "../img_custom/Img/" . $fileName, 'Upload_type' => 'by_file']]);
file_put_contents($jsonFilePath, $jsonData);
} else {
echo 'Failed to move the uploaded file.';
}
} else {
echo 'Invalid file type. Only JPEG, PNG, and GIF images are allowed.';
}
}
}
// Image by URL handling
if (isset($_POST['url-submit'])) {
$imageUrl = $_POST['image-url'];
// Validate the URL
if (filter_var($imageUrl, FILTER_VALIDATE_URL)) {
$jsonFilePath = './img/fundo/image_filenames.json';
// Prepare the data to be added to the JSON file
$newImageData = [
'ImageName' => $imageUrl,
'Upload_type' => 'by_url'
];
// Read the existing JSON data
$jsonData = file_get_contents($jsonFilePath);
// Decode the JSON data
$imageData = json_decode($jsonData, true);
// Add the new image data to the existing array
$imageData[0] = $newImageData;
// Encode the updated data as JSON
$jsonData = json_encode($imageData);
// Save the updated JSON data to the file
if (file_put_contents($jsonFilePath, $jsonData)) {
echo "";
} else {
echo 'Failed to save the image data to the JSON file.';
}
} else {
echo 'Invalid URL.';
}
}
?>