Ahmad Waqas
Ahmad Waqas

Reputation: 89

How to check whether file exist or not in given URL?

How to check whether file exist or not in given URL? I can't verify that the file is found or not in a given URL:

$url = "https://content.gomasterkey.com/images/watermark.aspx?imageurl=/uf/1002/property/43644/581330_Image.JPG&width=640&group=1575&module=1&watermarktype=default&position=TopRight";

if(file_exists($url)){
    echo "exist";
}else{
    echo "not exist";
}
if(file_get_contents($url)){
    echo "exist";
}else{
    echo "not exist";
}

Upvotes: 0

Views: 1241

Answers (3)

user969068
user969068

Reputation: 2943

You should use get_headers, because its less overhead for just checking if a file exists

   $headers=get_headers($url);
   if(stripos($headers[0],"200 OK")){
     echo "file exists";
   } else {
     echo "file doesn't exists";
   }

Upvotes: 1

Asim Shahzad
Asim Shahzad

Reputation: 1609

You can use fopen() to check either file exists or not.

$url = "https://content.gomasterkey.com/images/watermark.aspx?imageurl=/uf/1002/property/43644/581330_Image.JPG&width=640&group=1575&module=1&watermarktype=default&position=TopRight";

// Open for reading only.

if(@fopen($url, 'r') ){
  
  echo  'File Found';
    
}else{
    echo 'file not found';   
}

Upvotes: 2

Arslan Butt
Arslan Butt

Reputation: 795

Usinge fopen() function you can check the remote file exists or not.

// Remote file url
$remoteFile = 'https://content.gomasterkey.com/images/watermark.aspx?imageurl=/uf/1002/property/43644/581330_Image.JPG&width=640&group=1575&module=1&watermarktype=default&position=TopRight';

// Open file
$handle = @fopen($remoteFile, 'r');

// Check if file exists
if(!$handle){
    echo 'File not found';
}else{
    echo 'File exists';
}

Upvotes: 1

Related Questions