Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to check if a file exist on an external server

Tags:

php

How to check if a file exist on an External Server? I have a url "http://logs.com/logs/log.csv" and I have an script on another server to check if this file exists. I tried

$handle = fopen("http://logs.com/logs/log.csv","r");
if($handle === true){
return true;
}else{
return false;
}

and

if(file_exists("http://logs.com/logs/log.csv")){
return true;
}else{
return false;
}

These methos just do not work

like image 256
Elitmiar Avatar asked Feb 18 '10 12:02

Elitmiar


People also ask

How to check if a remote file exists in PHP?

The file_exists() function in PHP, is used to check if a file or directory exists on the server. But the file_exists() function will not usable if you want to check the file existence on the remote server. The fopen() function is the easiest solution to check if a file URL exists on a remote server using PHP.

Which function is used to check file existence?

The file_exists() function checks whether a file or directory exists.

How can check file exist in PHP server?

The file_exists() function in PHP is an inbuilt function which is used to check whether a file or directory exists or not. The path of the file or directory you want to check is passed as a parameter to the file_exists() function which returns True on success and False on failure.

What is file exist?

Exists(String) is an inbuilt File class method that is used to determine whether the specified file exists or not. This method returns true if the caller has the required permissions and path contains the name of an existing file; otherwise, false. Also, if the path is null, then this method returns false.


4 Answers

function checkExternalFile($url)
{
    $ch = curl_init($url);
    curl_setopt($ch, CURLOPT_NOBODY, true);
    curl_exec($ch);
    $retCode = curl_getinfo($ch, CURLINFO_HTTP_CODE);
    curl_close($ch);

    return $retCode;
}

$fileExists = checkExternalFile("http://example.com/your/url/here.jpg");

// $fileExists > 400 = not found
// $fileExists = 200 = found.
like image 58
kipzes Avatar answered Oct 17 '22 00:10

kipzes


  1. Make sure error reporting is on.

  2. Use if($handle)

  3. Check allow_url_fopen is true.

  4. If none of this works, use this method on the file_exists page.

like image 25
Skilldrick Avatar answered Oct 17 '22 01:10

Skilldrick


This should work:

$contents = file_get_contents("http://logs.com/logs/log.csv");

if (strlen($contents))
{
  return true; // yes it does exist
}
else
{
  return false; // oops
}

Note: This assumes file is not empty

like image 26
Sarfraz Avatar answered Oct 17 '22 01:10

Sarfraz


  <?php
    $ch = curl_init();
    curl_setopt($ch, CURLOPT_URL, 4file dir);
    curl_setopt($ch, CURLOPT_HEADER, true);
    curl_setopt($ch, CURLOPT_NOBODY, true);
    curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
    curl_setopt($ch, CURLOPT_FOLLOWLOCATION, true);
    curl_setopt($ch, CURLOPT_MAXREDIRS, 10);

    $data = curl_exec($ch);
    curl_close($ch);

    preg_match_all("/HTTP\/1\.[1|0]\s(\d{3})/",$data,$matches); //check for HTTP headers

    $code = end($matches[1]);

    if(!$data) 
    {
        echo "file could not be found";
    } 
    else 
    {
        if($code == 200) 
        {
            echo "file found";
        } 
        elseif($code == 404) 
        {
            echo "file not found";
        }
    }
  ?>
like image 30
Adam Kiss Avatar answered Oct 16 '22 23:10

Adam Kiss