duanliang1019 2017-02-24 03:42
浏览 45

Php fopen写入txt文件 - >文件为空

I have the following PHP code:

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<title>mencari link dari hasil google search dan menyimpan ke file</title>
</head>
<body>
<form method="post" action="">
<input name="wewewe" >
<div id="submit">
<input type="submit" />
</div>
</form>
<?php
require_once('simple_html_dom.php');
$myfile = fopen("blogspot.txt", "a+") or die("Unable to open file!");
$url  = $_POST['wewewe'];
$html = file_get_html($url);

$linkObjs = $html->find('li.sidebar-item a');
foreach ($linkObjs as $linkObj) {

    $link  = trim($linkObj->href);

    // if it is not a direct link but url reference found inside it, then extract
    if (!preg_match('/^https?/', $link) && preg_match('/q=(.+)&amp;sa=/U', $link, $matches) && preg_match('/^https?/', $matches[1])) {
        $link = $matches[1];
    } else if (!preg_match('/^https?/', $link)) { // skip if it is not a valid link
        continue;    
    }
    echo   "<a href='". $link ."'>$link</a>". '</p>' ;
    fwrite ($myfile, "<a href='". $link ."'>$link</a>". '</p>'."
") ;

}
fclose($myfile);
?>
</body>
</html>

I'm using:

  • Xampp OK
  • Simplehtmldom1_5 OK
  • Imacros OK

I'm also having another script like this but no needed Imacros but the file is still empty. Can anyone help?

  • 写回答

0条回答 默认 最新

    报告相同问题?

    悬赏问题

    • ¥15 Vue3 大型图片数据拖动排序
    • ¥15 划分vlan后不通了
    • ¥15 GDI处理通道视频时总是带有白色锯齿
    • ¥20 用雷电模拟器安装百达屋apk一直闪退
    • ¥15 算能科技20240506咨询(拒绝大模型回答)
    • ¥15 自适应 AR 模型 参数估计Matlab程序
    • ¥100 角动量包络面如何用MATLAB绘制
    • ¥15 merge函数占用内存过大
    • ¥15 使用EMD去噪处理RML2016数据集时候的原理
    • ¥15 神经网络预测均方误差很小 但是图像上看着差别太大