php 文件和 fopen 函数不工作
php file and fopen functions not working
我正在努力学习如何创建网络机器人,并且我正在阅读 Michael Schrenk 的一本名为 Webbots、Spiders 和 Screen Scrapers 的书。在书中,他给出了下载网页的基本机器人的示例代码。我已经完全按照书中的代码复制了代码(没有注释):
<?
$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 文件并上传到我的网站。但是,当我在浏览器中导航到它时,没有任何反应。它只是加载一个空白页面。无内容。
早些时候我尝试了作者提供的另一个片段,同样,这个片段是从书中完全复制的,只有这个我并没有真正得到一个空白页面,该页面只是尝试加载直到它最终计时出去。从未得到正确的内容:
$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');
添加到您的脚本中,以便在您的脚本中显示错误,正如 AbraCadaver 在他的评论中提到的那样。
一个原因可能是,allow_url_fopen
在您的主机上被禁用。
This option enables the URL-aware fopen wrappers that enable accessing URL object like files. Default wrappers are provided for the access of remote files using the ftp or http protocol, some extensions like zlib may register additional wrappers.
参见:http://php.net/manual/en/filesystem.configuration.php#ini.allow-url-fopen
您可以通过以下方式查看:
var_dump(ini_get('allow_url_fopen'));
您的脚本需要 true
才能 运行 正确。
如果 allow_url_fopen
不是 true
或 1
您可以尝试使用 file_get_contents()
加载 url.
<?php
$homepage = file_get_contents('http://www.example.com/');
echo $homepage;
?>
不是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 );
通过 fopen()
访问 URL 是一个 坏主意 。它要求您在 PHP 配置中启用 allow_url_fopen
,这为大量攻击打开了大门(托管商禁用它是有原因的)。
尝试使用 cURL functions instead: they will give you much more flexibility and control. PHP documentation gives you some great examples 开始。
我正在努力学习如何创建网络机器人,并且我正在阅读 Michael Schrenk 的一本名为 Webbots、Spiders 和 Screen Scrapers 的书。在书中,他给出了下载网页的基本机器人的示例代码。我已经完全按照书中的代码复制了代码(没有注释):
<?
$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 文件并上传到我的网站。但是,当我在浏览器中导航到它时,没有任何反应。它只是加载一个空白页面。无内容。
早些时候我尝试了作者提供的另一个片段,同样,这个片段是从书中完全复制的,只有这个我并没有真正得到一个空白页面,该页面只是尝试加载直到它最终计时出去。从未得到正确的内容:
$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');
添加到您的脚本中,以便在您的脚本中显示错误,正如 AbraCadaver 在他的评论中提到的那样。
一个原因可能是,allow_url_fopen
在您的主机上被禁用。
This option enables the URL-aware fopen wrappers that enable accessing URL object like files. Default wrappers are provided for the access of remote files using the ftp or http protocol, some extensions like zlib may register additional wrappers.
参见:http://php.net/manual/en/filesystem.configuration.php#ini.allow-url-fopen
您可以通过以下方式查看:
var_dump(ini_get('allow_url_fopen'));
您的脚本需要 true
才能 运行 正确。
如果 allow_url_fopen
不是 true
或 1
您可以尝试使用 file_get_contents()
加载 url.
<?php
$homepage = file_get_contents('http://www.example.com/');
echo $homepage;
?>
不是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 );
通过 fopen()
访问 URL 是一个 坏主意 。它要求您在 PHP 配置中启用 allow_url_fopen
,这为大量攻击打开了大门(托管商禁用它是有原因的)。
尝试使用 cURL functions instead: they will give you much more flexibility and control. PHP documentation gives you some great examples 开始。