【问题标题】:php file and fopen functions not workingphp 文件和 fopen 函数不起作用
【发布时间】:2016-02-20 06:47:30
【问题描述】:

我正在努力学习如何创建网络机器人,并且正在阅读 Michael Schrenk 的一本名为 Webbots, Spiders, and Screen Scrapers 的书。在书中,他给出了下载网页的基本机器人的示例代码。我已经完全按照书中的内容复制了代码(无 cmets):

<? 
$target = "http://www.schrenk.com/nostarch/webbots/hello_world.html";
$downloaded_page_array = file($target); 
for($xx=0; $xx<count($downloaded_page_array); $xx++) 
echo $downloaded_page_array[$xx]; 
?>

我将此代码放在一个 php 文件中并上传到我的网站。但是,当我在浏览器中导航到它时,什么也没有发生。它只是加载一个空白页面。无内容。

之前我尝试了作者提供的另一个sn-p,同样,这个是从书中完全复制的,只有这个我并没有真正得到一个空白页,页面只是试图加载直到它最终计时出去。从未得到正确的内容:

$target = "http://www.schrenk.com/nostarch/webbots/hello_world.html";
$file_handle = fopen($target, "r");
while (!feof($file_handle))
echo fgets($file_handle, 4096);
fclose($file_handle);

我检查了 URL 以确保文件存在并且确实存在。我不知道为什么这行不通。我已经阅读了如何使用 file();和 fopen(); PHP 中的函数,但据我所知,它们都被正确使用。我在这里做错了什么?

【问题讨论】:

  • error_reporting(E_ALL); ini_set('display_errors', '1');
  • 另外,使用&lt;?php - 通常&lt;?(php.ini 中的'short_open_tag')被禁用。
  • 请参阅 AbraCadaver 建议。尝试用&lt;?php 替换&lt;?&lt;? 仅在显式配置时有效。您的代码有效。如果错误仍然存​​在,您的 file_get_contents 可能由于某些原因而失败,但按照 AbraCadaver 的建议,您会看到错误。
  • 我已经添加了 AbraCadaver 提供的代码行,以及从
  • 你正确调用了页面?您可以在命令行模式下尝试吗?您确定上面的代码是您文件中的所有代码吗?可能剩余代码有错别字,所以产生编译错误:编译错误只能在apache错误日志或命令行运行脚本中看到。

标签: php fopen bots


【解决方案1】:

通过fopen() 访问网址是一个坏主意。它要求您在 PHP 配置中启用 allow_url_fopen,这为大量漏洞利用打开了大门(主机出于某种原因禁用它)。

尝试改用cURL functions:它们将为您提供更多的灵活性和控制力。 PHP 文档为您提供了一些 great examples 的开头。

【讨论】:

    【解决方案2】:

    不是 fgets($file_handle, 4096) 而是 fread($file_handle, 4096) ;

    $target = "http://www.schrenk.com/nostarch/webbots/hello_world.html";
    $file_handle = fopen($target, "r");
    while (!feof($file_handle))
    echo  fread($file_handle, 4096);
    fclose($file_handle);
    

    然后如果你想从提取的文本中创建一个新文件:

      // extracting text operation
    $target = "http://www.schrenk.com/nostarch/webbots/hello_world.html";
    $file_handle = fopen($target, "r");
    $getText =  fread($file_handle, 4096);
    fclose($file_handle);
    
    // writing file operation
    $writeHandle = fopen ("folder/text.txt","w"); // file will be created if not existed
    $writeFile = fwrite($writeHandle,$getText );
    fclose($writeHandle );
    

    【讨论】:

      【解决方案3】:

      首先,您应该将error_reporting(E_ALL); ini_set('display_errors', '1'); 添加到您的脚本中,以便在您的脚本中显示错误,正如 AbraCadaver 在他的评论中提到的那样。

      一个原因可能是,allow_url_fopen 在您的主机上被禁用。

      此选项启用可识别 URL 的 fopen 包装器,该包装器可以访问 URL 对象(如文件)。为使用 ftp 或 http 协议访问远程文件提供了默认包装器,一些扩展(如 zlib)可能会注册额外的包装器。

      见:http://php.net/manual/en/filesystem.configuration.php#ini.allow-url-fopen

      您可以通过以下方式检查:

      var_dump(ini_get('allow_url_fopen'));
      

      您的脚本需要true 才能正确运行。

      如果allow_url_fopen 不是true1,您可以尝试使用file_get_contents() 加载网址。

      <?php
      $homepage = file_get_contents('http://www.example.com/');
      echo $homepage;
      ?>
      

      见:http://php.net/manual/en/function.file-get-contents.php

      【讨论】:

      • 感谢您花时间回答。我已采纳您的建议并添加了报告错误的代码行。这是返回的内容:string '' (length=0)
      • 因此,出于安全原因,allow_url_fopen 似乎没有被激活/允许。看来您不能使用fopen() 来加载网址。请阅读我的更新答案。
      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2013-04-14
      • 2015-11-20
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多