14, // New ID 'libros' => 15, // New ID 'fotos_conchita' => 16, 'miguel_angel_martin' => 17, '1jornadafa' => 18, 'headers' => 19, 'sampledata' => 20 ]; global $wpdb; $fbv_table = $wpdb->prefix . 'fbv'; $fbv_att_table = $wpdb->prefix . 'fbv_attachment_folder'; foreach ($folders as $folder_name => $folder_id) { // Create folder if not exists $exists = $wpdb->get_var($wpdb->prepare("SELECT id FROM $fbv_table WHERE name = %s", $folder_name)); if (!$exists) { $wpdb->query($wpdb->prepare("INSERT INTO $fbv_table (name) VALUES (%s)", $folder_name)); $folder_id = $wpdb->insert_id; echo "Created folder: $folder_name (ID: $folder_id)\n"; } else { $folder_id = $exists; echo "Folder exists: $folder_name (ID: $folder_id)\n"; } $base_dir = '/var/www/html/wp-content/uploads/' . $folder_name; if (!is_dir($base_dir)) { echo "Dir not found: $base_dir\n"; continue; } $files = scandir($base_dir); $count = 0; foreach ($files as $file) { if ($file === '.' || $file === '..') continue; $filepath = $base_dir . '/' . $file; if (is_dir($filepath)) continue; // Check if exists $guid_search = '%' . $folder_name . '/' . $file; $att_id = $wpdb->get_var($wpdb->prepare("SELECT ID FROM $wpdb->posts WHERE guid LIKE %s AND post_type = 'attachment'", $guid_search)); if (!$att_id) { $cmd = "wp media import " . escapeshellarg($filepath) . " --porcelain --allow-root"; $output = shell_exec($cmd); $att_id = trim($output); if (is_numeric($att_id)) { $wpdb->query($wpdb->prepare("INSERT INTO $fbv_att_table (folder_id, attachment_id) VALUES (%d, %d)", $folder_id, $att_id)); $count++; } } } echo "Imported $count files to $folder_name\n"; } echo "ALL DONE\n";