P
P
Pavl-852020-07-01 17:01:08
PHP
Pavl-85, 2020-07-01 17:01:08

Why doesn't the script create an image file in the image folder when parsing from a site?

Good afternoon!
I create a script for parsing from the site.
The script should: save the names of countries and the paths to the pictures to the database (this can be done). Also, you need to save the gif images to the image folder.
Many lines appear in the browser with the following error:
Warning: file_put_contents(image/FLAG-small/abhaziya.gif): failed to open stream: No such file or directory in C:\OSPanel\domains\test\www\rusakov\parsing17. php on line 36
And the gif file is not written to the image folder, it remains empty. Why is it so? The image folder exists and it is in the same folder as the script.
Here is the whole code:

<?php
require 'C:\OSPanel\vendor\autoload.php';

$host = 'localhost'; // адрес сервера
$database = 'mysite'; // имя базы данных
$user = 'root'; // имя пользователя
$password = ''; // пароль

// подключаемся к серверу
$link = mysqli_connect($host, $user, $password, $database)
or die("Ошибка " . mysqli_error($link));

$url = 'https://33tura.ru/flagi';


function getCurl($url){
    $curl = curl_init();
    curl_setopt($curl,CURLOPT_URL, $url);
    curl_setopt($curl,CURLOPT_RETURNTRANSFER, 1);
    curl_setopt($curl,CURLOPT_FOLLOWLOCATION, 1);
    curl_setopt($curl, CURLOPT_SSL_VERIFYPEER, false);
    curl_setopt($curl, CURLOPT_FOLLOWLOCATION, 0);
    $result = curl_exec($curl);
    return $result;
}

$curlPage = getCurl($url);
$pq = phpQuery::newDocument($curlPage);
$data = $pq->find('div.tur-article tbody tr td table tbody tr');
foreach($data as $elem){
    $countryName = pq($elem)->find('a:eq(1)')->text();
    $imgFlag = pq($elem)->find('a:eq(0) img')->attr('src');
    $img = getCurl('https://33tura.ru'.$imgFlag);
    if(!file_exists("image".$imgFlag)){
        file_put_contents("image".$imgFlag, $img);
    }
    $query="SELECT name FROM content3 WHERE name='".$countryName."'";
    $result= mysqli_query($link,$query);
    $data = mysqli_fetch_assoc($result);
    if(empty($data)){
        $query="INSERT INTO content3 SET name='".$countryName."', img='image".$imgFlag."'";
        mysqli_query($link,$query)or die(mysqli_error($link));
    }
}
?>

Answer the question

In order to leave comments, you need to log in

2 answer(s)
S
Stalker_RED, 2020-07-01
@Pavl-85

probably the FLAG-small folder is missing in the image folder

$dir = dirname(realpath($filename)); // получаем имя папки
if (!is_dir($dir)) mkdir($dir, 0755, true); // создаем 
// и только потом file_put_contents

F
format_iai, 2020-07-01
@format_iai

Here are the parameters for curl, you have problems with SSL
$ch = curl_init();
curl_setopt($ch, CURLOPT_URL, $urlFrom);
curl_setopt($ch, CURLOPT_VERBOSE, 1);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
curl_setopt($ch, CURLOPT_AUTOREFERER, false);
curl_setopt($ch, CURLOPT_HTTP_VERSION, CURL_HTTP_VERSION_1_1);
curl_setopt($ch, CURLOPT_HEADER, 0);
curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, 0);
curl_setopt($ch, CURLOPT_SSL_VERIFYHOST, 0);
curl_setopt($ch, CURLOPT_TIMEOUT, 10);

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question