Pages

Saturday, October 29, 2011

PHP - File Upload: HTML Form


PHP ကို အသံုးျပဳၿပီ သင့္ uploads မ်ားအား မစီမံမီ သင့္အေနျဖင့္ file အား upload ျပဳလုပ္ရန္အတြက္ HTML form တစ္ခုကို မျဖစ္မေန တည္ေဆာက္ရပါမည္။ ဒါက user ေတြ fiel တစ္ခုကို upload လုပ္ဖို႔ျဖစ္ပါတယ္။ HTML form ႏွင့္ ပတ္သက္၍ ပိုမို နက္နက္ရိႈင္းရိႈင္း သိလို လွ်င္ ကၽြန္ေတာ္တို႔၏ ဒီ  HTML Form lesson တြင္ ေလ့လာေပးပါ။

HTML Code:

<form enctype="multipart/form-data" action="uploader.php" method="POST">
<input type="hidden" name="MAX_FILE_SIZE" value="100000" />
Choose a file to upload: <input name="uploadedfile" type="file" /><br />
<input type="submit" value="Upload File" />
</form>
   ေအာက္ပါ အခ်က္မ်ားသည္ အထက္ေဖာ္ျပပါ ကုဒ္ မ်ားအတြက္ အေရးႀကီးေသာ အႏွစ္ခ်ဳပ္ အခ်က္မ်ား ျဖစ္ပါသည္။
  • enctype="multipart/form-data" - Necessary for our to-be-created PHP file to function properly.
  • action="uploader.php" - The name of our PHP page that will be created, shortly.
  • method="POST" - Informs the browser that we want to send information to the server using POST.
  • input type="hidden" name="MA... - Sets the maximum allowable file size, in bytes, that can be uploaded. This safety mechanism is easily bypassed and we will show a solid backup solution in PHP. We have set the max file size to 100KB in this example.
  • input name="uploadedfile" - uploadedfile is how we will access the file in our PHP script.
ထို ကုဒ္ မ်ားအား upload.html ဆိုသည့္ အမည္ျဖင့္ သိမ္းပါ။ အကယ္၍ သင္ ထို ဖိုင္အား  browser  တြင္ ၾကည့္ၾကည့္လွ်င္ ေအာက္ပါပံု အတိုင္း ေတြ႔လိမ့္မည္။

User သည္ submit ကို ႏွိပ္လိုက္သည္ႏွင့္   data မ်ားအား server ဆီသို႔ ပို႔ေပးျဖစ္ ျဖစ္သည္။  User အေနျဖင့္ uploader.php ႏွင့္ ဆက္သြယ္အလုပ္လုပ္သြား ေသာေၾကာင့္ PHP file သည္ လုပ္ရမည့္ တာဝန္မ်ား ေဆာင္ရြက္သြားပါမည္။
PHP - File Upload: What's the PHP Going to Do?
 HTML form တစ္ခု ရထားၿပီးျဖစ္ေသာ ေၾကာင့္ ကၽြန္ေတာ္တို႔ အေနျဖင့္ upload အတြက္ PHP script မ်ားအား စတင္ေရးသား ႏုိင္ၿပီျဖစ္သည္။ ထံုးစံ အတိုင္းပင္ upload ျပဳလုပ္မည့္ file မ်ားအား သတ္မွတ္ခ်က္မ်ား ထားသင့္ပါသည္။
အေၾကာင္းျပခ်က္ တစ္ခ်ဳိ႕အား ၾကည့္ပါ -
  • The file is too large and you do not want to have it on your server.
  • You wanted the person to upload a picture and they uploaded something else, like an executable file (.exe).
  • There were problems uploading the file and so you can't keep it.
ဤ ဥပမာမ်ားသည္ အလြန္ ရိုးရွင္း ၿပီး ေပ်ာ့ညံ့ ပါသည္။ ဤ ထက္မက လက္ေတြ႔က်ေသာ ကုဒ္မ်ား ေပါင္းထည့္ သင့္သည္။
PHP - File Upload: uploader.php
uploader.php file is execute လုပ္ၿပီးသြားသည့္ အခ်ိန္တြင္ upload တင္ၿပီးသြားေသာ ဖိုင္သည္ sever တြင္ရွိေသာ temporary storage area တြင္သာ ရွိေနသည္။ တစ္ျခား location တစ္ခုခု သို႔ေျပာင္း ေပးရပါမည္။ သို႔မဟုတ္လွ်င္ ထို ဖုိင္သည္ ပ်က္စီးသြားပါလိမ့္မည္။ ထို uploaded files မ်ားအား သိမ္းထားႏုိင္ရန္အတြက္ $_FILES associative array ဆိုသည့္ array ကို ေရးသား အသံုး ျပဳရပါမည္။
 $_FILES array သည္ PHP အေနျဖင့္ upload ျပဳလုပ္မည့္  files ၏ အခ်က္အလက္မ်ား မည္သည့္ ေနရာတြင္ သိုေလွာင္ထားမည္ ကိုသတ္မွတ္ျခင္းျဖစ္သည္။ ဤေနရာတြင္ array ၏  elements  ႏွစ္ခု ရွိသည္။ ထိုႏွစ္ခုကို ကၽြန္ေတာ္တို႔ ယခု ဥပမာအတြက္ နားလည္ သေဘာေပါက္ထားရပါမည္။
  • uploadedfile - uploadedfile is the reference we assigned in our HTML form. We will need this to tell the $_FILES array which file we want to play around with.
  • $_FILES['uploadedfile']['name'] - name contains the original path of the user uploaded file.
  • $_FILES['uploadedfile']['tmp_name'] - tmp_name contains the path to the temporary file that resides on the server. The file should exist on the server in a temporary directory with a temporary name.
