PHP में multiple files upload करना और उन्हें server पर store करने का पूरा गाइड।
HTML Form (आवश्यक Attributes)
<form action="upload.php" method="POST" enctype="multipart/form-data">
<input type="file" name="files[]" id="files" multiple accept="image/*,.pdf,.doc">
<button type="submit">अपलोड करें</button>
</form>
महत्वपूर्ण HTML Attributes:
enctype="multipart/form-data"- फाइल अपलोड के लिए आवश्यकname="files[]"- ब्रैकेट्स [] array बनाते हैंmultiple- multiple file selection सक्षम करता हैaccept- फाइल प्रकार प्रतिबंधित करता है (वैकल्पिक)
PHP Processing Script (upload.php)
<?php
// कॉन्फ़िगरेशन
$uploadDir = __DIR__ . '/uploads/';
$allowedTypes = ['image/jpeg', 'image/png', 'image/gif', 'application/pdf'];
$maxFileSize = 5 * 1024 * 1024; // 5MB
$maxFiles = 10;
// अपलोड डायरेक्टरी बनाएं
if (!is_dir($uploadDir)) {
mkdir($uploadDir, 0755, true);
}
$results = ['success' => [], 'errors' => []];
// खाली entries फ़िल्टर करें
$fileNames = array_filter($_FILES['files']['name']);
$totalFiles = count($fileNames);
if ($totalFiles === 0) {
die(json_encode(['error' => 'कोई फाइल चुनी नहीं गई']));
}
if ($totalFiles > $maxFiles) {
die(json_encode(['error' => "अधिकतम $maxFiles फाइलें अपलोड कर सकते हैं"]));
}
for ($i = 0; $i < $totalFiles; $i++) {
$name = $_FILES['files']['name'][$i];
$tmpName = $_FILES['files']['tmp_name'][$i];
$size = $_FILES['files']['size'][$i];
$type = $_FILES['files']['type'][$i];
$error = $_FILES['files']['error'][$i];
// त्रुटि जांच
if ($error !== UPLOAD_ERR_OK) {
$results['errors'][] = "$name: अपलोड त्रुटि कोड $error";
continue;
}
// फाइल साइज जांच
if ($size > $maxFileSize) {
$results['errors'][] = "$name: फाइल बहुत बड़ी (अधिकतम " . ($maxFileSize/1024/1024) . "MB)";
continue;
}
// MIME type जांच
$finfo = finfo_open(FILEINFO_MIME_TYPE);
$realType = finfo_file($finfo, $tmpName);
finfo_close($finfo);
if (!in_array($realType, $allowedTypes)) {
$results['errors'][] = "$name: अनुमत फाइल प्रकार नहीं ($realType)";
continue;
}
// सुरक्षित फाइलनाम जनरेट करें
$ext = pathinfo($name, PATHINFO_EXTENSION);
$safeName = bin2hex(random_bytes(8)) . '.' . $ext;
$destPath = $uploadDir . $safeName;
// फाइल मूव करें
if (move_uploaded_file($tmpName, $destPath)) {
$results['success'][] = [
'original' => $name,
'saved_as' => $safeName,
'size' => $size,
'type' => $realType
];
} else {
$results['errors'][] = "$name: सर्वर पर सेव करने में विफल";
}
}
// JSON प्रतिक्रिया दें
header('Content-Type: application/json');
echo json_encode($results);
?>
Client-Side JavaScript (AJAX Upload)
<script>
document.getElementById('files').addEventListener('change', function(e) {
const files = e.target.files;
const formData = new FormData();
for (let file of files) {
formData.append('files[]', file);
}
fetch('upload.php', {
method: 'POST',
body: formData
})
.then(res => res.json())
.then(data => {
console.log('सफल:', data.success);
console.log('त्रुटियां:', data.errors);
});
});
</script>
सुरक्षा चेकलिस्ट
- ✅
finfo_file()से वास्तविक MIME type वैलिडेट करें - ✅ फाइल एक्सटेंशन के बजाय content-based validation
- ✅ Random filenames जनरेट करें (collision और security के लिए)
- ✅ फाइल साइज सीमा लागू करें
- ✅ अपलोड डायरेक्टरी को web root से बाहर रखें या .htaccess से execution ब्लॉक करें
- ✅
move_uploaded_file()उपयोग करें (नहींcopy()याrename())
.htaccess सुरक्षा (अपलोड फोल्डर में)
# PHP execution रोकें
<Files *.php>
Order Allow,Deny
Deny from all
</Files>
# या
SetHandler default-handler
Options -ExecCGI