Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/php/268.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
PHP | fopen()&;fwrite()在我的脚本中不工作?_Php_Scripting_Fopen_Fwrite - Fatal编程技术网

PHP | fopen()&;fwrite()在我的脚本中不工作?

PHP | fopen()&;fwrite()在我的脚本中不工作?,php,scripting,fopen,fwrite,Php,Scripting,Fopen,Fwrite,各位。我有一个运行循环的脚本,它的结构与下面的一样 当我将fopen()放在循环之外时,我没有看到任何数据进入文件。当我在循环的最后两个条件中用fopen()打开文件时,我确实得到了更新,并且可以实时看到它们 问题是这个脚本可以运行很长时间。由于我没有看到输出文件被更新,我甚至不知道它是否工作 如果不行,那为什么不行呢?如何修复呢? 我想关于执行和fopen()关于PHP的工作原理,有一些我不知道的东西 <?php ini_set('max_execution_time', 0);

各位。我有一个运行循环的脚本,它的结构与下面的一样

当我将fopen()放在循环之外时,我没有看到任何数据进入文件。当我在循环的最后两个条件中用fopen()打开文件时,我确实得到了更新,并且可以实时看到它们

问题是这个脚本可以运行很长时间。由于我没有看到输出文件被更新,我甚至不知道它是否工作

如果不行,那为什么不行呢?如何修复呢? 我想关于执行和fopen()关于PHP的工作原理,有一些我不知道的东西

<?php   

ini_set('max_execution_time', 0);

$output_file = fopen("output.txt, "a");

for ($i = 0; 50000 < $max_run; $i) { 

    $url = "http://www.some-website.com/whatever?parameter=value

    $html_file = file_get_contents($url);

    $doc = new DOMDocument();
    @$doc->loadHTML($html_file);

    $xpath = new DOMXpath($doc);

    $extracted_data = $xpath->query('//whatever')[-999]->textContent;

    if (whatever){

        if (-some-condition) { 
            fwrite($output_file, $extracted_data."\n");
        }

        if (-something-else) {
            fwrite($output_file, "other-data"."\n");
        }
    }

}

?>  
您错过了放置

替换

$output_file = fopen("output.txt, "a");
作者:

您的代码应该是:-

ini_set('max_execution_time', 0);
// You have missed " in below line.
$output_file = fopen("output.txt", "a");

for ($i = 0; 50000 < $max_run; $i) { 
    // You have missed " and ; in below line.
    $url = "http://www.some-website.com/whatever?parameter=value";

    $html_file = file_get_contents($url);

    $doc = new DOMDocument();
    @$doc->loadHTML($html_file);

    $xpath = new DOMXpath($doc);

    $extracted_data = $xpath->query('//whatever')[-999]->textContent;
    // Added this line here.
    $extracted_data .= "\n";
    if (whatever){

        if (-some-condition) { 
            //fwrite($output_file, $extracted_data."\n");
            file_put_contents($output_file, $extracted_data);
            // I have used file_put_contents here. fwrite is also fine.
        }

        if (-something-else) {
            //fwrite($output_file, "other-data"."\n");
            file_put_contents($output_file, "other-data"."\n");
            // I have used file_put_contents here. fwrite is also fine.
        }
    }

}
ini\u集('max\u execution\u time',0);
//你在下面的一行中漏掉了。
$output_file=fopen(“output.txt”、“a”);
对于($i=0;50000<$max_run;$i){
//您在下面一行中漏掉了“和”。
$url=”http://www.some-website.com/whatever?parameter=value";
$html\u file=file\u get\u contents($url);
$doc=新的DOMDocument();
@$doc->loadHTML($html\u文件);
$xpath=新的DOMXpath($doc);
$extracted_data=$xpath->query('//whatever')[-999]->textContent;
//在这里添加了这一行。
$extracted_data.=“\n”;
如果(无论什么){
如果(-某些条件){
//fwrite($output_file,$extracted_data.\n”);
文件内容($output文件,$extracted数据);
//我在这里使用了文件内容。fwrite也可以。
}
如果(-其他内容){
//fwrite($output_file,“其他数据”);
文件内容($output文件,“其他数据”);
//我在这里使用了文件内容。fwrite也可以。
}
}
}

希望它能帮助你:)

语法突出显示显示你的打字错误。你忘记了“和”;(分号)在$url语句中。问题:为什么使用PHP4文件函数而不是PHP5(
file\u put\u contents
)?别忘了解释更改的内容。
ini_set('max_execution_time', 0);
// You have missed " in below line.
$output_file = fopen("output.txt", "a");

for ($i = 0; 50000 < $max_run; $i) { 
    // You have missed " and ; in below line.
    $url = "http://www.some-website.com/whatever?parameter=value";

    $html_file = file_get_contents($url);

    $doc = new DOMDocument();
    @$doc->loadHTML($html_file);

    $xpath = new DOMXpath($doc);

    $extracted_data = $xpath->query('//whatever')[-999]->textContent;
    // Added this line here.
    $extracted_data .= "\n";
    if (whatever){

        if (-some-condition) { 
            //fwrite($output_file, $extracted_data."\n");
            file_put_contents($output_file, $extracted_data);
            // I have used file_put_contents here. fwrite is also fine.
        }

        if (-something-else) {
            //fwrite($output_file, "other-data"."\n");
            file_put_contents($output_file, "other-data"."\n");
            // I have used file_put_contents here. fwrite is also fine.
        }
    }

}