Switch0r Posted February 25, 2007 Share Posted February 25, 2007 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 More sharing options...
Stuie_b Posted February 25, 2007 Share Posted February 25, 2007 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 https://forums.phpfreaks.com/topic/40033-file_exists-doesnt-want-to-work/#findComment-193624 Share on other sites More sharing options...
Switch0r Posted February 25, 2007 Author Share Posted February 25, 2007 Cheers Stuie, I'd already seen that and it makes as much sense to me a second time as it did the first I had tried it with the local file path, and that doesn't work either Link to comment https://forums.phpfreaks.com/topic/40033-file_exists-doesnt-want-to-work/#findComment-193627 Share on other sites More sharing options...
Stuie_b Posted February 25, 2007 Share Posted February 25, 2007 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 https://forums.phpfreaks.com/topic/40033-file_exists-doesnt-want-to-work/#findComment-193636 Share on other sites More sharing options...
Switch0r Posted February 25, 2007 Author Share Posted February 25, 2007 well I'm afraid this doesn't work either, still just shows the nopic image Link to comment https://forums.phpfreaks.com/topic/40033-file_exists-doesnt-want-to-work/#findComment-193638 Share on other sites More sharing options...
Stuie_b Posted February 25, 2007 Share Posted February 25, 2007 hmm... strange Can you please send me your phpinfo and php.ini file Stuie Link to comment https://forums.phpfreaks.com/topic/40033-file_exists-doesnt-want-to-work/#findComment-193640 Share on other sites More sharing options...
Switch0r Posted February 25, 2007 Author Share Posted February 25, 2007 Is there something in it that would need turning on/off? Link to comment https://forums.phpfreaks.com/topic/40033-file_exists-doesnt-want-to-work/#findComment-193680 Share on other sites More sharing options...
Stuie_b Posted February 25, 2007 Share Posted February 25, 2007 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 https://forums.phpfreaks.com/topic/40033-file_exists-doesnt-want-to-work/#findComment-193806 Share on other sites More sharing options...
Recommended Posts
Archived
This topic is now archived and is closed to further replies.