How to check whether an image (physical file) exists on an external web server via an absolute URL

Asked

Viewed 1,170 times

1

I usually use the function file_exists of PHP to do this when I am developing remotely, but now I need to know when the image exists or not in a URL absolute of an external web server.

Editing the question to include more information:

I tested all these methods both on the local server and on a web server other than www.issam.com.br and none returns the correct value, someone knows whether it is possible?

Imagem que existe = http://www.issam.com.br/ximages/produtos/356031.jpg
Imagem que não existe = http://www.issam.com.br/ximages/produtos/356030.jpg

if(file_exists('http://www.issam.com.br/ximages/produtos/356030.jpg')){
   echo"sim";
}else{
  echo"nao";
}


if(file_get_contents('http://www.issam.com.br/ximages/produtos/356030.jpg')) {
   echo "Existe";
}else{
   echo"nao";
}

$arquivo = 'http://www.issam.com.br/ximages/produtos/356030.jpg';
$file_headers = @get_headers($arquivo);
if(!$file_headers || $file_headers[0] == 'HTTP/1.1 404 Not Found') {
   echo 'Arquivo não existe.';
}else {
echo 'Arquivo existe.';
}

$arquivo = 'http://www.issam.com.br/ximages/produtos/356030.jpg';
if (!$fp = curl_init($arquivo)){
   echo 'Arquivo não existe.';
}else{
    echo 'Arquivo existe.';
}

2 answers

3


You can do it in many ways.

In addition to the option quoted by @Lucas Caires, it could be...

Using get_headers:

$arquivo = 'http://www.seu_dominio.com.br/seu_caminho/absoluto/arquivo.jpg';
$file_headers = @get_headers($arquivo);
if(!$file_headers || $file_headers[0] == 'HTTP/1.1 404 Not Found') {
    echo 'Arquivo não existe.';
}else {
     echo 'Arquivo existe.';
}

Using CURL:

$arquivo = 'http://www.seu_dominio.com.br/seu_caminho/absoluto/arquivo.jpg';
if (!$fp = curl_init($arquivo)){
   echo 'Arquivo não existe.';
}else{
   echo 'Arquivo existe.';
}

NOTE: These two options are interesting as they do not download the complete file. Getting much faster.

I hope I’ve helped

  • 2

    PLEASE SEARCH IF THERE ARE NO ANSWERS ON THE SITE THAT ANSWER ANY QUESTIONS, I AM VERY GRATEFUL IF YOU FAVOUR THE EXISTING ANSWERS http://answall.com/q/1819/3635

  • Obs: was not me who denied your answer, but I have already guided you more than once on how to mark the question as duplicate

  • 1

    @Guilhermenascimento... thank you once again for your attention. I will endeavor not to answer before checking.

  • It is not duplicated because the question is for external servers and not own or remote server as Kenny Rafael’s answer says. I had already tried this answer but it does not work. I will try these of friends Lucas Caires and Allan Andrade.

  • Denis, let me know if it works.

  • @Remote server and external server Denis is pretty much the same thing, what do you mean by absolute URL? Would that be it /home/user/teste/image.jpg or would that be http://siteexterno.com/image;jpg or would that be smb://shared/image.jpg. Please find more details.

  • Soon testo Allan. Guilherme would be http://siteexterno.com/image.jpg I tested with file_exists and it didn’t work.

Show 2 more comments

0

You can use the function file_get_contents() for that reason.

<?php
if(file_get_contents('http://www.exemplo.com.br/imagem.jpg')) {
 echo "Existe";
}
?>

Remembering for this function to work, you will need to activate the allow_url_fopen in php.ini. If you cannot activate this, there is an alternative to using the Curl library. Here is an example of use.

Browser other questions tagged

You are not signed in. Login or sign up in order to post.