prefix . 'fbv_attachment_folder'; // Define paths $uploads = wp_upload_dir(); $base_dir = $uploads['basedir']; $source_dir = $base_dir . '/' . $target_folder_name; if (!is_dir($source_dir)) { die("Error: Source directory '$source_dir' not found.\n"); } // Get all files $files = glob($source_dir . '/*'); echo "Found " . count($files) . " files in '$target_folder_name'.\n"; $count_imported = 0; foreach ($files as $file) { if (is_dir($file)) continue; $ext = strtolower(pathinfo($file, PATHINFO_EXTENSION)); if (in_array($ext, ['html', 'php', 'ds_store'])) continue; $filename = basename($file); // echo "Processing: $filename... "; // Run wp media import via shell_exec using FULL PATH // Note: Since we are running as www-data via docker exec, wp should be executable. // We add --porcelain to get just the ID. // We add --allow-root because in some contexts inside docker it might be needed, though we run as www-data. // Actually, running 'wp' inside 'wp eval-file' might be tricky due to recursion locks or environment. // But let's try. $cmd = "/usr/local/bin/wp media import " . escapeshellarg($file) . " --porcelain --allow-root 2>&1"; $output = shell_exec($cmd); $attachment_id = trim($output); if (is_numeric($attachment_id) && $attachment_id > 0) { // Success! Now assign to FileBird folder $wpdb->insert( $fbv_rel_table, array( 'folder_id' => $fbv_folder_id, 'attachment_id' => $attachment_id ) ); // echo "Imported (ID: $attachment_id) & Assigned.\n"; $count_imported++; } else { echo "Failed to import $filename. Output: $output\n"; } } echo "------------------------------------------------\n"; echo "Total imported and assigned to folder '$target_folder_name' (ID: $fbv_folder_id): $count_imported\n"; ?>