Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/cocoa/3.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/8/meteor/3.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
如何在php中检查链接是否为可下载文件?_Php_Broken Links - Fatal编程技术网

如何在php中检查链接是否为可下载文件?

如何在php中检查链接是否为可下载文件?,php,broken-links,Php,Broken Links,我正在尝试用php制作断开的链接检查器。 我修改了一些在网上找到的php代码,我不是php程序员。 它让我们在一些未中断的链接,但这是好的。 但是我对所有的展示、拉链等都有问题。。。 基本上,如果它是向下的,那么算法认为它是一个死链接 <?php set_time_limit(0); //ini_set('memory_limit','512M'); $servername = "localhost"; $username = ""; $pass

我正在尝试用php制作断开的链接检查器。 我修改了一些在网上找到的php代码,我不是php程序员。 它让我们在一些未中断的链接,但这是好的。 但是我对所有的展示、拉链等都有问题。。。 基本上,如果它是向下的,那么算法认为它是一个死链接

<?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'];
    }

尝试使用file\u exists方法:

curlopt\u nobody设置为TRUE会发出HTTP头请求而不是GET请求,因此尝试使用
curl\u setopt($ch,curlopt\u nobody,TRUE)

对远程文件使用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;}
}

如果资源是外部的,我假设它是,链接是外部的。谢谢,现在检查它。至少它比我的好用。将看到结果。效果更好,但仍然将文件的链接计数为dead one=false。您提供的链接中的文件不存在。我下载了它来验证它,但我没有打开。如果该文件存在但无效或为空,则您仍可以使用上述代码检查该文件,下载该文件,然后您必须使用其他函数或脚本验证其完整性。我编辑了答案。我可能误解了您试图实现的目标,但我希望它能有所帮助。谢谢您,我将尝试了解更多有关它的信息。似乎问题可能出在%20的内部单词中。