Enuma
Enuma

Reputation: 61

PHP Write to text file full contents

Frist of all,this is my code so take a look :)

<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>';
?>

The results i receive in the textarea when i submit the URL like this :

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"/>

But when i use file_put_contents function to write to text file and i open it to check the result, i only get

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

Any ideas?

Upvotes: 0

Views: 41

Answers (1)

MrCode
MrCode

Reputation: 64536

You need to append to $data not overwrite it. Use $data .= not $data = because the latter overwrites it.

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

Upvotes: 2

Related Questions