# PHP check if file in another web server exists

**URL:** https://forum.kirupa.com/t/php-check-if-file-in-another-web-server-exists/263097
**Category:** programming
**Created:** [June 12, 2008, 3:44pm UTC](https://forum.kirupa.com/t/php-check-if-file-in-another-web-server-exists/263097 "2008-06-12T15:44:02Z")
**Posts on this page:** 11
**Page:** 1

<div class="post-metadata">

### Author: ![imagined](https://avatars.discourse-cdn.com/v4/letter/i/a87d85/32.png) [@imagined](https://forum.kirupa.com/u/imagined)
#### Post date: [June 12, 2008, 3:44pm UTC](https://forum.kirupa.com/t/php-check-if-file-in-another-web-server-exists/263097/1 "2008-06-12T15:44:02Z")

</div>

im programming a photo gallery that will grab thumbnails and photos that are hosted in photobucket. photobucket gives you the direct link to your photo files.

now, i programmed the little script using file\_exists, the path to the image is fine, but i guess file\_exists does not work on files in other web servers.

is there a function that will allow me to see if a file exists in another web server?

---

<div class="post-metadata">

### Author: ![sekasi](https://avatars.discourse-cdn.com/v4/letter/s/5f8ce5/32.png) [@sekasi](https://forum.kirupa.com/u/sekasi)
#### Post date: [June 12, 2008, 3:47pm UTC](https://forum.kirupa.com/t/php-check-if-file-in-another-web-server-exists/263097/2 "2008-06-12T15:47:26Z")

</div>

fopen will return false if you cant open it (allow\_URL\_FOPEN) gotta be true in the ini file tho…

---

<div class="post-metadata">

### Author: ![imagined](https://avatars.discourse-cdn.com/v4/letter/i/a87d85/32.png) [@imagined](https://forum.kirupa.com/u/imagined)
#### Post date: [June 12, 2008, 3:57pm UTC](https://forum.kirupa.com/t/php-check-if-file-in-another-web-server-exists/263097/3 "2008-06-12T15:57:01Z")

</div>

it tells me that URL file-access is disabled in the server configuration. i guess i have to make changes to the php.ini file.

i can create the paths to the img tag, is there a way to find out if the link is broken so when its not it will not display it?

---

<div class="post-metadata">

### Author: ![imagined](https://avatars.discourse-cdn.com/v4/letter/i/a87d85/32.png) [@imagined](https://forum.kirupa.com/u/imagined)
#### Post date: [June 12, 2008, 4:05pm UTC](https://forum.kirupa.com/t/php-check-if-file-in-another-web-server-exists/263097/4 "2008-06-12T16:05:08Z")

</div>

I just read that setting allow\_url\_open is a huge security risk.

I just want to check if the image in the photobucket link exists. any other way?

---

<div class="post-metadata">

### Author: ![simplistik](https://yyz1.discourse-cdn.com/flex011/user_avatar/forum.kirupa.com/simplistik/32/7747_2.png) [@simplistik](https://forum.kirupa.com/u/simplistik)
#### Post date: [June 12, 2008, 5:29pm UTC](https://forum.kirupa.com/t/php-check-if-file-in-another-web-server-exists/263097/5 "2008-06-12T17:29:04Z")

</div>

i had to do this once, you can do it with curl. i set this up w/ a demo image for you

```php

<?php
$ch = curl_init();
curl_setopt($ch, CURLOPT_URL, "http://l.yimg.com/g/images/home_photo_jam343.jpg");
curl_setopt($ch, CURLOPT_HEADER, true);
curl_setopt($ch, CURLOPT_NOBODY, true);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
curl_setopt($ch, CURLOPT_FOLLOWLOCATION, true);
curl_setopt($ch, CURLOPT_MAXREDIRS, 10);

$data = curl_exec($ch);
curl_close($ch);

preg_match_all("/HTTP\/1\.[1|0]\s(\d{3})/",$data,$matches);

$code = end($matches[1]);

if(!$data) 
{
	echo "file could not be found";
} 
else 
{
	if($code == 200) 
	{
		echo "file found";
	} 
	elseif($code == 404) 
	{
		echo "file not found";
	}
}
?>

```

this isn’t my code i found it a long time ago but don’t remember where, so I can’t take any credit for it

[ot]  
fyi fopen on most servers will be turned off cause of the security risk  
[/ot]

---

<div class="post-metadata">

### Author: ![imagined](https://avatars.discourse-cdn.com/v4/letter/i/a87d85/32.png) [@imagined](https://forum.kirupa.com/u/imagined)
#### Post date: [June 12, 2008, 6:39pm UTC](https://forum.kirupa.com/t/php-check-if-file-in-another-web-server-exists/263097/6 "2008-06-12T18:39:06Z")

</div>

Thanks! I just copy and pasted the code and did some minor changes. It works great.

```php
						$table.= '<img src="'.$imgthLink.'1024x768.png" alt="'.$row['name'].' Wallpaper"><strong>'.$row['name'].' Wallpaper</strong><br />';
						for($i=0; $i<=10; $i++)
						{
							if($i==0)
							{
								$dimensions ='800x600';
							}
							if($i==1)
							{
								$dimensions ='1024x768';
							}
							if($i==2)
							{
								$dimensions = '1152x864';
							}
							if($i==3)
							{
								$dimensions = '1280x800';
							}
							if($i==4)
							{
								$dimensions = '1280x960';
							}
							if($i==5)
							{
								$dimensions = '1280x1024';
							}
							if($i==6)
							{
								$dimensions = '1400x1050';
							}
							if($i==7)
							{
								$dimensions = '1440x900';
							}
							if($i==8)
							{
								$dimensions = '1600x1200';
							}
							if($i==9)
							{
								$dimensions = '1920x1440';
							}
							if($i==10)
							{
								$dimensions = '1920x1200';
							}
							$ch = curl_init();
							curl_setopt($ch, CURLOPT_URL, $imgLink.$dimensions.'.png');
							curl_setopt($ch, CURLOPT_HEADER, true);
							curl_setopt($ch, CURLOPT_NOBODY, true);
							curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
							curl_setopt($ch, CURLOPT_FOLLOWLOCATION, true);
							curl_setopt($ch, CURLOPT_MAXREDIRS, 10);
							
							$data = curl_exec($ch);
							curl_close($ch);
							preg_match_all("/HTTP\/1\.[1|0]\s(\d{3})/",$data,$matches);
							$code = end($matches[1]);
							
							if(!$data) 
							{
// echo "file could not be found";
							} 
							else 
							{
								if($code == 200) 
								{
// echo "file found";
									$table .= '<a href="'.$imgLink.$dimensions.'.png">'.$dimensions.'</a><br />';
								} 
								elseif($code == 404) 
								{
// echo "file not found";
								}
							}
// $table .= '<a href="'.$imgLink.$dimensions.'.png">'.$dimensions.'</a><br />';
// echo '<img src="'.$imgthLink.$dimensions.'.png">'.$dimensions.'<br />';
						}

```

The only issue it’s that it looks like it does it slowly. Check the other pages and they come out really quick, but if you try the free wallpaper it takes a lot longer.

[http://www.iampixels.com/index.php?pagetype=wallpapers](http://www.iampixels.com/index.php?pagetype=wallpapers)

---

<div class="post-metadata">

### Author: ![imagined](https://avatars.discourse-cdn.com/v4/letter/i/a87d85/32.png) [@imagined](https://forum.kirupa.com/u/imagined)
#### Post date: [June 12, 2008, 6:51pm UTC](https://forum.kirupa.com/t/php-check-if-file-in-another-web-server-exists/263097/7 "2008-06-12T18:51:08Z")

</div>

by the way…

some of the girly images were done by a female friend that is learning photoshop. he he he.

we plan to stuff the website with graphics.

---

<div class="post-metadata">

### Author: ![jwilliam](https://avatars.discourse-cdn.com/v4/letter/j/cc9497/32.png) [@jwilliam](https://forum.kirupa.com/u/jwilliam)
#### Post date: [June 12, 2008, 9:14pm UTC](https://forum.kirupa.com/t/php-check-if-file-in-another-web-server-exists/263097/8 "2008-06-12T21:14:20Z")

</div>

If fopen wrappers are enabled on your server then try using file().

```auto

if(file('http://l.yimg.com/g/images/home_photo_jam343.jpg')) {
  echo "File Exists";
}

```

---

<div class="post-metadata">

### Author: ![simplistik](https://yyz1.discourse-cdn.com/flex011/user_avatar/forum.kirupa.com/simplistik/32/7747_2.png) [@simplistik](https://forum.kirupa.com/u/simplistik)
#### Post date: [June 12, 2008, 9:17pm UTC](https://forum.kirupa.com/t/php-check-if-file-in-another-web-server-exists/263097/9 "2008-06-12T21:17:06Z")

</div>

one thing you can do, to sorta counteract the slowness of curl’s fetch is using a caching method, smarty is a good framework to use. but that way you only request the images once outta x minutes, the rest of the time it’s cached.

---

<div class="post-metadata">

### Author: ![simplistik](https://yyz1.discourse-cdn.com/flex011/user_avatar/forum.kirupa.com/simplistik/32/7747_2.png) [@simplistik](https://forum.kirupa.com/u/simplistik)
#### Post date: [June 12, 2008, 9:17pm UTC](https://forum.kirupa.com/t/php-check-if-file-in-another-web-server-exists/263097/10 "2008-06-12T21:17:54Z")

</div>

[QUOTE=jwilliam;2341752]If fopen wrappers are enabled on your server then try using file().

```auto

if(file('http://l.yimg.com/g/images/home_photo_jam343.jpg')) {
  echo "File Exists";
}

```

[/QUOTE]  
fopen has to be open on the recipients server

---

<div class="post-metadata">

### Author: ![jwilliam](https://avatars.discourse-cdn.com/v4/letter/j/cc9497/32.png) [@jwilliam](https://forum.kirupa.com/u/jwilliam)
#### Post date: [June 12, 2008, 9:38pm UTC](https://forum.kirupa.com/t/php-check-if-file-in-another-web-server-exists/263097/11 "2008-06-12T21:38:33Z")

</div>

Oh yeah… good call…
