Jump to content

Recommended Posts

My goal is simple. I want to check a URL for an image. If the image is there, do this. If not, do this. In its simplest form, I cannot get it to work. Any help is appreciated. Running VPS, PHP 5.1.6. The image is there, but its still saying image does not exist.

 

http://www.communitychurch.cc/if-else-test2.php

 

<?php

$filename = 'http://www.communitychurch.cc/site-uploads/about/header-image.jpg';

if (file_exists($filename)) {
   echo "The file $filename exists";
} else {
   echo "The file $filename does not exist";
}

?> 

Link to comment
https://forums.phpfreaks.com/topic/241983-file_exists-function-not-working/
Share on other sites

Your $filename isn't a filename, it's a URL. You cannot use a http URL in the file_exists function. You must use a file system path as an argument to the file_exists function.

 

Is this image located on your server or some other server?

Your $filename isn't a filename, it's a URL. You cannot use a http URL in the file_exists function. You must use a file system path as an argument to the file_exists function.

 

I thought this was only true if the allow_url_fopen were turned off. Just did a test, looks like you are right :)

 

Either or, I would do it with cURL Check here for a function that should do what you want: http://php.net/manual/en/function.file-exists.php#85246

Honestly, I got a little intimidated and wasn't sure what to do with it. I gave it a try and it works like a charm. Thank you all!

 

Final Code:

 

<?php

function url_exists($url) {
    // Version 4.x supported
    $handle   = curl_init($url);
    if (false === $handle)
    {
        return false;
    }
    curl_setopt($handle, CURLOPT_HEADER, false);
    curl_setopt($handle, CURLOPT_FAILONERROR, true);  // this works
    curl_setopt($handle, CURLOPT_HTTPHEADER, Array("User-Agent: Mozilla/5.0 (Windows; U; Windows NT 5.1; en-US; rv:1.8.1.15) Gecko/20080623 Firefox/2.0.0.15") ); // request as if Firefox   
    curl_setopt($handle, CURLOPT_NOBODY, true);
    curl_setopt($handle, CURLOPT_RETURNTRANSFER, false);
    $connectable = curl_exec($handle);
    curl_close($handle);  
    return $connectable;
}

$imageURL = 'http://www.communitychurch.cc/site-uploads/about/header-image.jpg';

if (url_exists($imageURL)) {
   echo "The file $imageURL exists";
} else {
   echo "The file $imageURL does not exist";
}

?> 

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.