作成日:2018-08-13, 更新日:2018-08-14
読み込むPHPファイルのサンプル
▼readonly.php(読み込むPHPファイル)
<?php echo 'hello';
このファイルを「file_get_contents()」したときにPHPソースをまるっと出力させるのか、実行結果の「hello」を出力させるのかで異なる。
PHPソースとして取得
<?php
echo @file_get_contents('readonly.php');
▼出力結果
<?php echo 'hello';
PHPの実行結果として取得
<?php
function ofGetContents($path='') {
$contents = '';
// 出力バッファリングを開始
ob_start();
// 出力バッファに外部ファイルを読み込む
include_once($path);
// 出力バッファの内容を変数に入れる
$contents = ob_get_contents();
// 出力バッファリングを終了
ob_end_clean();
return $contents;
}
echo ofGetContents('readonly.php');
▼出力結果
hello
引数を使いたい
▼使いたいデータたちを「ofGetContents()」の引数で受けとる
function ofGetContents($path='', $prms=array()) {
→「include_once($path)」内で「$prms」が使える。
PHPの実行結果として取得:URL指定
URL指定するといけるっぽい。サーバの設定次第っていう噂もアリ。
<?php
echo @file_get_contents('http://〇〇〇/readonly.php');