PHP ဖိုင်ကို တင်ခြင်း။
PHP ဖြင့် ဖိုင်များကို ဆာဗာသို့ အပ်လုဒ်လုပ်ရန် လွယ်ကူသည်။
သို့သော် လွယ်ကူစွာဖြင့် အန္တရာယ်ရှိလာသောကြောင့် ဖိုင်ကို အပ်လုဒ်တင်ခွင့်ပြုသည့်အခါ အမြဲသတိထားပါ။
"php.ini" ဖိုင်ကို စီစဉ်သတ်မှတ်ပါ။
ပထမဦးစွာ၊ ဖိုင်အပ်လုဒ်တင်ခြင်းအား ခွင့်ပြုရန် PHP ကို ပြင်ဆင်သတ်မှတ်ထားကြောင်း သေချာပါစေ။
သင်၏ "php.ini" ဖိုင်တွင်၊ file_uploads
လမ်းညွှန်ချက်ကို ရှာဖွေပြီး On ထားရန်-
file_uploads = On
HTML Form ဖန်တီးပါ။
ထို့နောက် အသုံးပြုသူများသည် ၎င်းတို့တင်လိုသော ပုံဖိုင်ကို ရွေးချယ်နိုင်စေမည့် HTML ဖောင်ပုံစံတစ်ခုကို ဖန်တီးပါ-
<!DOCTYPE html>
<html>
<body>
<form action="upload.php" method="post"
enctype="multipart/form-data">
Select image to upload:
<input type="file" name="fileToUpload" id="fileToUpload">
<input type="submit" value="Upload Image" name="submit">
</form>
</body>
</html>
အထက်ဖော်ပြပါ HTML ဖောင်အတွက် လိုက်နာရမည့် စည်းမျဉ်းအချို့
- ဖောင်တွင် method="post" ကို အသုံးပြုကြောင်း သေချာပါစေ။
- ဖောင်တွင် အောက်ဖော်ပြပါ အရည်အချင်းလည်း လိုအပ်သည်- enctype="multipart/form-data"။ ဖောင်ကို တင်သွင်းရာတွင် မည်သည့်အကြောင်းအရာအမျိုးအစားကို အသုံးပြုရမည်ကို ၎င်းက သတ်မှတ်ပေးပါသည်။
အထက်ဖော်ပြပါ လိုအပ်ချက်များမရှိဘဲ၊ ဖိုင်ကို အပ်လုဒ်လုပ်၍မရပါ။
အခြားသတိပြုစရာများ-
- <input> tag ၏ type="file" attribute သည် input ထိန်းချုပ်မှုဘေးရှိ "Browse" ခလုတ်ဖြင့် ထည့်သွင်းသည့်အကွက်ကို ဖိုင်ရွေးချယ်သည့် ထိန်းချုပ်မှုအဖြစ် ပြသသည်
အထက်ဖော်ပြပါပုံစံသည် ကျွန်ုပ်တို့နောက်တစ်ခုဖန်တီးမည့် "upload.php" ဟုခေါ်သော ဖိုင်တစ်ခုသို့ ဒေတာပေးပို့ပါသည်။
Upload File PHP Script ကိုဖန်တီးပါ။
"upload.php" ဖိုင်တွင် ဖိုင်တစ်ခုကို အပ်လုဒ်တင်ရန်အတွက် ကုဒ်ပါရှိသည်။
<?php
$target_dir = "uploads/";
$target_file = $target_dir . basename($_FILES["fileToUpload"]["name"]);
$uploadOk = 1;
$imageFileType =
strtolower(pathinfo($target_file,PATHINFO_EXTENSION));
// Check if image file is a actual image or fake image
if(isset($_POST["submit"])) {
$check = getimagesize($_FILES["fileToUpload"]["tmp_name"]);
if($check !== false) {
echo "File is an image - " . $check["mime"] . ".";
$uploadOk = 1;
} else {
echo "File is not an image.";
$uploadOk = 0;
}
}
?>
PHP script ကို ရှင်းပြထားသည်
- $target_dir = "uploads/" - ဖိုင်ထားရှိမည့် လမ်းညွှန်ကို သတ်မှတ်ပေးသည်
- $target_file သည် အပ်လုဒ်လုပ်ရန် ဖိုင်လမ်းကြောင်းကို သတ်မှတ်သည်။
- $uploadOk=1 ကို အသုံးမပြုသေးပါ (နောက်ပိုင်းတွင် အသုံးပြုပါမည်)
- $imageFileType သည် ဖိုင်၏ ဖိုင် extension ကို ကိုင်ထားသည် (အသေးစိတ်ဖြင့်)
- ထို့နောက် ရုပ်ပုံဖိုင်သည် တကယ့်ပုံ သို့မဟုတ် ပုံအတု ဟုတ်မဟုတ် စစ်ဆေးပါ။
မှတ်ချက်- "upload.php" ဖိုင်တည်ရှိရာ လမ်းညွှန်တွင် "အပ်လုဒ်များ" ဟုခေါ်သော လမ်းညွှန်အသစ်တစ်ခု ဖန်တီးရန် လိုအပ်ပါသည်။ အပ်လုဒ်လုပ်ထားသောဖိုင်များကို ထိုနေရာတွင် သိမ်းဆည်းပါမည်။
ဖိုင်ရှိမရှိ စစ်ဆေးပါ။
ယခု ကျွန်ုပ်တို့သည် ကန့်သတ်ချက်အချို့ကို ထည့်သွင်းနိုင်ပြီဖြစ်သည်။
ဦးစွာ၊ "အပ်လုဒ်များ" ဖိုဒါတွင် ဖိုင်ရှိနှင့်ပြီးကြောင်း စစ်ဆေးပါမည်။ အကယ်၍ ၎င်းသည် အမှားအယွင်း မက်ဆေ့ချ်ကို ပြသပြီး $uploadOk ကို 0 ဟု သတ်မှတ်သည်-
// Check if file already exists
if (file_exists($target_file)) {
echo "Sorry, file already exists.";
$uploadOk = 0;
}
ဖိုင်အရွယ်အစားကို ကန့်သတ်ပါ။
အထက်ဖော်ပြပါ ကျွန်ုပ်တို့၏ HTML ဖောင်တွင် ဖိုင်ထည့်သွင်းမှုအကွက်ကို "fileToUpload" ဟုခေါ်သည်။
ယခု ကျွန်ုပ်တို့သည် ဖိုင်အရွယ်အစားကို စစ်ဆေးလိုပါသည်။ ဖိုင်သည် 500KB ထက် ပိုကြီးပါက၊ အမှားအယွင်း မက်ဆေ့ချ်တစ်ခု ပြသမည်ဖြစ်ပြီး $uploadOk ကို 0 ဟု သတ်မှတ်ထားသည်-
// Check file size
if ($_FILES["fileToUpload"]["size"] > 500000) {
echo "Sorry, your file is too large.";
$uploadOk = 0;
}
ဖိုင်အမျိုးအစားကို ကန့်သတ်ပါ။
အောက်ပါကုဒ်သည် အသုံးပြုသူများအား JPG၊ JPEG၊ PNG နှင့် GIF ဖိုင်များကိုသာ အပ်လုဒ်လုပ်ခွင့်ပြုသည်။ အခြားဖိုင်အမျိုးအစားများအားလုံးသည် $uploadOk ကို 0 သို့မသတ်မှတ်မီ အမှားအယွင်းသတင်းတစ်ခုပေးသည်-
// Allow certain file formats
if($imageFileType != "jpg" && $imageFileType != "png" && $imageFileType != "jpeg"
&& $imageFileType != "gif" ) {
echo "Sorry, only JPG, JPEG, PNG & GIF files are allowed.";
$uploadOk = 0;
}
PHP Script ဖိုင်ကို အပြည့်အ၀ တင်ပါ။
ပြီးပြည့်စုံသော "upload.php" ဖိုင်သည် ယခုပုံစံအတိုင်းဖြစ်သည်-
<?php
$target_dir = "uploads/";
$target_file = $target_dir . basename($_FILES["fileToUpload"]["name"]);
$uploadOk = 1;
$imageFileType =
strtolower(pathinfo($target_file,PATHINFO_EXTENSION));
// Check if image file is a actual image or fake image
if(isset($_POST["submit"])) {
$check = getimagesize($_FILES["fileToUpload"]["tmp_name"]);
if($check !== false) {
echo "File is an image - " . $check["mime"] . ".";
$uploadOk = 1;
} else {
echo "File is not an image.";
$uploadOk = 0;
}
}
// Check if file already exists
if (file_exists($target_file)) {
echo "Sorry, file already exists.";
$uploadOk = 0;
}
// Check file size
if ($_FILES["fileToUpload"]["size"] > 500000) {
echo "Sorry, your file is too large.";
$uploadOk = 0;
}
// Allow certain file formats
if($imageFileType != "jpg" && $imageFileType != "png" && $imageFileType != "jpeg"
&& $imageFileType != "gif" ) {
echo "Sorry, only JPG, JPEG, PNG & GIF files are allowed.";
$uploadOk = 0;
}
// Check if $uploadOk is set to 0 by an error
if ($uploadOk == 0) {
echo "Sorry, your file was not uploaded.";
// if everything is ok, try to upload file
} else {
if (move_uploaded_file($_FILES["fileToUpload"]["tmp_name"], $target_file)) {
echo "The file ". htmlspecialchars( basename( $_FILES["fileToUpload"]["name"])).
" has been uploaded.";
} else {
echo "Sorry, there was an error uploading your file.";
}
}
?>
PHP Filesystem Reference ကို ပြီးအောင်လုပ်ပါ။
ဖိုင်စနစ်လုပ်ဆောင်ချက်များကို အပြည့်အစုံကိုးကားရန်အတွက်၊ ကျွန်ုပ်တို့၏ ပြီးပြည့်စုံသော PHP Filesystem Reference သို့ သွားပါ ။