Checking if File exists on remote server

Checking if File exists on remote server

  • (2 Pages)
  • +
  • 1
  • 2

15 Replies - 183187 Views - Last Post: 18 March 2011 - 12:26 PM Rate Topic: -----

#1 morkman  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 2
  • Joined: 18-May 05

Checking if File exists on remote server

Posted 18 May 2005 - 09:16 AM

I am currently working on a site that will need to include an image from a remotes server.

It is a jobsite in the UK JobXpresso. So I am looking at checking for the image first and then displaying if it exists.

Cheers
Is This A Good Question/Topic? 0
  • +

#5 skyhawk133  Icon User is offline

  • Head DIC Head
  • member icon

Reputation: 1877
  • View blog
  • Posts: 20,284
  • Joined: 17-March 01

Re: Checking if File exists on remote server

Posted 18 May 2005 - 09:44 AM

I'm thinking the quickest solution for this is to use the fopen() function... The file_exists() function doesn't support remote URL's, however, fopen() does. You can do something like this

<?
// Set the URL you want to connect to
$url = "http://url.to/file.jpg";

// Check to see if the file exists by trying to open it for read only
if (fopen($url, "r")) {

  echo "File Exists";

} else {

  echo "Can't Connect to File";

}

?>



Quote

If the open fails, the function returns FALSE and an error of level E_WARNING is generated. You may use @ to suppress this warning.

Was This Post Helpful? 1

#6 morkman  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 2
  • Joined: 18-May 05

Re: Checking if File exists on remote server

Posted 18 May 2005 - 09:58 AM

I have tried fopen, however to load the image (or should I say when it checks to see if the image is there) it takes some serious time.

Basically it is going to be for this page
http://www.jobxpress.../agentslist.php

so next to the agent there will be a logo of them. mmm, I will carry on searching, but if you do come up with something else let me know.

Thank you very much for your time.
Was This Post Helpful? 0
  • +
  • -

#7 skyhawk133  Icon User is offline

  • Head DIC Head
  • member icon

Reputation: 1877
  • View blog
  • Posts: 20,284
  • Joined: 17-March 01

Re: Checking if File exists on remote server

Posted 18 May 2005 - 03:10 PM

I was looking yesterday and did find some information on fopen and setting the timeout to like 1 or 2 seconds so it doesn't hold up pages. I think you can also control how many bytes to return with fopen or some similar function, you could pull back 1 byte and then it wouldn't bother downloading the entire thing, just 1 byte to check if it existed.

Search for "file" at php.net and it'll give you all the different File IO functions on the left.

Oh, and welcome to dream.in.code!!!
Was This Post Helpful? 0
  • +
  • -

#8 cyberscribe  Icon User is offline

  • humble.genius
  • member icon

Reputation: 10
  • View blog
  • Posts: 1,062
  • Joined: 05-May 02

Re: Checking if File exists on remote server

Posted 18 May 2005 - 11:53 PM

If these people are signing up for accounts you can have them upload an image or provide a link to an external image, like we do here for avatars. Then, at time of entry you check the URL or file is valid. From then on, you're golden -- just using <img = to do the work.
Was This Post Helpful? 0
  • +
  • -

#9 72dpi  Icon User is offline

  • New D.I.C Head
  • member icon

Reputation: 0
  • View blog
  • Posts: 3
  • Joined: 24-May 05

Re: Checking if File exists on remote server

Posted 24 May 2005 - 01:58 AM

I currently use this:
<?php
// url to your images folder (no trailing slash)
$url = "http://www.your.com/imagefolder";
// check if image exists ($thumbnail is our image variable)
if(file_exists("$url/$thumbnail"))
{
// If yes, print out the image
echo "<img src=\"$url/$thumbnail\" alt=\"$thumbnail\" width=\"75\" height=\"100\" />";
}
// if no, give an alternative image
else
{
echo "<img src=\"$url/noimage.gif\" alt=\"$thumbnail\" width=\"75\" height=\"100\" />";
}
?>



It works nice, but same deal, i don't know about speeds from.
I don't know the difference betweeen fopen & this tho.... I am a bit of a newb. :blink:
Was This Post Helpful? 0
  • +
  • -

#10 BooleanOperator  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 3
  • Joined: 15-September 06

Re: Checking if File exists on remote server

Posted 15 September 2006 - 03:28 PM

View Postcyberscribe, on 18 May, 2005 - 11:53 PM, said:

If these people are signing up for accounts you can have them upload an image or provide a link to an external image, like we do here for avatars. Then, at time of entry you check the URL or file is valid. From then on, you're golden -- just using <img = to do the work.


Hate to drag up an old thread but ....

That doesnt solve the issue if the file has been removed after setup.

Possible Directions:
1. doing this at every request is a significant slow down.
2. doing it incrementally and placing the data of the remote existance locally then at reuqest time check how long since last check, and if needed check again.

I found this thread looking for a good way to check the existance of a remote api.
Still looking, will report back with results later tonight.

This post has been edited by BooleanOperator: 15 September 2006 - 03:29 PM

Was This Post Helpful? 0
  • +
  • -

#11 BooleanOperator  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 3
  • Joined: 15-September 06

