Search code examples
phplinuxnusoap

Checking directory or file from external owned server


I am developing a script for a music company in PHP that has different servers so they need to display a file if it exists or not on the external server

like they have 3 versions of each music file mp3 mp4 etc ..... and they are accessing the files (each version ) from there specific external server . i have made three solutions for it all of them worked like charm but they are making the server slow .

First Method :

$handle = curl_init($url);
curl_setopt($handle,  CURLOPT_RETURNTRANSFER, TRUE);

/* Get the HTML or whatever is linked in $url. */
$response = curl_exec($handle);

/* Check for 404 (file not found). */
$httpCode = curl_getinfo($handle, CURLINFO_HTTP_CODE);
if($httpCode == 404) {
    /* Handle 404 here. */
}

curl_close($handle);

/* Handle $response here. */

Second Method : Using NuSOAP i made an api which checks internally the file and returns yes/no

Third Method:

function checkurl($url)
{
    return true;
    $file_headers = @get_headers($url);
    //var_dump($file_headers);

    if($file_headers[0] == 'HTTP/1.1 302 Moved Temporarily' || $file_headers[0] =='HTTP/1.1 302 Found') {
        $exists = false;

    }
    else {
        $exists = true;
    }
    return $exists;
}

So i need a solution that doesn't makes the server slow any suggestions


Solution

  • I thought it works with above answers but it wasnt working where there were too many requests so i finally try again and again and found this solution its working perfectly actually the problem was redirects too many of them so i set time_out 15 in curl and it worked

                            $ch = curl_init();
    
    curl_setopt($ch, CURLOPT_URL,            $url);
    curl_setopt($ch, CURLOPT_HEADER,         true);
    curl_setopt($ch, CURLOPT_NOBODY,         true);
    curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
    curl_setopt($ch, CURLOPT_TIMEOUT,        15);
    
    $r = curl_exec($ch);
    $r = split("\n", $r);
    var_dump($r);