Jump to content

[SOLVED] Problem with file_exists...


lilbadger25

Recommended Posts

I have a page that lists an image (pulled from a folder on the server, the name is in the database, associated with a specific event) as well as the copy from the event (from the database). The admin has the ability to delete photos but I want to have some sort of insurance in place in case they delete a photo that is already associated with an existing event.

 

I have to get this done quickly, so I figured that I'd have something that checked to see if the image exists in the folder on the server, and if it doesn't, it needs to display a default image.

 

The page worked fine before I put in this script, and now it shows four instances of the default image, and no copy or other events listings (even though all the photos currently exist on the server).

 

Where am I going wrong?

 

	$path = "images/";
?>
<tr>
    <td align="left">
<?php if (file_exists($path . $row[eventImage])) { ?>
    <img src="images/<?php echo $eventImage ?>" align="left" style="margin-right:10px"> 
<?php } 
else {  ?>
     <img src="images/default.jpg"> <? } ?> <a href="<? echo "<b>$eventTitle</b><br /> $eventInfo<br><br>"; ?>
      <div align="right"><span class="style1"><a href="eventsEdit.php?id=<? echo $id; ?>">[ edit ]</a>       <a href="eventsDelete.php?id=<? echo $id; ?>">[ delete ]</a></div></td>
  </tr>

Link to comment
Share on other sites

I changed the path as suggested.

 

eventImage is a field name in my db...I put quotations around it.

 

It is still showing four instances of the default image, the edit and delete links, but no copy.

 

I moved the closing bracket, thinking maybe it was that...but it wasn't.

 

Any ideas?

Link to comment
Share on other sites

valtido, thanks for the response...I can't believe I missed that (ok, so I can...this tiny little thing is driving me crazy!).

Unfortunately, it didn't fix the problem.

I've clearly forgotten something, or put something in the wrong order.

I'm posting all of the script (less the connect include, obviously I have that!) so maybe you guys can spot the problem and point me in the right direction.

 

<?php
mysql_select_db($database_site, $site);
$query = "SELECT * FROM events";
$result = mysql_query($query);
$num = mysql_num_rows($result);
$i=0;
while ($i < $num) {
$id = mysql_result($result,$i,"eventID");
$eventTitle = mysql_result($result,$i,"eventTitle");
$eventInfo = mysql_result($result,$i,"eventInfo");
$eventImage = mysql_result($result,$i,"eventImage");
$path = $_SERVER['DOCUMENT_ROOT'] . "images/";
?>
  <tr>
    <td align="left">
    
<?php if (file_exists($path . $row[$eventImage])) { ?>
    <img src="images/<?php echo $eventImage ?>" align="left" style="margin-right:10px"> 
<?php } 
else {  ?>
     <img src="images/default.jpg"> <? } ?> <a href="<? echo "<b>$eventTitle</b><br /> $eventInfo<br><br>"; ?>
      <div align="right"><span class="style1"><a href="eventsEdit.php?id=<? echo $id; ?>">[ edit ]</a>       <a href="eventsDelete.php?id=<? echo $id; ?>">[ delete ]</a></div></td>
  </tr>
  <?php 
$i++;
}
?>

Link to comment
Share on other sites

Make this changes loool

 

duno this script is bad but i tried to keep it the way you want it

	mysql_select_db($database_site, $site);
$query = "SELECT * FROM events";
$result = mysql_query($query);


