လက်ရှိတည်နေရာ: ပင်မစာမျက်နှာ> နောက်ဆုံးရဆောင်းပါးများစာရင်း> File_exists နှင့်ဖိုင်ရှိမတည်မရှိ,

File_exists နှင့်ဖိုင်ရှိမတည်မရှိ,

gitbox 2025-05-17

PHP တွင်ကျွန်ုပ်တို့သည်ပြင်ပဖိုင်များကို မိတ်ဆက်ရန် သို့မဟုတ်လိုအပ်သောသို့မဟုတ် လိုအပ်သည် ။ ဤလုပ်ဆောင်ချက်များကိုလမ်းညွှန်မျိုးစုံရှိဖိုင်များကိုရှာဖွေရန် PHP တွင်လမ်းကြောင်းဆိုင်ရာယန္တရားတစ်ခုပါဝင်သည်။ Get_includude_path () function ကကျွန်တော်တို့ကိုလက်ရှိလမ်းကြောင်းကိုရဖို့ကူညီပေးနိုင်ပါတယ်

ဤဆောင်းပါးသည် Get_includude_Path () နှင့် file_exists () နှင့် File_exists () ကို မည်သို့အသုံးပြုရမည်ကိုမိတ်ဆက်ပေးနိုင်သည်။ PHP တွင်ဖိုင်တစ်ခုတွင်ဖိုင်ရှိ,

get_include_path ကိုနားလည်သည် ()

PHP ၏ Get_includude_Path () function သည်လမ်းကြောင်းများရေးစပ်ထားသည့် string တစ်ခုဖြစ်ပြီး operating system မှသတ်မှတ်ထားသော delimiters များမှဖြတ်သန်းသွားသောလမ်းကြောင်းများဖြင့်ခွဲထားသည့်လမ်းကြောင်းများစွာဖြင့်ခွဲထားသည်။

  • Unix / Linux Systems တွင် ,

  • Windows Systems တွင်လမ်းကြောင်းခွဲနေခြင်း ,

ဤလမ်းကြောင်းများသည် PHP အင်ဂျင်သည် Executing သို့မဟုတ် လိုအပ်သော အခါဖိုင်များကိုရှာဖွေသည့် directory များဖြစ်သည်။

ဥပမာအားဖြင့်:

 echo get_include_path();

output ကိုဖြစ်လိမ့်မည်:

 .:/usr/share/php:/usr/local/lib/php

အကယ်. ဖိုင်တစ်ခုတွင်ဖိုင်တစ်ခုတွင်ရှိလျှင်မည်သို့ရှာရမည်နည်း

File_exists () တွင် ပါ 0 င်သည့်လမ်းကြောင်းကိုအလိုအလျောက်ရှာဖွေခြင်းမပြုလုပ်နိုင်ပါ (၎င်းသည်ပေးထားသောလမ်းကြောင်းတည်ရှိမှုရှိ, မရှိစစ်ဆေးသည် ) ကိုကျွန်ုပ်တို့ဖယ်ရှားပေးရန်လိုသည်။

နမူနာကုဒ်

အောက်ပါ function ကို function_exists_in_include_path ($ fileName) သည် ဤလုပ်ဆောင်မှုကိုအကောင်အထည်ဖော်သည်။

 function file_exists_in_include_path($filename) {
    $paths = explode(PATH_SEPARATOR, get_include_path());
    
    foreach ($paths as $path) {
        $fullPath = rtrim($path, DIRECTORY_SEPARATOR) . DIRECTORY_SEPARATOR . $filename;
        if (file_exists($fullPath)) {
            return $fullPath;
        }
    }
    
    return false;
}

သင်ဤကဲ့သို့သောကိုသုံးနိုင်သည်။

 $filename = 'config/settings.php';
$result = file_exists_in_include_path($filename);

if ($result !== false) {
    echo "ဖိုင်ကိုထည့်သွင်းလေ့လာမှုတွင်တွေ့ရသည်,အပြည့်အဝလမ်းကြောင်းသည်: " . $result;
} else {
    echo "ဖိုင်တွင်မပါဝင်ပါ。";
}

လက်တွေ့ကျသော application အခြေအနေများ

သင်ဟာစီမံကိန်းကြီးတစ်ခုမှာရှိနေတယ်ဆိုပါစို့။ configuration files များကိုမတူညီသောလမ်းညွှန်များတွင်ထည့်ထားနိုင်သည် ဖိုင်တစ်ခုသည်သတိပေးမက်ဆေ့ခ်ျများကိုရှောင်ရှားရန်မ ပါ 0 င်မီသင်စစ်ဆေးလိုသည်။

ဥပမာအားဖြင့်:

 $filename = 'lib/MyClass.php';

if ($path = file_exists_in_include_path($filename)) {
    include $path;
} else {
    error_log("စာချုပ်စာတမ်း $filename ထည့်သွင်းလမ်းကြောင်းအတွက်မတည်ရှိပါဘူး");
}

setting တွင်လမ်းကြောင်းပါဝင်သည်

သင်၏ sect_include_path () ကို သင်၏ script ရှိလမ်းကြောင်းများထည့်ရန်လည်းအသုံးပြုနိုင်သည်။

 set_include_path(get_include_path() . PATH_SEPARATOR . '/var/www/gitbox.net/includes');

ကောက်ချက်

Get_includude_Path () မှတဆင့်လမ်းကြောင်းများအားလုံးကိုရယူပါ။ ထို့နောက်ပရိုဂရမ်၏ပြောင်းလွယ်ပြင်လွယ်မှုနှင့်ကြံ့ခိုင်မှုကိုမြှင့်တင်နိုင်သည့်ဖိုင်သည် file_exists () နှင့်အတူရှိ, မရှိစစ်ဆေးပါ။ အထူးသဖြင့်လမ်းညွှန်များနှင့်ရှုပ်ထွေးသောဖိုင်ဖွဲ့စည်းပုံများစွာပါ 0 င်သောစီမံကိန်းများတွင်ဤချဉ်းကပ်မှုသည်မလိုအပ်သောအမှားအယွင်းများကိုထိရောက်စွာရှောင်ရှားနိုင်ပြီးသတင်းအချက်အလက်များကိုရှင်းလင်းစွာထုတ်ဖော်ပြောဆိုနိုင်သည်။