« Back to History
uploader_by_category.php
|
20260920_164915.php
Initial Domain Snapshot
Copy Code
<?php // uploader_by_category_bulk.php // Category-based image uploader with bulk upload (multiple files + ZIP extraction) // ---------------- CONFIG / DB (optional) ---------------- $DB_HOST = 'localhost'; $DB_NAME = 'u410576661_shayari'; $DB_USER = 'u410576661_shayari'; $DB_PASS = 'Shayari@9898'; // optional PDO (uploader works without it) $pdo = null; try { $dsn = "mysql:host={$DB_HOST};dbname={$DB_NAME};charset=utf8mb4"; $pdo = new PDO($dsn, $DB_USER, $DB_PASS, [ PDO::ATTR_ERRMODE => PDO::ERRMODE_EXCEPTION, PDO::ATTR_DEFAULT_FETCH_MODE => PDO::FETCH_ASSOC, ]); } catch (Exception $e) { error_log("Optional DB connect failed: " . $e->getMessage()); $pdo = null; } // ---------------- Category => folder mapping ---------------- $category_map = [ 'love_shayari' => 'love_shayari_img', 'sad_shayari' => 'sad_shayri_img', 'funny' => 'funny_shayari_img', 'default' => 'misc_shayari_img', ]; // settings $allowed_ext = ['jpg','jpeg','png','webp','gif']; $max_file_size = 10 * 1024 * 1024; // 10 MB per file (adjust as needed) $max_files_bulk = 100; // max files processed in one request $errors = []; $success = null; $results = []; // array of uploaded files info function safe_unique_name($prefix, $ext) { try { $uniq = bin2hex(random_bytes(6)); } catch (Exception $e) { $uniq = bin2hex(openssl_random_pseudo_bytes(6)); } return $prefix . '_' . time() . '_' . $uniq . '.' . $ext; } if ($_SERVER['REQUEST_METHOD'] === 'POST') { $category_key = $_POST['category'] ?? 'default'; $folder_slug = $category_map[$category_key] ?? $category_map['default']; $relative_dir = 'uploads/' . $folder_slug; $abs_dir = rtrim(__DIR__, '/\\') . '/' . $relative_dir; if (!is_dir($abs_dir)) { if (!@mkdir($abs_dir, 0775, true)) { $errors[] = "Upload folder create karne me fail: {$abs_dir}"; } else { @chmod($abs_dir, 0775); } } if (empty($errors) && !is_writable($abs_dir)) { $errors[] = "Upload folder writable nahi hai: {$abs_dir}"; } // ---- HANDLE MULTIPLE IMAGE FILES (input name="images[]") ---- if (!empty($_FILES['images']) && is_array($_FILES['images']['name'])) { $names = $_FILES['images']['name']; $tmps = $_FILES['images']['tmp_name']; $errs = $_FILES['images']['error']; $sizes = $_FILES['images']['size']; $count = count($names); if ($count > $max_files_bulk) { $errors[] = "Zyada files select kiye gaye. Max allowed: {$max_files_bulk}"; } else { for ($i = 0; $i < $count; $i++) { if ($errs[$i] === UPLOAD_ERR_NO_FILE) continue; if ($errs[$i] !== UPLOAD_ERR_OK) { $results[] = ['status' => 'error', 'file' => $names[$i], 'msg' => "Upload error code: " . $errs[$i]]; continue; } if ($sizes[$i] > $max_file_size) { $results[] = ['status' => 'error', 'file' => $names[$i], 'msg' => "File too large"]; continue; } $ext = strtolower(pathinfo($names[$i], PATHINFO_EXTENSION)); if (!in_array($ext, $allowed_ext, true)) { $results[] = ['status' => 'error', 'file' => $names[$i], 'msg' => "Invalid file type"]; continue; } $new_name = safe_unique_name('img', $ext); $dest = rtrim($abs_dir, '/\\') . '/' . $new_name; if (move_uploaded_file($tmps[$i], $dest)) { $web_path = $relative_dir . '/' . $new_name; $entry = ['status' => 'ok', 'file' => $names[$i], 'path' => $web_path, 'abs' => $dest, 'size' => $sizes[$i]]; // optional DB insert if table exists if ($pdo) { try { $check = $pdo->query("SHOW TABLES LIKE 'shayari_images'")->fetch(); if ($check) { $ins = $pdo->prepare("INSERT INTO shayari_images (category, folder, filename, relative_path, abs_path, size, created_at) VALUES (:category,:folder,:filename,:relative_path,:abs_path,:size,NOW())"); $ins->execute([ ':category' => $category_key, ':folder' => $relative_dir, ':filename' => $new_name, ':relative_path' => $web_path, ':abs_path' => $dest, ':size' => $sizes[$i], ]); $entry['db_id'] = (int)$pdo->lastInsertId(); } } catch (Exception $ex) { error_log("DB insert failed for $new_name: " . $ex->getMessage()); } } $results[] = $entry; } else { $results[] = ['status' => 'error', 'file' => $names[$i], 'msg' => "move_uploaded_file failed"]; } } } } // ---- HANDLE ZIP upload (input name="zipfile") ---- if (!empty($_FILES['zipfile']) && ($_FILES['zipfile']['error'] ?? UPLOAD_ERR_NO_FILE) !== UPLOAD_ERR_NO_FILE) { $zf = $_FILES['zipfile']; if ($zf['error'] !== UPLOAD_ERR_OK) { $errors[] = "ZIP upload error code: " . $zf['error']; } elseif ($zf['size'] > 50 * 1024 * 1024) { // safety limit for zip; adjust $errors[] = "ZIP file bahut badi hai (max 50MB)."; } else { if (!class_exists('ZipArchive')) { $errors[] = "ZipArchive extension PHP me enabled nahi hai."; } else { $zip = new ZipArchive(); $openRes = $zip->open($zf['tmp_name']); if ($openRes === true) { // iterate entries for ($i = 0; $i < $zip->numFiles; $i++) { $stat = $zip->statIndex($i); $name = $stat['name']; // skip directories if (substr($name, -1) === '/') continue; // sanitize entry name and extension $ext = strtolower(pathinfo($name, PATHINFO_EXTENSION)); if (!in_array($ext, $allowed_ext, true)) { $results[] = ['status' => 'skipped', 'file' => $name, 'msg' => 'Not an allowed image type']; continue; } // read file stream and save $stream = $zip->getStream($name); if (!$stream) { $results[] = ['status' => 'error', 'file' => $name, 'msg' => 'Cannot read zip entry']; continue; } $new_name = safe_unique_name('img', $ext); $dest = rtrim($abs_dir, '/\\') . '/' . $new_name; $out = fopen($dest, 'w'); if (!$out) { fclose($stream); $results[] = ['status' => 'error', 'file' => $name, 'msg' => 'Cannot create destination file']; continue; } while (!feof($stream)) { $chunk = fread($stream, 1024 * 8); fwrite($out, $chunk); } fclose($stream); fclose($out); $web_path = $relative_dir . '/' . $new_name; $entry = ['status' => 'ok', 'file' => $name, 'path' => $web_path, 'abs' => $dest]; // optional DB insert if ($pdo) { try { $check = $pdo->query("SHOW TABLES LIKE 'shayari_images'")->fetch(); if ($check) { $ins = $pdo->prepare("INSERT INTO shayari_images (category, folder, filename, relative_path, abs_path, size, created_at) VALUES (:category,:folder,:filename,:relative_path,:abs_path,:size,NOW())"); $filesize = filesize($dest); $ins->execute([ ':category' => $category_key, ':folder' => $relative_dir, ':filename' => $new_name, ':relative_path' => $web_path, ':abs_path' => $dest, ':size' => $filesize, ]); $entry['db_id'] = (int)$pdo->lastInsertId(); $entry['size'] = $filesize; } } catch (Exception $ex) { error_log("DB insert failed for zip entry $new_name: " . $ex->getMessage()); } } $results[] = $entry; } $zip->close(); } else { $errors[] = "ZIP open failed (code: $openRes)"; } } } } // final success message if at least one ok $okCount = 0; foreach ($results as $r) if (!empty($r['status']) && $r['status'] === 'ok') $okCount++; if ($okCount > 0) { $success = "Total uploaded images: {$okCount}"; } elseif (empty($errors) && empty($results)) { $errors[] = "Kuch bhi upload nahi hua. Ensure you selected files or zip."; } } ?> <!doctype html> <html lang="en"> <head> <meta charset="utf-8"> <title>Bulk Category Image Uploader</title> <style> body{font-family:system-ui,Segoe UI,Roboto,Arial; background:#f7f7f8; margin:0; padding:20px} .wrap{max-width:900px;margin:20px auto;background:#fff;padding:18px;border-radius:8px;box-shadow:0 6px 18px rgba(0,0,0,.06)} h1{margin-top:0} .row{margin-bottom:12px} label{display:block;font-weight:600;margin-bottom:6px} input[type="file"]{display:block} .err{background:#ffe8e8;border:1px solid #f2b8b8;padding:10px;border-radius:6px;margin-bottom:12px} .ok{background:#e9fff0;border:1px solid #bfeecf;padding:10px;border-radius:6px;margin-bottom:12px} table{width:100%;border-collapse:collapse;margin-top:12px} th,td{padding:8px;border:1px solid #eee;font-size:13px;text-align:left} .small{font-size:13px;color:#666} .preview{max-width:120px;border-radius:6px} </style> </head> <body> <div class="wrap"> <h1>Bulk Category Image Uploader</h1> <?php if ($errors): ?> <div class="err"> <strong>Errors:</strong> <ul> <?php foreach ($errors as $e): ?> <li><?php echo htmlspecialchars($e); ?></li> <?php endforeach; ?> </ul> </div> <?php endif; ?> <?php if ($success): ?> <div class="ok"><strong><?php echo htmlspecialchars($success); ?></strong></div> <?php endif; ?> <form method="post" enctype="multipart/form-data"> <div class="row"> <label for="category">Category</label> <select name="category" id="category"> <?php foreach ($category_map as $k => $v): ?> <option value="<?php echo htmlspecialchars($k); ?>"><?php echo htmlspecialchars($k . ' → ' . $v); ?></option> <?php endforeach; ?> </select> </div> <div class="row"> <label for="images">Select multiple images (Ctrl/Cmd + click)</label> <input type="file" name="images[]" id="images" accept="image/*" multiple> <div class="small">Allowed: <?php echo implode(', ', $allowed_ext); ?> — Max per file: <?php echo ($max_file_size/1024/1024); ?> MB</div> </div> <div style="margin:12px 0; font-weight:600; text-align:center">OR</div> <div class="row"> <label for="zipfile">Upload ZIP (will extract images)</label> <input type="file" name="zipfile" id="zipfile" accept=".zip,application/zip"> <div class="small">ZIP should contain image files. ZIP max ~50MB (adjust in code).</div> </div> <div class="row"> <button type="submit">Upload</button> </div> </form> <?php if (!empty($results)): ?> <h3>Results</h3> <table> <thead><tr><th>File</th><th>Status</th><th>Path / Info</th><th>Preview</th></tr></thead> <tbody> <?php foreach ($results as $r): ?> <tr> <td><?php echo htmlspecialchars($r['file'] ?? ''); ?></td> <td><?php echo htmlspecialchars($r['status']); ?></td> <td> <?php if (!empty($r['path'])): ?> <code><?php echo htmlspecialchars($r['path']); ?></code> <?php if (!empty($r['db_id'])): ?> <div class="small">db_id: <?php echo (int)$r['db_id']; ?></div><?php endif; ?> <?php else: ?> <?php echo htmlspecialchars($r['msg'] ?? ''); ?> <?php endif; ?> </td> <td> <?php if (!empty($r['path']) && preg_match('/\.(jpg|jpeg|png|webp|gif)$/i', $r['path'])): ?> <img class="preview" src="<?php echo htmlspecialchars($r['path']); ?>" alt=""> <?php endif; ?> </td> </tr> <?php endforeach; ?> </tbody> </table> <?php endif; ?> <hr> <div class="small"> <strong>Notes:</strong> <ul> <li>ZIP extraction requires PHP ZipArchive extension.</li> <li>Adjust <code>$max_file_size</code>, <code>$max_files_bulk</code>, and ZIP size checks in code as needed.</li> <li>Large bulk upload may take time; consider increasing <code>max_execution_time</code> if you expect many files.</li> <li>If DB insert is required, create <code>shayari_images</code> table with columns: <code>id, category, folder, filename, relative_path, abs_path, size, created_at</code>.</li> </ul> </div> </div> </body> </html>