Menu
PHP AUTO INCLUDE FILE

PHP AUTO INCLUDE FILE

 คนทำเว็บ  2017-03-06 16:18:49  2,828

บทความ : PHP AUTO INCLUDE FILE

ปัญหาก่อนคิด File นี้ขึ้นมาคือเมื่อ File ที่เรา ต้องการ เรียกใช้ อยู่ต่าง Folder กัน ทำให้มีปัญหาเรื่อง PATH เป็นประจำครับ
เลยต้องเขียน Function เพื่อให้หา ROOT อัตโนมัติ จึงได้เป็น Code ออกมาดังนี้ครับ

 
function get_include_file($filename) {
 $string_path = "";
 for($iLoop =0;$iLoop<5;$iLoop++){
 if($iLoop > 0){$string_path = $string_path . "../";}
  if (is_file($string_path . $filename)) {
 ob_start();
 $include_file = $string_path . $filename;
         return $include_file;
 exit;
  }
 }
 return false;
}
require(get_include_file('inc/webconfig.php'));
require(get_include_file('inc/calendar.php'));
?>
สังเกตุว่า จะมีการ เรียก include
สมมุติ folder ที่จำลอง คือ inc/ชื่อไฟล์
ก็สามารถเอาไปใช้ ได้ ทั้ง ใน และ นอก เลย

หากชอบบทความนี้ กดให้ค่ากาแฟที่นี่ครับ Support Me on Ko-fi

ฝากติดตามผลงานด้านล่างด้วยครับ

--------------------------
► Website : https://www.siamfocus.com/
► LINE@ : https://line.me/ti/p/@siamfocus.com
► Facebook : https://www.facebook.com/fanpage.siamfocus
► Twitter : https://twitter.com/siamfocus
► Instagram : https://www.instagram.com/iamtaam
--------------------------

รับทำเว็บไซต์ สอนทำเว็บไซต์ รับดูแลเว็บไซต์

สายด่วนโทร 061-583-7888

บทความแนะนำ