Check if remote file exists, is slow, PHP

0

I'm having trouble checking with remote files, the structure looks like this:

function endereco_existe($url) {  
$h = get_headers($url);
$status = array();
preg_match('/HTTP\/.* ([0-9]+) .*/', $h[0] , $status);
$result = $status[1];
if($result == "200")
{
    return true;//verdadeiro
}else 
{
    return false;//falso
}
}

This function works perfectly, but what happens when I call this function many times leaves the page very slow and does not open, I did the test taking this function and the problem does not appear,

This function is used in this context:

function lista($ini,$fin,$url,$ext,$op=0,$stp=1)
{
        echo "<p id='rep'></p>";

        echo "<script>function rep(url,i){var t = '<style>div.img{position: relative; width: 100%;}div.img > img{position: absolute; right: 3%; margin-right: 0px; top: 4%; margin-top: 0px; background-color: ; width:12%;opacity: 0.2; filter: alpha(opacity=20);}</style><div style=text-align:center; id=top>Lista '+i+'</div><div style=text-align:right;><a href=javascript:void(0); onclick=fechar();>[Fechar]</a></div><div class=img><video width=100% controls><source src='+url+' type=video/mp4>Seu Navegador não Suporta Repoduzir esse video baixe o Firefox ou Google Chrome</video><img src=logo.png></div>';document.getElementById('rep').innerHTML = t;} function fechar(){document.getElementById('rep').innerHTML =' ';}</script>";
        for($i=$ini;$i<=$fin;$i++)
        {
        if($i<10)
        {
        $i = "0".$i;
        }
        if($stp==1)
        {
            if(endereco_existe($url.$i.$ext)==false)
            { break;}
        }

        echo "Lista 1".($i-$op)."&nbsp";
        echo "<a href='#top' onclick='rep('".$url.$i.$ext."',".($i-$op).")'>Repoduzir</a>";
        echo " | ";
        echo "<a download='Lista ".$i.".mp4' href='".$url.$i.$ext."' target='_blank'>Baixar</a>";
        echo "<br />";
        }
}

It lists some videos, passed to the function that are listed by the suffix 01.02 and by that goes that are determined in the "for" (www.examplo.com/video01.mp4), I would like to know if there is any solution in this check if the file exists and in the listing thereof?

    
asked by anonymous 03.10.2016 / 16:45

1 answer

0

What you are doing is not checking files but checking URLs, each URL checked is a different connection and download, varying according to content and size, this can take a lot of time, since you will have to read content by content:

<?php
function endereco_existe($url) {
      $ch = curl_init($value);

      curl_setopt($ch, CURLOPT_NOBODY, true); //não traz o corpo

      //Desabilitei a checagem SSL, mas é preferivel configurar no php.ini
      curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, false);
      curl_setopt($ch, CURLOPT_SSL_VERIFYHOST, false);

      curl_setopt($ch, CURLOPT_RETURNTRANSFER, false); //não retorna a transferencia

      curl_exec($ch);

      return curl_getinfo($ch[$key], CURLINFO_HTTP_CODE) == 200;
}

But keep in mind that this does not get much better, it only helps a little, you can still try sub-connections, like this (as the example link ):

// Todas url gravadas em array
$url = array();
$url[] = 'http://www.link1.com.br';
$url[] = 'https://www.link2.com.br';
$url[] = 'https://www.link3.com.br';

// Setando opção padrão para todas url e adicionando a fila para processamento
$mh = curl_multi_init();
foreach($url as $key => $value){
  $ch[$key] = curl_init($value);
  curl_setopt($ch[$key], CURLOPT_NOBODY, true);
  curl_setopt($ch[$key], CURLOPT_HEADER, true);
  curl_setopt($ch[$key], CURLOPT_RETURNTRANSFER, true);
  curl_setopt($ch[$key], CURLOPT_SSL_VERIFYPEER, false);
  curl_setopt($ch[$key], CURLOPT_SSL_VERIFYHOST, false);

  curl_multi_add_handle($mh,$ch[$key]);
}

// Executando consulta
do {
  curl_multi_exec($mh, $running);
  curl_multi_select($mh);
} while ($running > 0);

// Obtendo dados de todas as consultas e retirando da fila
foreach(array_keys($ch) as $key) {
  $url = curl_getinfo($ch[$key], CURLINFO_EFFECTIVE_URL);

  if (curl_getinfo($ch[$key], CURLINFO_HTTP_CODE) == 200) {
      echo $url, ' está disponivel';
  } else {
      echo $url, ' está inacessível';
  }

  curl_multi_remove_handle($mh, $ch[$key]);
}

// Finalizando
curl_multi_close($mh);
    
03.10.2016 / 17:54