Good day. Tell me please. There is a PHP script which, when running, parses 10 links to pictures from the wall of one group in VK and writes them to a file on ftp.

I need to make a condition with checking that the script would link the link, go into the file and check if there already is one, if there is, then skip it, if not, write to the others.

Thanks in advance

  • this is a check for all lines for each link, quite an expensive procedure if there are a lot of lines in the file - nueq
  • Maybe you will have another idea how this can be implemented? I would be grateful - Vladimir V.
  • There are no ideas, but how to realize the above described already I am writing in response - nueq
  • And how many pictures for example for a month? What version of PHP on the server? Why not with a database? - E_p
  • Of course, it would be better to do everything through the database, but first, the file is enough for me. Not many pictures are expected, up to 100 pieces - Vladimir V.

2 answers 2

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); } } 
  • Everything is exactly as you wrote, but it does not work :( PHP Parse error: syntax error, unexpected '$ line' (T_VARIABLE). In line 41 (namely while ($ line = fgets ($ f))). - Vladimir V.
  • @ VladimirV. Corrected my code. Launched, works. Make sure the file exists! I lacked ; , but in a completely different line. Check your code or add to the question I can look. - E_p
  • @ VladimirV. You probably missed the line above ; or something like that. - E_p
  • @ VladimirV. Try to completely copy and run. And then edit. You can start without a server from the command line. php file.php . PS: I tried under 5.6 - E_p
  • I also have 5.6 I added my code to the question - Vladimir V.
 <?php //Тут в переменную записываем вашу ссылку (в цикле, которым парсится все) $str="http://link.ru"; //Открываем ваш файл с ссылками $file=file("link.txt"); //Проверяем наличие $str в $file if(!in_array($str,$file)){ //если нет такой,пишите в файл строку, тем способом каким и записывали ранее, если есть - ничего не происходит } ?> 
  • The answer is correct, but if the PHP version allows, then generators should be used. They will be faster and do not use memory. - E_p
  • Of course, but as far as I know, not all hosting works with PHP since version 5.5 (generator support) - nueq
  • PHP 5.5 Today it is not even updated and is not supported, it is necessary to run from the hosting that has not been updated. - E_p