dtkjthe4025 2012-01-12 15:04 采纳率: 0%
浏览 78
已采纳

PHP代码从.php文件中获取字符集给出错误消息

Thanks in advance.

Getting this warning when using below code:

Warning: file_get_contents(test.php) [function.file-get-contents]: failed to open stream: No such file or directory in /path/index.php on line so-n-so.

Here's the code I am using,

<?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/>";
}
}
?>

展开全部

  • 写回答

2条回答 默认 最新

  • duanrong5167 2012-01-12 15:07
    关注

    The error message says that the file isn't found.

    This is because scandir() returns only the basename of the files from your directory. It doesn't include the directory name. You could use glob() instead:

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

    This returns the path in the result list, and would also make your extension check redundant.

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(1条)
编辑
预览

报告相同问题?

手机看
程序员都在用的中文IT技术交流社区

程序员都在用的中文IT技术交流社区

专业的中文 IT 技术社区,与千万技术人共成长

专业的中文 IT 技术社区,与千万技术人共成长

关注【CSDN】视频号,行业资讯、技术分享精彩不断,直播好礼送不停!

关注【CSDN】视频号,行业资讯、技术分享精彩不断,直播好礼送不停!

客服 返回
顶部