[X] Choose Font Here

PHP File Upload

PHP Programming Language မွာလည္း တစ္ျခား Programming ေတြလိုပဲ File ကုိ upload တင္လို ့ရပါတယ္။

အဓိက သံုးတာကေတာ့ $_FILES ဆိုတဲ့ variable ကိုသံုးတာျဖစ္ပါတယ္။

 

ဒီ code ကေတာ့ upload တင္မယ့္ HTML Form ျဖစ္ပါတယ္။

 

<html>
<body>

<form action="upload_file.php" method="post" enctype="multipart/form-data">
<label for="file">Filename:</label>
<input type="file" name="file" id="file" />
<br />
<input type="submit" name="submit" value="Submit" />
</form>

</body>
</html>

ဒီ sample ေလးကို ၾကည့္မယ္ဆိုရင္ ထူးျခားတာေလး (၂) ခုရွိပါတယ္။


* html form tag မွာ enctype="multiport/form-data" ဆိုတာကိုေတြ ့ႏိုင္ပါတယ္။ ဆုိလိုတာကေတာ့

Client Side Form ကေန Server ကို Binary Data ေတြကို Pass လုပ္ဖို ့လိုလာရင္ သံုးပါတယ္။


* ဒီ Form မွာ Input element ကို ၾကည့္မယ္ဆိုရင္ type="file" ဆိုတာကို ေတြ ့ရပါမယ္။ အခု ဒီ textbox ကို

File Upload တင္ဖို ့သံုးမွာ ျဖစ္တာေၾကာင့္ ဒီ type="file" ကုိ သံုးတာျဖစ္ပါတယ္။ ထူးျခားတာကေတာ့ အဲ့ဒီလို

ေရးလိုက္တာနဲ ့ ေဘးမွာ File ကို select လုပ္ဖို ့ Browse ဆိုတဲ့ Button ပါလာမွာ ျဖစ္ပါတယ္။

 

upload_file.php ဆိုတဲ့ script ေလးကို create လုပ္ပါမယ္။

 

<?php
if ($_FILES["file"]["error"] > 0)
{
echo "Error: " . $_FILES["file"]["error"] . "<br />";
}
else
{
echo "Upload: " . $_FILES["file"]["name"] . "<br />";
echo "Type: " . $_FILES["file"]["type"] . "<br />";
echo "Size: " . ($_FILES["file"]["size"] / 1024) . " Kb<br />";
echo "Stored in: " . $_FILES["file"]["tmp_name"];
}
?>

 

PHP ရဲ ့ Global variable ျဖစ္တဲ့ $_FILES ကို သံုးျပီး client computer ကေနျပီး Remote Server ကို File Upload

တင္ႏိုင္တာ ကို ေတြ ့ရပါမယ္။ ဒီ sample ေလးမွာ ေတြ ့ႏိုင္တာကေတာ့


- $_FILES["file"]["name"] – the name of the upload file

(upload တင္တဲ့ file ရဲ ့့ Name ကို ေျပာတာျဖစ္ပါတယ္။)

- $_FILES["file"]["type"] – the type of the upload file

(upload တင္တဲ့ file ရဲ ့Type ကို ေျပာတာျဖစ္ပါတယ္။ ဥပမာ jpg လား gif လား ဆိုတာမ်ိဳးျဖစ္ပါတယ္။)

- $_FILES["file"]["size"] – the size in bytes of upload file

(upload တင္တဲ့ File ရဲ ့ size ကုိ bytes နဲ ့ေဖာ္ျပတာကို ဆိုလိုပါတယ္။)

- $_FILES["file"]["tmp_name"] – the name of the temporary copy of flie stored on the server
(upload တင္တဲ့ အခါ ကိုယ္တင္လိုက္တဲ့ File ကို server ရဲ ့ temporary location တစ္ခုမွာ ခဏ store လုပ္ပါတယ္။)

 

အဲ့ဒီလို upload တင္တဲ့ အခါမွာ user ကို Restriction လုပ္လို ့ရပါတယ္။ ဒီ sample ကေတာ့ upload file ဟာ

.gif (or) .jpeg ျဖစ္ရမယ္။ ျပီးေတာ့ 20kB ေအာက္ငယ္ရမယ္ဆိုတာ သတ္မွတ္ထားတာျဖစ္ပါတယ္။

 

<?php

