How to check if a link is a downloadable file in php?

I am trying to make a broken checker link with php. I changed some php code that I found online. I am not a php programmer. This allowed some unbroken links, but everything is in order. However, I have problems with all the presentations, lightning and so on ... Basically, if it is downlaod, then the algorithm considers this a dead link.

<?php
    set_time_limit(0);
    //ini_set('memory_limit','512M');
    $servername = "localhost";
    $username   = "";
    $password   = "";

    try {
        $conn = new PDO("mysql:host=$servername;dbname=test", $username, $password);
        // set the PDO error mode to exception
        $conn->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION);
        echo "Connected successfully" . "<br />";
        echo "----------------------------------------------------<br />";
    }
    catch (PDOException $e) {
        echo "Connection failed: " . $e->getMessage();
    }

    $sql    = "SELECT object,value FROM metadata where xpath = 'lom/technical/location'";
    $result = $conn->query($sql)->fetchAll(PDO::FETCH_ASSOC);
    //print_r($result);

    $array_length = sizeof($result); //26373
    //$array_length = 26373;
    $i            = 0;

    $myfile = fopen("Lom_Link_patikra1.csv", "w") or die("Unable to open file!");
    $menu_juosta = "Objektas;Nuoroda;Klaidos kodas;\n";
    //fwrite($myfile,$menu_juosta);

    for ($i; $i < $array_length; $i++) {
        $new_id           = $result[$i]["object"];
        $sql1             = "SELECT published from objects where id ='$new_id'";
        $result_published = $conn->query($sql1)->fetchAll(PDO::FETCH_ASSOC);
        //print_r ($result_published);                 

        if ($result_published[0]["published"] != 0) {
            $var1             = $result[$i]["value"];
            $var1             = str_replace('|experience|902', '', $var1);
            $var1             = str_replace('|packed_in|897', '', $var1);
            $var1             = str_replace('|packed_in|911', '', $var1);
            $var1             = str_replace('|packed_in|895', '', $var1);
            $request_response = check_url($var1); // Puslapio atsakymas

            if ($request_response != 200) {
                $my_object = $result[$i]["object"] . ";" . $var1 . ";" . $request_response . ";\n";
                fwrite($myfile, $my_object);
            }
        }
    }
    fclose($myfile);
    $conn = null;

    function check_url($url)
    {
        $ch = curl_init();
        curl_setopt($ch, CURLOPT_URL, $url);
        curl_setopt($ch, CURLOPT_HEADER, 1);
        curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
        $data    = curl_exec($ch);
        $headers = curl_getinfo($ch);
        curl_close($ch);
        return $headers['http_code'];
    }

Link example: http://lom.emokykla.lt/MO/Matematika/pazintis_su_erdviniais%20_kunais_1.doc

Any solutions, tips?

Thank you all for your help. Now it works faster. There seems to be a problem with spaces, but this is even intriguing.

, , http-, , . , , , 301 302 - . https://en.wikipedia.org/wiki/List_of_HTTP_status_codes

.

+4
3

CURL

function checkRemoteFile($url)
{
$ch = curl_init();
curl_setopt($ch, CURLOPT_URL,$url);
// don't download content
curl_setopt($ch, CURLOPT_NOBODY, 1);
curl_setopt($ch, CURLOPT_FAILONERROR, 1);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
if(curl_exec($ch)!==FALSE)
{
    return true;
}
else
{
    return false;
}
}

: , , , URL- , , .

function url_exists($url) {
if(@file_get_contents($url,0,NULL,0,1))
{return 1;}
else
{return 0;}
}
+3

curlopt_nobody, TRUE, HTTP HEAD GET, curl_setopt( $ch, CURLOPT_NOBODY, true );

+1

Source: https://habr.com/ru/post/1617194/


All Articles