Jump to content

File_exists Problem


Hooch
 Share

Recommended Posts

Hey all.I have copied the following code from php.net then changed it to my appropriateinfo.It always faults to false.

<?php$filename = '/path/to/foo.txt';if (file_exists($filename)) {	echo "The file $filename exists";} else {	echo "The file $filename does not exist";}?>

Is there another way to see if a file exists?Thanks

Link to comment
Share on other sites

Here is my existing code

			$rackcard = '/images/three_horse/' . $_GET['c'] . '_rackcard.pdf';			if(file_exists($rackcard))			{				echo '<a href="' . $rackcard . '" target="_blank">Rack Card</a>';			}

$_Get['c'] can be replaced with 2009The site in question is thecoachhousegallery.com ( thecoachhousegallery.com/images/three_horse/2009_rackcard.pdf )Just as a test I replaced $_Get['c'] with 2009 in my code and it still did not work.Just an FYI this website is on PHP 4

Edited by Hooch
Link to comment
Share on other sites

When you start a path with a slash it starts in the root of the filesystem, not the current directory. It's looking for a file in /images/three_horse/, not /home/coachhouse/public_html/images/three_horse/, which I presume is where the file actually is (whatever the account name is). You can either leave off the slash so that it starts in the current directory, or you can prepend the complete path.The lesson is to trust the code. Computers never make mistakes, programmers do. A computer will always do exactly what you tell it to, nothing more and nothing less. People seem to assume that it's the computer that made the mistake and not them, but that's never the case.

Link to comment
Share on other sites

For that to work, your PHP file must be in the folder where the "images" folder is. Is it? Can we have links to both files?

Link to comment
Share on other sites

For that to work, your PHP file must be in the folder where the "images" folder is. Is it? Can we have links to both files?
thecoachhousegallery.com/images/three_horse/2009_rackcard.pdf(The page this code is on is in the root directory)The page in question is... thecoachhousegallery.com/three_horse.php?c=2009If you select any other year the "Rack Card" image should not show.So try..thecoachhousegallery.com/three_horse.php?c=2010This should not show that image since there is no file for 2010 yet. Edited by Hooch
Link to comment
Share on other sites

