Chat LIVE With Programming Experts! There Are 23 Online Right Now...

Welcome to Dream.In.Code
Become a PHP Expert!

Join 244,060 PHP Programmers for FREE! Get instant access to thousands of PHP experts, tutorials, code snippets, and more! There are 1,392 people online right now. Registration is fast and FREE... Join Now!




Checking if File exists on remote server

 
Reply to this topicStart new topic

Checking if File exists on remote server, Checking if File exists on remote server

morkman
18 May, 2005 - 08:16 AM
Post #1

New D.I.C Head
*

Joined: 18 May, 2005
Posts: 2

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

User is offlineProfile CardPM
+Quote Post


skyhawk133
RE: Checking If File Exists On Remote Server
18 May, 2005 - 08:44 AM
Post #2

Head DIC Head
Group Icon

Joined: 17 Mar, 2001
Posts: 16,437



Thanked: 117 times
Dream Kudos: 1650
Expert In: Web Development

My Contributions
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

CODE

<?
// 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.

User is offlineProfile CardPM
+Quote Post

morkman
RE: Checking If File Exists On Remote Server
18 May, 2005 - 08:58 AM
Post #3

New D.I.C Head
*

Joined: 18 May, 2005
Posts: 2

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.jobxpresso.com/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.
User is offlineProfile CardPM
+Quote Post

skyhawk133
RE: Checking If File Exists On Remote Server
18 May, 2005 - 02:10 PM
Post #4

Head DIC Head
Group Icon

Joined: 17 Mar, 2001
Posts: 16,437



Thanked: 117 times
Dream Kudos: 1650
Expert In: Web Development

My Contributions
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!!!
User is offlineProfile CardPM
+Quote Post

cyberscribe
RE: Checking If File Exists On Remote Server
18 May, 2005 - 10:53 PM
Post #5

humble.genius
Group Icon

Joined: 5 May, 2002
Posts: 1,062



Thanked: 7 times
Dream Kudos: 154
My Contributions
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.
User is offlineProfile CardPM
+Quote Post

72dpi
RE: Checking If File Exists On Remote Server
24 May, 2005 - 12:58 AM
Post #6

New D.I.C Head
Group Icon

Joined: 24 May, 2005
Posts: 3


Dream Kudos: 25
My Contributions
I currently use this:
CODE

<?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.gif
User is offlineProfile CardPM
+Quote Post

BooleanOperator
RE: Checking If File Exists On Remote Server
15 Sep, 2006 - 02:28 PM
Post #7

New D.I.C Head
*

Joined: 15 Sep, 2006
Posts: 3


My Contributions
QUOTE(cyberscribe @ 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.


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 Sep, 2006 - 02:29 PM
User is offlineProfile CardPM
+Quote Post

BooleanOperator
RE: Checking If File Exists On Remote Server
15 Sep, 2006 - 03:17 PM
Post #8

New D.I.C Head
*

Joined: 15 Sep, 2006
Posts: 3


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

http://www.php.net/manual/en/function.fsockopen.php#39948
User is offlineProfile CardPM
+Quote Post

suleiman
RE: Checking If File Exists On Remote Server
24 Mar, 2007 - 07:58 PM
Post #9

New D.I.C Head
*

Joined: 24 Mar, 2007
Posts: 2


My Contributions
I am using the following code:

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.
User is offlineProfile CardPM
+Quote Post

suleiman
RE: Checking If File Exists On Remote Server
24 Mar, 2007 - 08:13 PM
Post #10

New D.I.C Head
*

Joined: 24 Mar, 2007
Posts: 2


My Contributions
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 Mar, 2007 - 09:04 PM
User is offlineProfile CardPM
+Quote Post

Styx
RE: Checking If File Exists On Remote Server
25 Mar, 2007 - 01:06 AM
Post #11

D.I.C Head
Group Icon

Joined: 4 Mar, 2007
Posts: 192



Thanked: 2 times
Dream Kudos: 225
My Contributions
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 Mar, 2007 - 01:08 AM
User is offlineProfile CardPM
+Quote Post

Fast ReplyReply to this topicStart new topic

Time is now: 7/3/09 11:12PM

Live PHP Help!

Be Social

Dream.In.Code RSS Feed Dream.In.Code LinkedIn Group Follow Us On Twitter Fan Us On Facebook

PHP Tutorials

Reference Sheets

PHP Snippets

DIC Chatroom

Bye Bye Ads

Monthly Drawing

Thumb Drive

Top Contributors

Top 10 Kudos This Month