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

Link to comment
Share on other sites

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

 

Link to comment
Share on other sites

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

Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

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