So, in reality, within three_horse.php (and not in a file that three_horse.php includes!), you have

			$rackcard = 'images/three_horse/' . $_GET['c'] . '_rack_card.pdf';			if(file_exists($rackcard))			{				echo '<a href="' . $rackcard . '" target="_blank" onmouseover="MM_swapImage('Rack card','','images/three_horse/rackcard_over.jpg',1)" onmouseout="MM_swapImgRestore()"><img src="images/three_horse/rackcard_reg.jpg" alt="' . $_GET['c'] .' Rack Card" name="' . $_GET['c'] . ' Rack Card" width="61" height="38" border="0" id="' . $_GET['c'] . ' Rack Card" /></a>			}

Correct? If not, either show your true code, or create a separate, reduced example, where the problem still occurs, and show us that.

Link to comment
Share on other sites

Yes, it's in three_horse.phpnot included to it.Here is the full code

<?PHPsession_start(); include 'db.php';$tbl_1	 = 'tch_three_horse'; $tbl_2	 = 'tch_three_horse_details'; $post_year = $_GET['c'];$timestamp = time(); $cur_year  = date("Y",$timestamp);// Record a hit for each page viewunset($_SESSION['page']);$_SESSION['page'] = "three_horse_" . $_GET['c'] . ".php";if (!isset($_SESSION['time-three_horse_' . $_GET['c'] . '.php']) || time() - $_SESSION['time-three_horse_' . $_GET['c'] . '.php'] > 300){	  $_SESSION['time-three_horse_' . $_GET['c'] . '.php'] = time();	include 'admin_zen_page_stats.php';}?><!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd"><html xmlns="http://www.w3.org/1999/xhtml"><head><meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1" /><title><?PHP echo $site_title;?></title><link href="css/style.css" rel="stylesheet" type="text/css" media="screen" /><link rel="stylesheet" href="styleLytebox.css" type="text/css" media="screen" /><script type="text/javascript" language="javascript" src="js/lytebox/lytebox.js"></script></head><body onload="MM_preloadImages('images/three_horse/rackcard_over.jpg')"><table width="1000" border="0" align="center"  cellpadding="0" cellspacing="0" bgcolor="#6C0000" class="border">  <tr align="center">	<td height="109" align="left" valign="top">	<table width="998" border="0" cellspacing="0" cellpadding="0">	  <tr>		<td><img name="logo" src="images/logo.jpg" width="248" height="109" alt="" /></td>		<td><img name="image" src="images/headers/horse.jpg" width="750" height="109" alt="" /></td>	  </tr>	</table>	</td>  </tr>    <tr align="center">	<td width="998" align="left" background="images/main_bg.gif"><?PHP include 'nav_links.php'; ?></td>  </tr>  <tr>	<td align="left" valign="top">	<?PHP		//////////////////////////////////////////////////////////////		//					   Current Tour					   //		//														  //		//////////////////////////////////////////////////////////////		if(isset($_GET['c']))		{			//Show Details Block			echo '<table align="center" cellpadding="0" cellspacing="0" width="886" class="news-border">';			echo '<tr>';			echo '<td colspan="3" background="images/three_horse/logo_large.jpg" height="200" align="right" valign="bottom">';			echo '<a href="images/three_horse/2009_SlideShow/2009_01.jpg" title="Image #1" rel="lytebox[house]"><img src="images/three_horse/2009_SlideShow/button.jpg" class="" style="background-color: #E7E7E7;border:none;"/></a>';			for ($i = 2; $i <= 21; $i++)			{				if($i <= 9)//images are numbered 01 to 21, so we need to add the "0" on images 1 through 9				{					echo '<a href="images/three_horse/2009_SlideShow/2009_0' . $i . '.jpg" title="Image #' . $i . '" rel="lytebox[house]" style="border:none;"><img src="images/three_horse/2009_SlideShow/spacer.gif" class="" style="background-color: #242021;border:none;"/></a>';				}					else				{					echo '<a href="images/three_horse/2009_SlideShow/2009_' . $i . '.jpg" title="Image #' . $i . '" rel="lytebox[house]" style="border:none;"><img src="images/three_horse/2009_SlideShow/spacer.gif" class="" style="background-color: #242021;border:none;"/></a>';							}			}			$rackcard = 'images/three_horse/' . $_GET['c'] . '_rackcard.pdf';			if(file_exists($rackcard))			{				echo '<a href="' . $rackcard . '" target="_blank"><img src="images/three_horse/rackcard_reg.jpg" alt="' . $_GET['c'] . ' Rack Card" name="' . $_GET['c'] . ' Rack Card" width="61" height="38" border="0" id="' . $_GET['c'] . ' Rack Card" /></a>';			}			echo '</td>';			echo '</tr>';			echo '<tr>';			echo '<td width="8"> </td>';			echo '<td width="864" align="justify">';			//If it's the current year that was selected show the text for the details			if($post_year == $cur_year)			{				$details = mysql_query("SELECT `info` FROM `" . $tbl_2 . "`") or die(mysql_error());				$r	   = mysql_fetch_array($details);				include 'clean_show.php';				$info = clean_show($r['info']);				echo '<div class="light-14">' . stripslashes($info) . '<br /><br /></div>';			}				else 			{				echo '<center><img src="images/three_horse/' . $post_year . '_left2.jpg" class="pic-border-larger" width="420" height="280">  <img src="images/three_horse/' . $post_year . '_right2.jpg" class="pic-border-larger" width="420" height="280"></center>';				}						echo '</td>';			echo '<td width="8"> </td>';			echo '</tr>';			echo '</table>';   			echo '<br />';			//Show all the Artists			$th_sql = mysql_query("SELECT * FROM `" . $tbl_1 . "` WHERE `tour_year` = '$post_year' AND `active` = 1 ORDER BY `set_order` ASC") or die(mysql_error());			while($th = mysql_fetch_array($th_sql))			{				echo '<table align="center" cellpadding="5" cellspacing="0" width="886" class="news-border">';				echo '<tr>';				echo '<td colspan="3"><strong class="light-14">' . $th['firstname'] . ' ' . $th['lastname'] . '</strong></td>';				echo '</tr>';				echo '<tr>';				echo '<td width="33%" align="center" valign="middle">';				echo '<img name="Left" src="images/three_horse/' . $th['tour_year'] . '_' . $th['firstname'] . '_' . $th['lastname'] . '_left.jpg" width="300" height="200" alt="" class="pic-border-larger"/>';				echo '</td>';				echo '<td width="34%" align="justify" valign="top">';				echo stripslashes(nl2br($th['info']));				echo '</td>';				echo '<td width="33%" align="center" valign="middle">';				echo '<img name="Right" src="images/three_horse/' . $th['tour_year'] . '_' . $th['firstname'] . '_' . $th['lastname'] . '_right.jpg" width="300" height="200" alt="" class="pic-border-larger"/>';				echo '</td>';				echo '</tr>';				echo '<tr>';				echo '</table>';				echo '<br />';			}									}	   	?>	</td>  </tr>  <tr height="15" class="light-9">	<td align="right" valign="middle"><?PHP include'copyright_main.php';?></td>  </tr></table></body></html>

Link to comment
Share on other sites

is the folder 'images' in the same directory as three_horses.php? (i think you said this php page was in the root, right?)

Link to comment
Share on other sites

Replace

if(file_exists($rackcard))

with

if(file_exists(dirname(__FILE__) . DIRECTORY_SEPARATOR . $rackcard))

In Linux, "root" is the folder

/

But what you're reffering to as "root" is actually your "document root" - the root of your website, which is different than the OS root, e.g.

/home/thecoachhousegallery.com/public_html/

so please forgive the confusion.

Link to comment
Share on other sites

My mistake, it looks like file_exists always requires an absolute path, not a relative path. You can get the path to the directory of the current script with dirname(__FILE__).
Thanks man!Okay so I have changed some code here
$rackcard = dirname(__FILE__) . '/images/three_horse/' . $_GET['c'] . '_rackcard.pdf';

This shows the correct path with a slash in front.And the code still comes up false when it should be true.

			$rackcard = dirname(__FILE__) . '/images/three_horse/' . $_GET['c'] . '_rackcard.pdf';			if(file_exists($rackcard))			{				echo '<a href="' . $rackcard . '" target="_blank">Rack Card</a>';			}

Link to comment
Share on other sites

			$rackcard = 'images/three_horse/' . $_GET['c'] . '_rackcard.pdf';			if(file_exists(dirname(__FILE__) . DIRECTORY_SEPARATOR . $rackcard))			{				echo '<a href="' . $rackcard . '" target="_blank">Rack Card</a>';			}

Still not working (I tried a forward slash in from of images too)

Link to comment
Share on other sites

You're not outputting the correct path within the link. You instead need

$rackcard = 'images/three_horse/' . $_GET['c'] . '_rackcard.pdf';if(file_exists(dirname(__FILE__) . DIRECTORY_SEPARATOR . $rackcard))

I also notice constant inconsistencies between the presence of "_" between "rack" and "card". Is there one or not? From what I noticed, it should be WITH one, i.e.

$rackcard = 'images/three_horse/' . $_GET['c'] . '_rack_card.pdf';if(file_exists(dirname(__FILE__) . DIRECTORY_SEPARATOR . $rackcard))

Whatever the case, verify it!

Link to comment
Share on other sites

You're not outputting the correct path within the link. You instead need
$rackcard = 'images/three_horse/' . $_GET['c'] . '_rackcard.pdf';if(file_exists(dirname(__FILE__) . DIRECTORY_SEPARATOR . $rackcard))

I also notice constant inconsistencies between the presence of "_" between "rack" and "card". Is there one or not? From what I noticed, it should be WITH one, i.e.

$rackcard = 'images/three_horse/' . $_GET['c'] . '_rack_card.pdf';if(file_exists(dirname(__FILE__) . DIRECTORY_SEPARATOR . $rackcard))

Whatever the case, verify it!

That did the trick!!Thank you so much!!
Link to comment
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now
 Share

×
×
  • Create New...