PHP $_FILES
In this page:
$_FILES["field_name"]["name"]; // original file name
$_FILES["field_name"]["type"]; // MIME type
$_FILES["field_name"]["size"]; // bytes
$_FILES["field_name"]["tmp_name"]; // temporary path
$_FILES["field_name"]["error"]; // UPLOAD_ERR_OK (0) means success
File Name और Type Access करना
$_FILES superglobal एक two-dimensional associative array है — किसी uploaded file के original name या MIME type को access करने के लिए, $_FILES[input_name][name] और $_FILES[input_name][type] इस्तेमाल करें।
उदाहरण: Accessing File Name and Type
<?php
// Set `$_FILES['avatar']` to `['name' => 'pic.png', 'type' => 'image/png']`
$_FILES['avatar'] = ['name' => 'pic.png', 'type' => 'image/png'];
// Print `$_FILES['avatar']['name'] . " - " . $_FILES['avatar']['type']` to the output
echo $_FILES['avatar']['name'] . " - " . $_FILES['avatar']['type'];
?>
Login to try C/C++/Java/PHP code in the editor
Upload Sizes Check करना
$_FILES में size key bytes में file की size रखती है, और file को एक permanent directory में move करने से पहले अपने project की limits के against इसे check करना oversized uploads पर disk space बर्बाद करने से बचाता है।
उदाहरण: Checking Upload Sizes
<?php
$_FILES['avatar']['size'] = 3000000;
// Declare `$maxSize`, set to `2000000`
$maxSize = 2000000;
// Check whether `$_FILES['avatar']['size'] > $maxSize`
if ($_FILES['avatar']['size'] > $maxSize) {
// Print "File too large" to the output
echo "File too large";
}
?>
Login to try C/C++/Java/PHP code in the editor
Upload Errors Identify करना
$_FILES के अंदर error key एक numeric code रखती है जैसे UPLOAD_ERR_OK (0) — किसी भी दूसरी metadata को छूने से पहले हमेशा पहले यह value check करें, यह confirm करने के लिए कि upload actually succeed हुआ।
उदाहरण: Identifying Upload Errors
<?php
$_FILES['avatar']['error'] = UPLOAD_ERR_OK;
// Check whether `$_FILES['avatar']['error'] === UPLOAD_ERR_OK`
if ($_FILES['avatar']['error'] === UPLOAD_ERR_OK) {
// Print "Upload OK, check other metadata now" to the output
echo "Upload OK, check other metadata now";
}
?>
Login to try C/C++/Java/PHP code in the editor
Temporary Paths Retrieve करना
जब एक file upload होती है, PHP इसे एक temporary directory में store करता है, इसका path tmp_name key के तहत saved होते हुए — वह temporary path वह है जो आप move_uploaded_file() को pass करते हैं या validation के लिए इस्तेमाल करते हैं।
उदाहरण: Retrieving Temporary Paths
<?php
$_FILES['avatar']['tmp_name'] = '/tmp/phpABC123';
// Print `"Temp path: " . $_FILES['avatar']['tmp_name']` to the output
echo "Temp path: " . $_FILES['avatar']['tmp_name'];
?>
Login to try C/C++/Java/PHP code in the editor
कई File Uploads Process करना
अगर एक form array-style input names (जैसे files[]) इस्तेमाल करके कई file uploads support करता है, $_FILES single strings के बजाय values के nested arrays return करता है, इसलिए हर file को individually process करने के लिए आप indices पर loop चलाते हैं।
उदाहरण: Processing Multiple File Uploads
<?php
// Set `$_FILES['files']` to `[`
$_FILES['files'] = [
'name' => ['a.jpg', 'b.jpg'],
'size' => [1000, 2000],
];
// Loop over `$_FILES['files']['name']`, binding each item to `$index => $name`
foreach ($_FILES['files']['name'] as $index => $name) {
// Print "$name: " . $_FILES['files']['size'][$index] . " bytes\n" to the output
echo "$name: " . $_FILES['files']['size'][$index] . " bytes\n";
}
?>
Login to try C/C++/Java/PHP code in the editor
- upload इस्तेमाल करने से पहले
$_FILES[f][error]check न करना। $_FILES[f][type]पर भरोसा करना, जिसे client control करता है।- request के बाद
$_FILES[f][tmp_name]इस्तेमाल करना, जबकि file अंत में delete हो जाती है।
Chapter Quiz — Complete all 8 topics to unlock
0/8 topics done
Complete these topics first: