Files
wevads-platform/scripts/hamid-files.php
2026-02-26 04:53:11 +01:00

62 lines
1.8 KiB
PHP
Executable File

<?php
header('Content-Type: application/json');
$file = $_FILES['file'] ?? null;
$action = $_POST['action'] ?? 'analyze';
if (!$file || $file['error'] !== UPLOAD_ERR_OK) {
echo json_encode(['error' => 'No file uploaded']);
exit;
}
$ext = strtolower(pathinfo($file['name'], PATHINFO_EXTENSION));
$tmpPath = $file['tmp_name'];
$content = '';
switch ($ext) {
case 'txt':
case 'md':
case 'csv':
$content = file_get_contents($tmpPath);
break;
case 'pdf':
// Utiliser pdftotext si disponible, sinon Python
exec("pdftotext '$tmpPath' - 2>/dev/null", $out);
if (empty($out)) {
exec("python3 -c \"import PyPDF2; f=open('$tmpPath','rb'); r=PyPDF2.PdfReader(f); print(''.join(p.extract_text() or '' for p in r.pages[:10]))\" 2>/dev/null", $out);
}
$content = implode("\n", $out);
break;
case 'docx':
exec("python3 -c \"from docx import Document; d=Document('$tmpPath'); print('\\n'.join(p.text for p in d.paragraphs[:100]))\" 2>/dev/null", $out);
$content = implode("\n", $out);
break;
case 'xlsx':
case 'xls':
exec("python3 -c \"import pandas as pd; df=pd.read_excel('$tmpPath'); print(df.head(50).to_string())\" 2>/dev/null", $out);
$content = implode("\n", $out);
break;
case 'json':
$content = json_encode(json_decode(file_get_contents($tmpPath)), JSON_PRETTY_PRINT);
break;
default:
$content = file_get_contents($tmpPath);
}
$content = substr($content, 0, 10000); // Limite 10K chars
echo json_encode([
'success' => true,
'filename' => $file['name'],
'type' => $ext,
'size' => $file['size'],
'content' => $content,
'chars' => strlen($content)
]);