while ($row = mysql_num_rows($result)) {
$id = mysql_result($result,$i,"eventID");
$eventTitle = mysql_result($result,$i,"eventTitle");
$eventInfo = mysql_result($result,$i,"eventInfo");
$eventImage = mysql_result($result,$i,"eventImage");
$path = $_SERVER['DOCUMENT_ROOT'] . "images/";

   echo'<tr>
    <td align="left">
    ';
 if (file_exists($path . $row[$eventImage])) { 
	echo '<img src="images/'.$eventImage.'" align="left" style="margin-right:10px"> ';
 } 
else {  
	echo '<img src="images/default.jpg"> ';
}
echo '  <a href="$eventTitle" >whatever goes here</a></b><br /> $eventInfo<br><br>"'; 
echo'      <div align="right"><span class="style1"><a href="eventsEdit.php?id='.$id.'">
  [ edit ]</a>       <a href="eventsDelete.php?id='.$id.'">[ delete ]</a></div></td>
  </tr>

Link to comment
Share on other sites

I made the changes you'd suggested, valtido...it now takes a very long time to load, still shows the default image instead of the image in the database, and I get this error:

Fatal error: Maximum execution time of 30 seconds exceeded in /home/sites/site19/eventsList.php on line 24

 

line 24:

	$id = mysql_result($result,$i,"eventID");

 

what would cause this to happen?

 

I am still learning and if there is a better way to write this script, I am more than willing to change it so I can learn more efficient ways in php.

 

 

Link to comment
Share on other sites

while ($row = mysql_num_rows($result)) {

$id = mysql_result($result,$i,"eventID");

$eventTitle = mysql_result($result,$i,"eventTitle");

$eventInfo = mysql_result($result,$i,"eventInfo");

$eventImage = mysql_result($result,$i,"eventImage");

$path = $_SERVER['DOCUMENT_ROOT'] . "images/";

should be

while ($row = mysql_fetch_array($result)) {
$id = $row["eventID"];
$eventTitle = $row["eventTitle"];
$eventInfo = $row["eventInfo"];
$eventImage = $row"eventImage"];
$path = $_SERVER['DOCUMENT_ROOT'] . "images/";

Link to comment
Share on other sites

try this:

 

<?php
  mysql_select_db($database_site, $site);
  $query = "SELECT * FROM events";
  $result = mysql_query($query);
  $num = mysql_num_rows($result);
  for($i = 0;$i < $num;$i++){
    $id = mysql_result($result,$i,"eventID");
    $eventTitle = mysql_result($result,$i,"eventTitle");
    $eventInfo = mysql_result($result,$i,"eventInfo");
    $eventImage = mysql_result($result,$i,"eventImage");
    if(!file_exists(dirname(__FILE__).'/images/'.$eventImage))
      $eventImage = "default.jpg";
?>
  <tr>
    <td align="left">
      <img src="images/<?php echo $eventImage ?>" align="left" style="margin-right:10px"> 
      <? echo "<b>$eventTitle</b><br /> $eventInfo<br><br>"; ?>
      <div align="right"><span class="style1"><a href="eventsEdit.php?id=<? echo $id; ?>">[ edit ]</a>       <a href="eventsDelete.php?id=<? echo $id; ?>">[ delete ]</a></div>
    </td>
  </tr>
<?php } ?>

Link to comment
Share on other sites

  for($i = 0;$i < $num;$i++){

that just saved me a few lines of code. I'd been doing it differently, but will be using this method from now on.

 

if(!file_exists(dirname(__FILE__).'/images/'.$eventImage))
      $eventImage = "../default.jpg";

 

Obviously, this is the "magic". If anyone has a moment to break this down for me so I can understand *why* it works the way it does, I would appreciate it.

 

I did adjust the last line of that to put the default.jpg outside of the images folder so the admin can't delete the default image that is in place in case he/she deletes a photo in use.

 

 

Link to comment
Share on other sites

if(!file_exists(dirname(__FILE__).'/images/'.$eventImage))
      $eventImage = "../default.jpg";

What it does:

First, let's discuss the pieces of "dirname(__FILE__).'/images/'.$eventImage". In the end, it will give us the full system path to the file.

  • __FILE__ gives us the full system path to the current PHP file where this code is. So something like '/var/www/test.php'
  • dirname() returns just the directory of a path (no trailing slash), so dirname(__FILE__) would give us '/var/www'
  • now, we just need to add on the image dir, and the name of the image from the database, and we have the full path: '/var/www/images/myfile.jpg'

 

Next, we use file_exists to test if this file exists on our hard drive. If it doesn't, replace the value of $eventImage with the path to the default image. It's better to do it here so there isn't an if/else while printing the HTML code.

 

Got it?

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.