2012-01-12 15 views
1

ありがとうございます。エラーメッセージを与える.phpファイルから文字セットを取得するPHPコード

警告:コードの下に使用している場合

この警告を取得するのfile_get_contents(test.phpを)を[取得function.file--内容]:中/パス/インデックスそのようなファイルまたはディレクトリ:ストリームをオープンに失敗しました。 php on line so-n-so。

は、ここで私が使用していたコードだ、

<?php 

// Scan directory for files 
$dir = "path/"; 
$files = scandir($dir); 

// Iterate through the list of files 
foreach($files as $file) 
{ 
// Determine info about the file 
$parts = pathinfo($file); 

// If the file extension == php 
if ($parts['extension'] === "php") 
{ 
// Read the contents of the file 
$contents = file_get_contents($file); 

// Find first occurrence of opening template tag 
$from = strpos($contents, "{{{{{"); 

// Find first occurrence of ending template tag 
$to = strpos($contents,"}}}}}"); 

// Pull out the unique name from between the template tags 
$uniqueName = substr($contents, $from+5, $to); 

// Print out the unique name 
echo $uniqueName ."<br/>"; 
} 
} 
?> 

答えて

4

エラーメッセージは、ファイルが見つからないと言っています。

これは、scandir()がディレクトリからファイルの基本名のみを返すためです。ディレクトリ名は含まれません。

$files = glob("$dir/*.php"); 

これは結果リストにパスを返し、またあなたの拡張チェックが冗長になるだろう:あなたは代わりにglob()を使用することができます。

+0

はい、それはファイルを見つけることです。しかし、 'file_get_contents'はそのパスを知らなくてもそれを読み取ることができません。そして、あなたのコードは現在、 '' file.php "'必要な '' path/file.php "'をfile_get_contentsに渡します。したがって、エラーメッセージ。 – mario

+0

ありがとうございました! – KGDD

1

scandir()DOCsで取得したファイルのリストから...を除外する必要があることをお勧めします。

// Iterate through the list of files 
foreach($files as $file) { 
    if('.' == $file or '..' == $file) { 
     continue; 
    } 
... 

また、あなたはあなたのファイル名の前にパスを配置する必要があります:

$contents = file_get_contents($path . $file); 
関連する問題