Checking if File exists on remote serverChecking if File exists on remote server
15 Replies - 161651 Views - Last Post: 18 March 2011 - 12:26 PM
#1
Checking if File exists on remote server
Posted 18 May 2005 - 09:16 AM
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
#5
Re: Checking if File exists on remote server
Posted 18 May 2005 - 09:44 AM
<?
// 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
#6
Re: Checking if File exists on remote server
Posted 18 May 2005 - 09:58 AM
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.
#7
Re: Checking if File exists on remote server
Posted 18 May 2005 - 03:10 PM
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!!!
#8
Re: Checking if File exists on remote server
Posted 18 May 2005 - 11:53 PM
#9
Re: Checking if File exists on remote server
Posted 24 May 2005 - 01:58 AM
<?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.
#10
Re: Checking if File exists on remote server
Posted 15 September 2006 - 03:28 PM
cyberscribe, on 18 May, 2005 - 11:53 PM, said:
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
#11
Re: Checking if File exists on remote server
Posted 15 September 2006 - 04:17 PM
http://www.php.net/m...kopen.php#39948
#12
Re: Checking if File exists on remote server
Posted 24 March 2007 - 08:58 PM
$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.
#13
Re: Checking if File exists on remote server
Posted 24 March 2007 - 09:13 PM
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
#14
Re: Checking if File exists on remote server
Posted 25 March 2007 - 02:06 AM
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
#15
Re: Checking if File exists on remote server
Posted 10 December 2009 - 05:04 AM
I can get working with CURL functions to validate the file exist in the remote server
#16
Re: Checking if File exists on remote server
Posted 17 December 2009 - 08:07 PM
<?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;
#17
Re: Checking if File exists on remote server
Posted 09 April 2010 - 01:19 AM
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
#18 Guest_Flavio*
Re: Checking if File exists on remote server
Posted 24 April 2010 - 05:50 AM
@fopen($origin, "r")
Both worked for me, but I realized that the get was faster. Tks for the help.
|
|

New Topic/Question
Reply



MultiQuote








|