if ((($_FILES["file"]["type"] == "image/gif")
|| ($_FILES["file"]["type"] == "image/jpeg")
|| ($_FILES["file"]["type"] == "image/pjpeg"))
&& ($_FILES["file"]["size"] < 20000))
{
if ($_FILES["file"]["error"] > 0)
{
echo "Error: " . $_FILES["file"]["error"] . "<br />";
}
else
{
echo "Upload: " . $_FILES["file"]["name"] . "<br />";
echo "Type: " . $_FILES["file"]["type"] . "<br />";
echo "Size: " . ($_FILES["file"]["size"] / 1024) . " Kb<br />";
echo "Stored in: " . $_FILES["file"]["tmp_name"];
}
}
else
{
echo "Invalid file";
}

?>

အခုလို Upload တင္ျပီးသား File ကုိ Server ရဲ ့ Location တစ္ေနရာမွာ save လုပ္ခ်င္တယ္ဆိုရင္ေတာ့ ဒီ sample

ေလးကို ဆက္ျပီး ၾကည့္လုိက္ပါ။

 

<?php
if ((($_FILES["file"]["type"] == "image/gif")
|| ($_FILES["file"]["type"] == "image/jpeg")
|| ($_FILES["file"]["type"] == "image/pjpeg"))
&& ($_FILES["file"]["size"] < 20000))
{
if ($_FILES["file"]["error"] > 0)
{
echo "Return Code: " . $_FILES["file"]["error"] . "<br />";
}
else
{
echo "Upload: " . $_FILES["file"]["name"] . "<br />";
echo "Type: " . $_FILES["file"]["type"] . "<br />";
echo "Size: " . ($_FILES["file"]["size"] / 1024) . " Kb<br />";
echo "Temp file: " . $_FILES["file"]["tmp_name"] . "<br />";

if (file_exists("upload/" . $_FILES["file"]["name"]))
{
echo $_FILES["file"]["name"] . " already exists. ";
}
else
{
move_uploaded_file($_FILES["file"]["tmp_name"],
"upload/" . $_FILES["file"]["name"]);
echo "Stored in: " . "upload/" . $_FILES["file"]["name"];
}
}
}
else
{
echo "Invalid file";
}
?>

 

ဒီ sample ေလးမွာ အသစ္ေတြ ့ရႏိုင္တဲ့ Function ကေတာ့ move_uploaded_file() ဆိုတဲ့ Function ျဖစ္ပါတယ္။

ကိုယ္ upload တင္ထားတဲ့ File ကုိ tmp location ကေနျပီ server ရဲ ့ တစ္ျခား location ကို ေျပာင္းခ်င္တယ္ဆိုရင္

သံုးပါတယ္။

Usage ကေတာ့

move_uploaded_file(string $filename, string $destination ) ျဖစ္ပါတယ္။

 

ဒါကေတာ့ PHP မွာ File Upload တင္တဲ့ အေၾကာင္း ကၽြန္ေတာ္ သိသေလာက္ ေရးထားတာျဖစ္ပါတယ္။

 

တျခား PHP Post ေတြအားလုံး ကို ႏွစ္သက္ရာ ေရြးဖတ္ ခ်င္တယ္ဆိုရင္ ဒီ Link ေလးကို Click လိုက္ပါ။

 

 

 

 

 

Comments (2)
  • Anonymous

    Warning: move_uploaded_file(upload/1 (13).JPG) [function.move-uploaded-file]: failed to open stream: No such file or directory in C:\xampp\htdocs\upload_file.php on line 66

    Warning: move_uploaded_file() [function.move-uploaded-file]: Unable to move 'C:\xampp\tmp\php48.tmp' to 'upload/1 (13).JPG' in C:\xampp\htdocs\upload_file.php on line 66
    Stored in: upload/1 (13).JPG ...
    when I run upload file save in server location code, I got that errors.. pls explain me bro.. why ... am i doing something wrong??

  • admin  - Re:

    ကြၽန္ေတာ္ထင္တာကေတာ့ move_uploaded_file မွာ distention ကိုေပးရတယ္ဗ်ာ။ အဲဒီ ေပးထားတဲ့ဟာက မရွိတာလဲ ျဖစ္ႏိုင္တယ္။ permission ရွိေနတာမ်ိဳးလဲ ျဖစ္ႏိုင္တယ္ဗ်။

Write comment
Your Contact Details:
Comment:
[b] [i] [u] [url] [quote] [code] [img]   
:D:angry::angry-red::evil::idea::love::x:no-comments::ooo::pirate::?::(
:sleep::););)):0
Security
Please input the anti-spam code that you can read in the image.

Login Form

Categories Table View

JoomlaWatch Stats 1.2.9 by Matej Koval

Facebook Share

Share on facebook

Accordion FAQ

mod_joomtouch

Version Iphone

Version Iphone by JoomTouch