K
K
Kurusa2018-05-23 02:34:42
PHP
Kurusa, 2018-05-23 02:34:42

php why filesize returns -1?

This method returns the size of the robots.txt file:

//return byte
    $ch = curl_init($url);
    curl_setopt($ch, CURLOPT_NOBODY, 1);
    curl_setopt($ch, CURLOPT_SSL_VERIFYPEER,false);
    curl_setopt($ch, CURLOPT_RETURNTRANSFER, 0);
    curl_setopt($ch, CURLOPT_HEADER, 0);
    curl_setopt($ch, CURLOPT_FOLLOWLOCATION, 1);
    curl_setopt($ch, CURLOPT_MAXREDIRS, 3);
    curl_exec($ch);
    $filesize = curl_getinfo($ch, CURLINFO_CONTENT_LENGTH_DOWNLOAD);
    curl_close($ch);
    if ($filesize) return $filesize;

For all sites, except for one, the normal size is returned to me like 84 or 165 bytes, for example. But in one case -1. Here is another method:
$file = fopen('robots.txt', 'w');

    // инициализация cURL
    $ch = curl_init();

    curl_setopt($ch, CURLOPT_FOLLOWLOCATION, true);
    curl_setopt($ch, CURLOPT_URL, $url);
    curl_setopt($ch, CURLOPT_FILE, $file);
    curl_exec($ch);
    fclose($file);
    curl_close($ch);

    $resultFile = 'robots.txt'; // файл, который получили

    if (!file_exists($resultFile)) {
        // Если файл отсутвует, сообщаем ошибку
        echo "Файл robots.txt отсутствует.";
    } else {
        echo "Файл robots.txt присутствует." . "\n";
        // Начинаем обрабатывать файл, если все прошло успешно
        $textget = file_get_contents($resultFile);
        htmlspecialchars($textget);

        if (preg_match("/Host/", $textget)) {
            echo "Директива Host есть" . "\n";
        } else {
            echo "Директивы Host нет";
        }

        echo 'Размер файла ' . $resultFile . ': ' . $this->remoteFileSize($url . "/robots.txt") . ' байт';
    }

Answer the question

In order to leave comments, you need to log in

1 answer(s)
I
ivankomolin, 2018-05-23
@ivankomolin

curl_getinfo($ch, CURLINFO_CONTENT_LENGTH_DOWNLOAD)
This function returns the size of the downloaded document read from the Content-Length header.
Accordingly, if this is not indicated in the header on the server for robots.txt, then -1 will be returned

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question