Jump to content

file_exists doesn't want to work...


Switch0r

Recommended Posts

I'm having some trouble with file_exists...

 

I want to find out whether an image file exists for a given url, and display it if it does, or display a different image if it doesn't.

 

What i've got so far is this:

 

function picture($picture)
{
// Create url for file path
$filepath = "http://localhost/images/" . $picture;

if(file_exists($filepath))
{
	?>
	<img src="images/<?php echo $picture; ?>" alt="">
	<?php
}
else
{
	?>
	<img src="images/nopic.jpg" alt="">
	<?php
}
}

 

And it doesn't work, it always returns FALSE and the nopic.jpg is displayed, even tho I know for a fact that the file does exist.

 

Running on Apache 2.0.559 & php 5.1.5 on winxp (if this makes any difference??)

 

I've tried all manner of other functions I can find inf the php manual, but they don't work, and file_exists is the one that fits the purpose...

Link to comment
https://forums.phpfreaks.com/topic/40033-file_exists-doesnt-want-to-work/
Share on other sites

i dont think file_exists() works with url's but server paths (ie C:\server\www\picture.jpg\) (maybe wrong on that)

 

You maybe able too use is_file() with urls, but it has been reported too have problems with large files.

 

 

There was an example on php's manual for url checking,

The following code was taken from php's manual and was NOT written by me,

<?php
   function url_exists($url) {
       $a_url = parse_url($url);
       if (!isset($a_url['port'])) $a_url['port'] = 80;
       $errno = 0;
       $errstr = '';
       $timeout = 30;
       if(isset($a_url['host']) && $a_url['host']!=gethostbyname($a_url['host'])){
           $fid = fsockopen($a_url['host'], $a_url['port'], $errno, $errstr, $timeout);
           if (!$fid) return false;
           $page = isset($a_url['path'])  ?$a_url['path']:'';
           $page .= isset($a_url['query'])?'?'.$a_url['query']:'';
           fputs($fid, 'HEAD '.$page.' HTTP/1.0'."\r\n".'Host: '.$a_url['host']."\r\n\r\n");
           $head = fread($fid, 4096);
           $head = substr($head,0,strpos($head, 'Connection: close'));
           fclose($fid);
           if (preg_match('#^HTTP/.*\s+[200|302]+\s#i', $head)) {
           $pos = strpos($head, 'Content-Type');
           return $pos !== false;
           }
       } else {
           return false;
       }
   } 
?>

 

Hope it helps

 

Stuie

I tested your code and found a was having the same problem  (running latest release of php,apache on winxp)

 

so i re-wrote your code too use file() (a little slow i know)

function picture($pic){
$url = "http://localhost/images/" . $pic; $f = @file($url);
if($f){
$f = "";
	?>
	<img src="images/<?php echo $pic; ?>" alt="">
	<?php
}else{
$f = "";
	?>
	<img src="images/nopic.jpg" alt="">
	<?php
}
}

 

This should now work on your setup :)

 

Stuie

 

Possibly... but the reason i'm asking for your php.ini is would be easier too find a solution if i'm using your exact php setup..

 

i understand if you dont wish too supply this file but it will be difficult for me too try and figure out why this problem is occuring,

 

However you could try and upgrade php too the latest version (5.2.1)

 

Stuie

Archived

This topic is now archived and is closed to further replies.

×
×
  • 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.