← Back to PHP Course | Chapter 9: File Handling | Lesson 4 of 8

PHP File Upload

एक file upload किसी visitor के आपको एक form के through एक photo सौंपने जैसा है। PHP इसे एक temporary spot में receive करता है, इसे check करता है, और फिर इसे एक permanent home में move करता है।
Syntax
php
$_FILES["field_name"]["name"];
$_FILES["field_name"]["tmp_name"];
$_FILES["field_name"]["size"];

move_uploaded_file($_FILES["field_name"]["tmp_name"], "uploads/target_name");

$_FILES Array

जब कोई file एक HTML form के through आपके server पर upload होती है, PHP उस file के बारे में सारी information $_FILES superglobal array के अंदर store करता है, उसका original name, MIME type, size, और temporary server location सहित।

उदाहरण: The $_FILES Array

php
<?php
// Set `$_FILES['photo']` to `[`
$_FILES['photo'] = [
    'name' => 'profile.jpg',
    'type' => 'image/jpeg',
    'size' => 24576,
    'tmp_name' => '/tmp/phpXXXXXX',
];
// Print `$_FILES['photo']['name'] . " (" . $_FILES['photo']['type'] . ")"` to the output
echo $_FILES['photo']['name'] . " (" . $_FILES['photo']['type'] . ")";
?>

Uploaded Files को Move करना

Files शुरुआत में server पर एक temporary folder में रखी जाती हैं। उन्हें permanently रखने के लिए, आपको move_uploaded_file() इस्तेमाल करके उन्हें अपने target directory में move करना ज़रूरी है, जो यह भी verify करता है कि file genuinely एक HTTP upload से आई थी।

उदाहरण: Moving Uploaded Files

php
<?php
// Simulated for demonstration -- in real use, $_FILES['photo']['tmp_name'] comes from an actual upload
$tmpPath = "data.txt";
file_put_contents($tmpPath, "uploaded content");
$destination = "uploads/photo.txt";
echo "Would move $tmpPath to $destination via move_uploaded_file()";
?>

File Types Verify करना

browser द्वारा report किए गए file extension या MIME type पर कभी भरोसा न करें, क्योंकि दोनों spoof करना आसान है — किसी upload को safe मानने से पहले actual file contents validate करें (उदाहरण के लिए, getimagesize() से image files check करना)।

उदाहरण: Verifying File Types

php
<?php
$_FILES['photo']['name'] = "photo.jpg.php"; // spoofed extension
$ext = pathinfo($_FILES['photo']['name'], PATHINFO_EXTENSION);
echo "Reported extension: $ext -- never trust this alone, verify contents instead";
?>

File Size Restrict करना

बहुत बड़ी files upload करना server space और memory consume कर सकता है, इसलिए हमेशा अपने project की limits के against $_FILES में size key check करें, और यह भी सुनिश्चित करें कि आपकी php.ini upload_max_filesize setting भी आपकी intended limit से match करती हो।

उदाहरण: Restricting File Size

php
<?php
$_FILES['photo']['size'] = 5000000;
// Declare `$maxSize`, set to `2000000`
$maxSize = 2000000;
// Check whether `$_FILES['photo']['size'] > $maxSize`
if ($_FILES['photo']['size'] > $maxSize) {
    // Print "File too large" to the output
    echo "File too large";
// Otherwise, run this branch
} else {
    // Print "File size OK" to the output
    echo "File size OK";
}
?>

Upload Errors Handle करना

$_FILES के अंदर error key एक numeric error code रखती है — 0 (UPLOAD_ERR_OK) success का मतलब है, जबकि दूसरी values php.ini में configured size limits से आगे निकलने जैसी specific problems की ओर point करती हैं।

उदाहरण: Handling Upload Errors

php
<?php
$_FILES['photo']['error'] = UPLOAD_ERR_OK;
// Check whether `$_FILES['photo']['error'] === UPLOAD_ERR_OK`
if ($_FILES['photo']['error'] === UPLOAD_ERR_OK) {
    // Print "Upload succeeded" to the output
    echo "Upload succeeded";
// Otherwise, run this branch
} else {
    // Print `"Upload failed with error code " . $_FILES['photo']['error']` to the output
    echo "Upload failed with error code " . $_FILES['photo']['error'];
}
?>
Related Topics
{# common_mistakes/chapter_summary/browser_support: on Hindi pages the view already swaps in the hi_ translation fields (or blanks these out if untranslated), so this renders correctly for both languages without a lang_code check here. #}
आम गलतियां
  1. file move करने से पहले $_FILES[file][error] check न करना।
  2. server पर check करने के बजाय client-provided file type पर भरोसा करना।
  3. form पर enctype="multipart/form-data" भूल जाना, ताकि कोई file न भेजी जाए।
🔒

Chapter Quiz — Complete all 8 topics to unlock

0/8 topics done

Complete these topics first:

Login to run this code

C/C++/Java/PHP execution requires a free account. Your code is saved — you'll land right back in the editor after logging in.