File manager - Edit - /home/asiatechinc/public_html/asiatech-websites/mayergramprsfarm.in/admin/upload.php
Back
<?php session_start(); if(!isset($_SESSION['admin'])){ header("Location: login.php"); exit; } // Configuration $uploadDir = '../uploads/'; $allowedTypes = ['image/jpeg', 'image/jpg', 'image/png', 'image/gif', 'image/webp']; $maxSize = 5 * 1024 * 1024; // 5MB // Create uploads directory if it doesn't exist if (!file_exists($uploadDir)) { mkdir($uploadDir, 0755, true); } if ($_SERVER['REQUEST_METHOD'] === 'POST' && isset($_FILES['image'])) { $response = ['success' => false, 'message' => '', 'filepath' => '']; $file = $_FILES['image']; // Check for errors if ($file['error'] !== UPLOAD_ERR_OK) { $response['message'] = 'Upload failed with error code: ' . $file['error']; echo json_encode($response); exit; } // Check file type $finfo = finfo_open(FILEINFO_MIME_TYPE); $mime = finfo_file($finfo, $file['tmp_name']); finfo_close($finfo); if (!in_array($mime, $allowedTypes)) { $response['message'] = 'Invalid file type. Only JPG, PNG, GIF, and WebP are allowed.'; echo json_encode($response); exit; } // Check file size if ($file['size'] > $maxSize) { $response['message'] = 'File is too large. Maximum size is 5MB.'; echo json_encode($response); exit; } // Generate unique filename $extension = pathinfo($file['name'], PATHINFO_EXTENSION); $filename = uniqid() . '_' . time() . '.' . $extension; $filepath = $uploadDir . $filename; // Move uploaded file if (move_uploaded_file($file['tmp_name'], $filepath)) { $response['success'] = true; $response['message'] = 'File uploaded successfully'; $response['filepath'] = 'uploads/' . $filename; // Relative path for frontend $response['filename'] = $filename; } else { $response['message'] = 'Failed to move uploaded file.'; } echo json_encode($response); exit; } // Handle file deletion if ($_SERVER['REQUEST_METHOD'] === 'POST' && isset($_POST['action']) && $_POST['action'] === 'delete') { $response = ['success' => false, 'message' => '']; if (isset($_POST['filepath'])) { $filepath = '../' . $_POST['filepath']; if (file_exists($filepath) && strpos($filepath, $uploadDir) === 0) { if (unlink($filepath)) { $response['success'] = true; $response['message'] = 'File deleted successfully'; } else { $response['message'] = 'Failed to delete file'; } } else { $response['message'] = 'File not found or invalid path'; } } echo json_encode($response); exit; } // Get list of uploaded images if ($_SERVER['REQUEST_METHOD'] === 'GET' && isset($_GET['action']) && $_GET['action'] === 'list') { $images = []; if (is_dir($uploadDir)) { $files = scandir($uploadDir); foreach ($files as $file) { if ($file !== '.' && $file !== '..' && !is_dir($uploadDir . $file)) { $images[] = [ 'name' => $file, 'path' => 'uploads/' . $file, 'size' => filesize($uploadDir . $file), 'modified' => filemtime($uploadDir . $file) ]; } } } echo json_encode($images); exit; } ?>
| ver. 1.4 |
Github
|
.
| PHP 7.4.33 | Generation time: 0.04 |
proxy
|
phpinfo
|
Settings