Re: Checking if File exists on remote server

Posted 15 September 2006 - 04:17 PM

The following example from php.net solved my issue with sending a small request:

http://www.php.net/m...kopen.php#39948
Was This Post Helpful? 0
  • +
  • -

#12 suleiman  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 2
  • Joined: 24-March 07

Re: Checking if File exists on remote server

Posted 24 March 2007 - 08:58 PM

I am using the following code:

$url = "/wp-content/avatars";
$thumbnail = '/' . get_the_author_ID() . '.jpg';
if(file_exists("$url/$thumbnail")) {
echo "<img src=\"$url/$thumbnail\" alt=\"$thumbnail\" float=\"left\" width=\"75\" height=\"100\" />";
}
else {
echo "<img src=\"$url/default.jpg\" alt=\"$thumbnail\" width=\"75\" height=\"100\" />";
}}


and it's not working. and it's driving me nuts.

If i set the else code to the same as the if code then the url loads up fine, which means that the php code knows the image is there. What am i doing wrong?

[mod edit] Code tags are there for a reason....use them.
Was This Post Helpful? 0
  • +
  • -

#13 suleiman  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 2
  • Joined: 24-March 07

Re: Checking if File exists on remote server

Posted 24 March 2007 - 09:13 PM

So I've had a bit of success replacing "file_exists" with if(fopen("$url$thumbnail", "r")) {

but the problem now is that the fopen command is executing all of the code both within the if brackets and the else brackets. So what I get is output that looks like this:

Warning: fopen() [function.fopen]: HTTP request failed! HTTP/1.1 404 Not Found in /home/..../public_html/wp-content/.../author_profile2.php on line 100

and then the image of my default.jpg file.

If i can only get rid of this fopen error then i'm home free!

EDIT: problem fixed. To those of you that are still mucking about with the same issues, when using file_exists you need to be passing a $path variable that looks like this: "home/.../public_html/...." when using fopen you need a $url variable that, not surprisingly, is actually a url.

This post has been edited by suleiman: 24 March 2007 - 10:04 PM

Was This Post Helpful? 0
  • +
  • -

#14 Styx  Icon User is offline

  • D.I.C Head
  • member icon

Reputation: 5
  • View blog
  • Posts: 192
  • Joined: 04-March 07

Re: Checking if File exists on remote server

Posted 25 March 2007 - 02:06 AM

Something you should note about using file_exists is that you don't put leading slashes when using directories.

Also, in your original script, for your path you put /dir/dir, then for your image you put / . id . jpg, then in file_exists you put $url/$thumbnail, which would resolve to /dir/dir//id.jpg

Other than that, it would work just fine the way it was.

Using fopen is another option, as you found, but not one that requires an actual url. Read more about fopen here.

This post has been edited by Styx: 25 March 2007 - 02:08 AM

Was This Post Helpful? 0
  • +
  • -

#15 jeyaseelansarc  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 1
  • Joined: 10-December 09

Re: Checking if File exists on remote server

Posted 10 December 2009 - 05:04 AM

file_exist or file() or fopen() are not supporting me while i m searching for this same problem..

I can get working with CURL functions to validate the file exist in the remote server
Was This Post Helpful? 0
  • +
  • -

#16 Keiran  Icon User is offline

  • New D.I.C Head
  • member icon

Reputation: 1
  • View blog
  • Posts: 49
  • Joined: 06-December 09

Re: Checking if File exists on remote server

Posted 17 December 2009 - 08:07 PM

try this

<?php

$url='http://www.blah.net/file.jpg';
$addy=parse_url($url);
$addy['port']=isset($addy['port'])?$addy['port']:80;
$sh=fsockopen($addy['host'],$addy['port']) or die('cant open socket');
fputs($sh,"HEAD {$addy['path']} HTTP/1.1\r\nHost: {$addy['host']}\r\n\r\n");

while($line=fgets($sh))
   if(preg_match('/^Content-Length: (d+)/',$line,$m))
      $size=$m[1];

echo isset($size)?"size of $url file is $size": 'no such file: '.$url; 


Was This Post Helpful? 0
  • +
  • -

#17 swashmedia  Icon User is offline

  • New D.I.C Head

Reputation: 2
  • View blog
  • Posts: 1
  • Joined: 09-April 10

Re: Checking if File exists on remote server

Posted 09 April 2010 - 01:19 AM

Apologies if this has been mentioned already, I haven't read the thread fully but at a glance couldn't see it.

I use the following to check for the existence of remote images, its about as simple as they come:

if(@GetImageSize($remoteImageURL)){
    //image exists!
}else{
    //image does not exist.
}


@swashmedia

This post has been edited by swashmedia: 09 April 2010 - 01:21 AM

Was This Post Helpful? 2
  • +
  • -

#18 Guest_Flavio*


Reputation:

Re: Checking if File exists on remote server

Posted 24 April 2010 - 05:50 AM

@GetImageSize($origin)

@fopen($origin, "r")

Both worked for me, but I realized that the get was faster. Tks for the help. :smartass:
Was This Post Helpful? 0

  • (2 Pages)
  • +
  • 1
  • 2