-2

I would like to check whether some files exist in some URLs. The two arrays here are independent. First I want to check the existence of each file from the $files array in url1. If file/files exist in url1, echo "found these files". If not, check the existence of files in url2 and so on.

If none of the files doesn't exist in any of the urls, echo "no file found".

I have written some codes to start with and now I need your help.

$urls  = array('url1','url2','url3');

$files = array('file1','file2','file3');

foreach ($urls as $url) & foreach($files as $file)
{
$ch = curl_init($url);
curl_setopt($ch, CURLOPT_NOBODY, true);
curl_exec($ch);
$retcode = curl_getinfo($ch, CURLINFO_HTTP_CODE);
curl_close($ch);

    if($retcode==200)
    {
    echo "found these files";
    }else{
    "no file found"
    }
}
  • You've only supplied pseudocode. Have you made an actual attempt at reaching the solution? – El_Vanja Apr 04 '20 at 12:56
  • your need to do a [HEAD request](https://stackoverflow.com/questions/1545432/what-is-the-easiest-way-to-use-the-head-command-of-http-in-php) if its offsite, if its not then just use file_exists – Lawrence Cherone Apr 04 '20 at 12:56
  • Hi, I have updated the code. –  Apr 04 '20 at 13:03

2 Answers2

0
    $urls  = array('url1','url2','url3');
    $files =  array('url1','url2','url3');

    foreach ($files as $key => $single_file)
    {
        if(in_array($single_file, $urls))
        {
            echo "found these files";
        }else{
            echo "no file found";
        }
    }
Mustak_Talukder
  • 125
  • 1
  • 8
0

I found the following code helpful.

<?
$urls  = array('url1','url2','url3');

$files = array('file1','file2','file3');
foreach ($urls as $url)
{
    foreach ($files as $fil)
    {
$ch = curl_init($url);
curl_setopt($ch, CURLOPT_NOBODY, true);
curl_exec($ch);
$retcode = curl_getinfo($ch, CURLINFO_HTTP_CODE);
curl_close($ch);

    if($retcode==200)
    {
    echo "found these files";
    }else{
    echo "no file found";
    }
}
}
?>