2017-06-09 41 views
0

弗里斯特的是,这是我的代码,以便看看:)PHP写入文本文件全部内容

<form method="POST"> 
    <input name="link"> 
    <button type="submit">></button> 
</form> 
<title>GET IMAGES</title> 
<?php 
if (!isset($_POST['link'])) exit(); 
$link = $_POST['link']; 
echo '<div id="pin" style="float:center"><textarea class="text" cols="110" rows="50">'; 
function curl($link) 
{ 
    $ch = curl_init(); 
    curl_setopt($ch, CURLOPT_URL, $link); 
    curl_setopt($ch, CURLOPT_RETURNTRANSFER,true); 
    $result = curl_exec($ch); 
    if(curl_errno($ch)){ 
     return FALSE; 
    } 
    return $result; 
} 
$get=array(); 

//GET TITLE 
    $get[$i] = curl($link); 
    if (preg_match_all('/">(.*?)<\/a><\/h1>/', $get[$i], $matches)) 
    foreach ($matches[1] as $title) 
    $data = "$title\n"; 
    echo $data; 

//GET IMAGES 
for($i=1;$i<=100;$i++) 
{ 
    if ($i == 1) $url = $link; 
    else $url = "$link?p=$i"; 
    $get[$i] = curl($url); 
    if (preg_match_all('/<img id="bigImg" src="(.*?)"/', $get[$i], $matches)) 
    { 
     foreach ($matches[1] as $content) { 
     $content = str_replace("//img","http://img",$content); 
     $data = "<img src=\"".$content."\" />"; 
     echo $data."\r\n"; 
     } 
    } 
    if (!substr_count($get[$i], '下一页')) break; 
} 
file_put_contents("1.txt","$data",FILE_APPEND | LOCK_EX); 
echo '</textarea>'; 
?> 

我收到textarea的结果,当我提出这样的网址:

THIS IS A TITLE 
<img src="https://img.example.com/1.jpg"/> <img 
src="https://img.example.com/2.jpg"/> <img 
src="https://img.example.com/3.jpg"/> 

但是当我使用file_put_contents函数写入文本文件,我打开它来检查的结果,我只得到

<img src="https://img.example.com/3.jpg"/> 

任何想法?

回答

2

您需要追加到$data才能覆盖它。使用$data .=而不是$data =,因为后者会覆盖它。

$data .= "<img src=\"".$content."\" />"; 
// ^append 
+0

是的,我试过这个解决方案,但我觉得它很慢:( – Enuma

+0

你有什么想法让它更快? – Enuma