ေနာက္ဆံုးတြင္ အေျခခံ PHP upload manager script ကို ကၽြန္ေတာ္တို႔ စတင္ေရး သားႏုိင္ပါၿပီ။ နာမည္ အား မည္သို႔ ရယူမည္နည္း ၊ မူရင္းအမည္ အားေရြးခ်ယ္ျခင္း ၊ သိမ္းထားမည့္ ေနရာ သတ္မွတ္ ေပးျခင္း မ်ားအား ဤ ေနရာတြင္ ေတြ႔ရ ပါမည္။

PHP Code:

// Where the file is going to be placed 
$target_path = "uploads/";
 
/* Add the original filename to our target path.  
Result is "uploads/filename.extension" */
$target_path = $target_path . basename( $_FILES['uploadedfile']['name']); 
NOTE: အခန္းတစ္ခု ဖြဲ႕ကာ သူ၏ အတြင္းဘက္တြင္ uploader.php ဖိုင္ တစ္ခု ဖန္တီးရမည္။ ထို႔ေနာက္ "uploads" ဆိုသည့္ folder အသစ္တစ္ခု ထပ္ေဆာက္ရမည္။ ထိုေနရာတြင္ upload ျပဳလုပ္ သည့္ ဖိုင္ထားမည္။
ယခု ကၽြန္ေတာ္တို႔ အေနျဖင့္ ၿပီးစီးဖို႔ အတြက္ server သိမ္းဆည္းျခင္း အပိုင္း လိုအပ္သည္။ $target_path  သည္ မည္သည့္ ေနရာတြင္ သိမ္းမည္ဆိုသည့္ အပိုင္းျဖစ္သည္။

PHP - File Upload: move_uploaded_file Function

ယခု ကၽြန္ေတာ္တို႔သည္ move_uploaded_file function အားဖန္တီးၿပီး PHP အား မ်က္လွည့္ ျပေစၾကစို႔။ move_uploaded_file function သည္ 1) temporary file ေနရာသိရမည္။ 2)  ေျပာင္း ေရြ႕သြားမည့္ေနရာလည္း သိရမည္။

PHP Code:

$target_path = "uploads/";
 
$target_path = $target_path . basename( $_FILES['uploadedfile']['name']); 
 
if(move_uploaded_file($_FILES['uploadedfile']['tmp_name'], $target_path)) {
    echo "The file ".  basename( $_FILES['uploadedfile']['name']). 
    " has been uploaded";
} else{
    echo "There was an error uploading the file, please try again!";
}
အကယ္၍ upload အဆင္ေျပေျပ ၿပီးသြားလွ်င္ "The file filename has been uploaded" ဆိုသည့္ စာသားအား ေတြ႔ရပါမည္။  move_uploaded_file သည္ မွန္ကန္စြာ ကူးေျပာင္းသြားၿပီး ျဖစ္သည္။ မွားယြင္း တစ္ခုခု ႀကံဳရပါက false အေနအထားသို႔ ျပန္လာပါမည္။
အကယ္၍သာ ျပႆနာ တစ္ခုခု ႀကံဳေနပါက "There was an error uploading the file, please try again!" ဆိုသည့္ စာသား ကိုေတြ႔ရပါမည္။

PHP - File Upload: Safe Practices!

Note: This script  ပညာေပး သင္ခန္းစာ အတြက္မွ်သာ ျဖစ္ပါသည္။ ကၽြန္ေတာ္တို႔ အေနျဖင့္ လူထု အသံုးျပဳတဲ့ website တစ္ခု တြင္ အသံုးမျပဳပါ။
ဤ အနည္းငယ္ေသာ ကုဒ္မ်ားသည္ မည္သူ မဆို သင့္ server ဆီသို႔ upload ျပဳလုပ္ႏုိင္မည္။ အဘယ္ေၾကာင့္ ဟုဆိုေသာ္ ကၽြန္ေတာ္ တုိ႔ အႀကံျပဳခ်က္တြင္ သင့္ထံတြင္ လူတိုင္း အသံုးျပဳႏုိင္ေသာ ရိုးရွင္းေသာ uploader မရွိေသး ဟု ယူဆထားေသာေၾကာင့္ ျဖစ္သည္။ ျခားတစ္ဖက္တြင္ ၾကည့္လွ်င္ သင့္ server တြင္ အသံုးမဝင္ သည္မ်ားႏွင့္ ျပည့္ေနပါလိမ့္မည္။သို႔မဟုတ္ လံုးၿခံဳေရးဆိုင္ရာ အေပးအယူ တစ္ခုုခုႏွင့္ ႀကံဳရပါမည္။
PHP ကုိအသံုးျပဳၿပီး uploading အား မည္ကဲ့သုိ႔ အလုပ္လုပ္သည္ကို သင္ေလ့လာၿပီး ေပ်ာ္ရႊင္ ေက်နပ္ေနမိမည္ဟု ကၽြန္ေတာ္တို႔ ေမွ်ာ္လင့္ပါသည္။ သိပ္မေဝးေတာ့ေသာ တစ္ခ်ိန္တြင္ ကၽြန္ေတာ္ တို႔ အေနျဖင့္ ယခုထက္ ပိုမိုအဆင့္ျမင့္ေသာ သင္ခန္းစာ ၊ ပိုမို လံုၿခံဳေရးျမင့္မားေသာ ေလ့က်င့္ ခန္းမ်ား ေဖာ္ျပေပးသြားပါမည္။


ကိုယ္ခ်င္းစာသူ
kochinsarthu@gmail.com

0 comments:

Post a Comment

အခုလို ဖတ္ေပးတဲ့အတြက္ ေက်းဇူးပါ။

Web Informer Button
Related Posts Plugin for WordPress, Blogger...