PHP File Uploading In Hindi

📔 : PHP 🔗

अभी तक हमने Form से normal input data handle किया था , इस topic में हम PHP में File Uploading भी सीखेंगे कि कैसे file को upload , rename करते हैं।

PHP में आप easily file upload कर सकते हैं , साथ साथ ये भी देखेंगे कि कैसे uploaded file को validate करते हैं file के type के according .


PHP में File upload करने के लिए Form Method 'post' ही होना चाहिए , 'get' method के साथ आप file upload नहीं कर सकते हैं। इसके अलावा Form tag में एक और attribute add करना होता है enctype="multipart/form-data" .

PHP File Uploading Example

PHP में File को handle करने के लिए हम $_FILES Super Global Variable का use करते हैं। क्योंकि file को हम $_GET , $_POST या $_REQUEST से handle नहीं कर सकते हैं। PHP , File Input Field के name का Associative Array create करता है  जिसमें  File से related info contain करता है जिसमे file का actual name , file type , ile size etc .. information होती है जिससे हम uploading के समय ही file rename करके save करें।

File : form.html

Copy Fullscreen Close Fullscreen
<!DOCTYPE html>
<html>
<head>
<title>PHP FileUploading</title>
</head>
<body>
<form action="file_upload.php" method="post" enctype="multipart/form-data">
<p>Upload File : <input type="file" accept="image/*" name="myimage" placeholder="Upload File" /></p>
<p><button type="submit">Submit</button></p>
</form>
</body>
</html>

Another File where we upload file : file_upload.php

Copy Fullscreen Close Fullscreen
<?php  
/* first check file */
if(isset($_FILES['myimage']['name']))
{
echo 'File Info : <pre>';
print_r($_FILES['myimage']);
echo '</pre>';
/* before upload the file we should rename it */
$tmp = $_FILES["myimage"]["tmp_name"];
$extension = explode("/", $_FILES["myimage"]["type"]);
$new_name = time().".".$extension[1];

if(move_uploaded_file($tmp, $new_name))
echo "Your file is uploaded with ".$new_name." name";
else
echo 'Error In Uploading';
}
else
{
echo 'File Upload Field Is Required';
}
?>
Output
File Info :
Array
(
    [name] => 1.png
    [type] => image/png
    [tmp_name] => C:\xampp\tmp\phpCA27.tmp
    [error] => 0
    [size] => 4913
)
Your file is uploaded with 1600055903.png name

PHP Multiple File Uploading

चूंकि जब हम single file upload करते हैं तो file field नाम का One Dimensional Array मिलता है और उसी field name में जब हम multiple files upload करते हैं तो हमें Two Dimensional Array मिलता है जिसे हम Foreach Loop का use करके आसानी एक - एक करके file को upload कर सकते हैं। इसके अलावा Multiple File करने के लिए हमें file input tag में 'multiple' attribute add करना पड़ता है।और filed name को as a Array variable define ( myimage[] ) करना पड़ता है। 

See Example

File : multi_form.html

CopyFullscreen Close Fullscreen
<!DOCTYPE html>
<html>
<head>
<title>PHP FileUploading</title>
</head>
<body>
<form action="multi_file_upload.php" method="post" enctype="multipart/form-data">
<p>Upload File : <input type="file" accept="image/*" name="myimage[]" placeholder="Upload File" multiple="multiple" /></p>
<p><button type="submit">Submit</button></p>
</form>
</body>
</html>

Another File where we upload files : multi_file_upload.php

Copy Fullscreen Close Fullscreen
<?php  
echo 'File Info : <pre>';
print_r($_FILES['myimage']);
echo '</pre>';
$total_files = count($_FILES['myimage']['name']);
for($n=0; $n<$total_files; $n++)
{
/* before upload the file we should rename it */
$tmp = $_FILES["myimage"]["tmp_name"][$n];
$extension = explode("/", $_FILES["myimage"]["type"][$n]);
$new_name = time().".".$extension[1];

if(move_uploaded_file($tmp, $new_name))
echo "Your file is uploaded with $new_name name <br>";
else
echo 'Error In Uploading';
}
?>
Output
File Info :
Array
(
    [name] => Array
        (
            [0] => open-admin.png
            [1] => open-xampp.png
        )

    [type] => Array
        (
            [0] => image/png
            [1] => image/png
        )

    [tmp_name] => Array
        (
            [0] => C:\xampp\tmp\php825E.tmp
            [1] => C:\xampp\tmp\php828E.tmp
        )

    [error] => Array
        (
            [0] => 0
            [1] => 0
        )

    [size] => Array
        (
            [0] => 57716
            [1] => 42910
        )

)
Your file is uploaded with 1600057720.png name
Your file is uploaded with 1600057880.png name

Related Topics :

Rahul Kumar

Rahul Kumar

Hi ! I'm Rahul Kumar Rajput founder of learnhindituts.com. I'm a software developer having more than 4 years of experience. I love to talk about programming as well as writing technical tutorials and blogs that can help to others. I'm here to help you navigate the coding cosmos and turn your ideas into reality, keep coding, keep learning :)

Get connected with me. :) LinkedIn Twitter Instagram Facebook

b2eprogrammers