-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathupload.php
More file actions
92 lines (76 loc) · 2.71 KB
/
Copy pathupload.php
File metadata and controls
92 lines (76 loc) · 2.71 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
<?php
declare(strict_types=1);
require_once __DIR__ . '/helpers.php';
requireLogin(false);
header('Content-Type: application/json; charset=utf-8');
// Solo POST
if ($_SERVER['REQUEST_METHOD'] !== 'POST') {
http_response_code(405);
echo json_encode(['ok' => false, 'error' => 'Método no permitido']);
exit;
}
// Validar CSRF
$csrf = (string)($_POST['csrf'] ?? '');
if (!csrf_valid($csrf)) {
http_response_code(400);
echo json_encode(['ok' => false, 'error' => 'CSRF inválido']);
exit;
}
$user = (string)($_SESSION['user'] ?? '');
if (session_status() === PHP_SESSION_ACTIVE) session_write_close();
// Comprobar que llegó un fichero
if (empty($_FILES['file'])) {
echo json_encode(['ok' => false, 'error' => 'Sin archivo']);
exit;
}
$err = (int)($_FILES['file']['error'] ?? UPLOAD_ERR_OK);
if ($err !== UPLOAD_ERR_OK) {
$errMsg = match ($err) {
UPLOAD_ERR_INI_SIZE, UPLOAD_ERR_FORM_SIZE => 'El archivo supera el tamaño máximo permitido',
UPLOAD_ERR_PARTIAL => 'La subida fue interrumpida',
UPLOAD_ERR_NO_FILE => 'No se seleccionó ningún archivo',
default => "Error de subida (código $err)",
};
echo json_encode(['ok' => false, 'error' => $errMsg]);
exit;
}
$srcTmp = (string)$_FILES['file']['tmp_name'];
$origRaw = (string)$_FILES['file']['name'];
// Sanitizar nombre
$orig = sanitizeFileName($origRaw);
$ext = strtolower(pathinfo($orig, PATHINFO_EXTENSION));
// Bloquear extensiones ejecutables en el servidor
$blockedExts = ['php', 'phtml', 'phar', 'php3', 'php4', 'php5', 'php7', 'shtml', 'cgi', 'pl', 'py', 'rb', 'sh', 'bat', 'exe'];
if (in_array($ext, $blockedExts, true)) {
$orig .= '.txt';
$ext = 'txt';
}
// Límite de tamaño: 10 GB (el límite real lo impone php.ini — ver .htaccess)
$maxBytes = 10 * 1024 * 1024 * 1024;
$fileSize = (int)$_FILES['file']['size'];
if ($fileSize > $maxBytes) {
echo json_encode(['ok' => false, 'error' => 'El archivo supera los 10 GB']);
exit;
}
// Directorio del usuario
$dir = ensureUserDir($user);
$destName = $orig;
$destPath = $dir . DIRECTORY_SEPARATOR . $destName;
$base = pathinfo($orig, PATHINFO_FILENAME);
$extDot = $ext ? ('.' . $ext) : '';
$idx = 1;
// Evitar sobreescribir (renombrar si ya existe)
while (is_file($destPath)) {
$destName = $base . ' (' . $idx . ')' . $extDot;
$destPath = $dir . DIRECTORY_SEPARATOR . $destName;
$idx++;
}
if (!move_uploaded_file($srcTmp, $destPath)) {
echo json_encode(['ok' => false, 'error' => 'No se pudo mover el archivo']);
exit;
}
echo json_encode([
'ok' => true,
'name' => $destName,
'size' => @filesize($destPath) ?: 0,
]);