Version with generators (PHP 5.5 +).
<?php // Читаем файл построчно. function getLines($file) { $f = fopen($file, 'r'); try { while ($line = fgets($f)) { yield $line; } } finally { fclose($f); } } $file = "links.txt"; $link = "http://example.com/image2.png"; $exists = false; // Проверяем ссылку. foreach (getLines($file) as $line) { $exists = $exists || trim($line) === $link; } if (!$exists) { file_put_contents ($file, $link . PHP_EOL, FILE_APPEND); }
Full answer :
<?php function getLines($file) { $f = fopen($file, 'r'); try { while ($line = fgets($f)) { yield $line; } } finally { fclose($f); } } $url = file_get_contents("https://api.vk.com/method/wall.get?domain=*NAME_OF_GROUP*&"); $data = json_decode($url,true); $file="file.txt"; foreach ($data['response'] as $item) { $link = $item['attachment']['photo']['src_big']; $exists = false; foreach (getLines($file) as $line) { $exists = $exists || trim($line) === $link; } if (!$exists) { file_put_contents ($file, $link . PHP_EOL, FILE_APPEND); } }