0
548

Download Small File With PHP

Myo Thant Kyaw


<a href=“https://eg.com/file.jpg” download>Download</a>


အထက်က code ကို web developer တိုင်းရေးဖူးကြပီး

အလုပ်မလုပ်တာကိုလည်းကြုံဖူးကြလိမ့်မယ်ထင်ပါတယ်

ဟုတ်ပါတယ် file တစ်ခုကို ဒေါင်းလုပ် လုပ်ဖို့ client ကနေ မသွားရပါဘူး

Server ကနေ http response ပြန်ပေးပီး browser ကို download လုပ်ခွင့်ပေးရပါတယ်။

http response ဆိုတာက php developer တွေ သုံးဖူးပိးသားပါ

header(“Location:path”) ဒါက server path တစ်ခုကို http response လုပ်ပေးတာပါ


Small file တစ်ခုကိုအလွယ်တကူ download လုပ်ခွင့်ရစေဖို့code သုံးကြောင်းပဲလိုပါတယ်


  1. => file path
  2. => header response for download
  3. => readfile(filename)


အထက်ကconcept လေးပါပဲ



<?php
$file = "/images/logo.png";
header('Content-Disposition: attachment; filename=logo.png');// logo.png မှာ သင်ဒေါင်းစေချင်တဲ့အမည်ထည့်ရပါမယ်
readfile($file);
?>




Content-Disposition: attachment  http string က browser ကနေ ဒေါင်းလုပ်လုပ်ခွင့်ကို 

ခွင့်ပြုပေးမှာပါ

 ဒီcode ကို run ရင်  logo.png အမည်နဲ့  file ကိုဒေါင်းလုပ် လုပ်ပါလိမ့်မယ်



ဒီ code က small file size တွေအတွက်သာ အဆင်ပြေမှာပါ

ဘာလို့လည်းဆိုတော့ php က file တစ်ခုလုံးကိုဖတ်လိုက်တာကြောင့်ပါ

ဥပမာ 1gb file ကိုဖတ်နေရပီး user အယောက် ၁၀၀၀ သုံးနေတယ်ဆိုရင် သေချာပီ

သင့် server performance ကောင်းကောင်းတစ်ခုမဟုတ်ဖူးဆိုရင် တော်တော်အခြေ အနေဆိုးနေမှာပါ


အထက်ကအဆင့်မရောက်အောင် php ကလည်းလက်မခံပါဘူး 1gb file တစ်ခုလုံး မဖတ်ပါဘူး error or warning တက်ပါလိမ့်မယ်


ဒီအတွက် browser ကို chunk လုပ်ပီး response လုပ်ပေးဖို့လိုပါတယ်

ဒီအပိုင်းကလည်း သေချာပြောပြရမဲ့ အပိုင်တွေအများကြီးပါကြောင့် 

နောက် article တစ်ခု မှာဆက်ဖတ်ပါခင်ဗျာ။

Comment:
Please Login To Create Comment Login
Loading...