WhatsApp Backup → Google Drive + Sheet
📱 WhatsApp Chat Backup Tool
Upload your exported WhatsApp ZIP (with media) and automatically save:
- ✅ Chat messages → Google Sheet
- ✅ Photos/Videos → Google Drive Folder
false, "error" => "No file uploaded"]);
exit;
}
$zipFile = $_FILES['zipFile']['tmp_name'];
$zip = new ZipArchive();
if ($zip->open($zipFile) === TRUE) {
$zip->extractTo($tmpDir);
$zip->close();
} else {
echo json_encode(["ok" => false, "error" => "Failed to unzip file"]);
exit;
}
$chatRows = [];
$regex = '/^(\d{1,2}\/\d{1,2}\/\d{2,4}), (\d{1,2}:\d{2}) ?([ap]m)? - ([^:]+): (.*)$/';
$msgCount = 0; $mediaCount = 0;
// ✅ Parse files
$rii = new RecursiveIteratorIterator(new RecursiveDirectoryIterator($tmpDir));
foreach ($rii as $file) {
if ($file->isDir()) continue;
$name = $file->getFilename();
if (preg_match('/\.txt$/i', $name)) {
$lines = file($file->getPathname(), FILE_IGNORE_NEW_LINES);
foreach ($lines as $line) {
if (preg_match($regex, $line, $m)) {
[$_, $date, $time, $ampm, $sender, $msg] = $m;
$chatRows[] = [$date, $time.($ampm?" ".$ampm:""), $sender, $msg, '', ''];
$msgCount++;
}
}
} else {
// ✅ Upload file to Drive (public)
$curl = curl_init();
$meta = [
'name' => $name,
'parents' => [$DRIVE_FOLDER_ID]
];
$boundary = uniqid();
$delimiter = '-------------' . $boundary;
$postData = "--$delimiter\r\n" .
"Content-Type: application/json; charset=UTF-8\r\n\r\n" .
json_encode($meta) . "\r\n" .
"--$delimiter\r\n" .
"Content-Type: " . mime_content_type($file->getPathname()) . "\r\n\r\n" .
file_get_contents($file->getPathname()) . "\r\n" .
"--$delimiter--";
curl_setopt_array($curl, [
CURLOPT_URL => "https://www.googleapis.com/upload/drive/v3/files?uploadType=multipart",
CURLOPT_RETURNTRANSFER => true,
CURLOPT_CUSTOMREQUEST => "POST",
CURLOPT_POSTFIELDS => $postData,
CURLOPT_HTTPHEADER => [
"Authorization: Bearer AIzaSyBHRB3AbKsBrwnOIaAfkBv2P5nJr9vY8Vk", // ⚠️ Replace with valid token
"Content-Type: multipart/related; boundary=$delimiter"
],
]);
$response = curl_exec($curl);
curl_close($curl);
$res = json_decode($response, true);
$driveUrl = isset($res['id']) ? "https://drive.google.com/file/d/".$res['id']."/view" : "";
$chatRows[] = ['', '', '', '', $name, $driveUrl];
$mediaCount++;
}
}
// ✅ Save to Google Sheet (using Sheets API)
$sheetUrl = "https://sheets.googleapis.com/v4/spreadsheets/$SHEET_ID/values/ChatData!A1:append?valueInputOption=USER_ENTERED";
$values = array_merge([['Date','Time','Sender','Message','Media File','Drive Link']], $chatRows);
$body = json_encode(['values'=>$values]);
$ch = curl_init();
curl_setopt($ch, CURLOPT_URL, $sheetUrl);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
curl_setopt($ch, CURLOPT_POST, true);
curl_setopt($ch, CURLOPT_HTTPHEADER, [
'Content-Type: application/json',
'Authorization: Bearer AIzaSyBHRB3AbKsBrwnOIaAfkBv2P5nJr9vY8Vk' // ⚠️ Replace with valid Google OAuth token
]);
curl_setopt($ch, CURLOPT_POSTFIELDS, $body);
curl_exec($ch);
curl_close($ch);
echo json_encode([
"ok" => true,
"msg" => "✅ Upload Complete!\nMessages: $msgCount\nMedia Files: $mediaCount"
]);
?>