lilbadger25 Posted January 29, 2008 Share Posted January 29, 2008 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> Quote Link to comment https://forums.phpfreaks.com/topic/88376-solved-problem-with-file_exists/ Share on other sites More sharing options...
scottybwoy Posted January 29, 2008 Share Posted January 29, 2008 is this file, being run from parent path of "images/"? Try using a more flexible path like $path = $_SERVER['DOCUMENT_ROOT'] . "images/"; Also you do not have quotes around eventImage is it a variable or a key? Good luck Quote Link to comment https://forums.phpfreaks.com/topic/88376-solved-problem-with-file_exists/#findComment-452309 Share on other sites More sharing options...
lilbadger25 Posted January 29, 2008 Author Share Posted January 29, 2008 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? Quote Link to comment https://forums.phpfreaks.com/topic/88376-solved-problem-with-file_exists/#findComment-452483 Share on other sites More sharing options...
valtido Posted January 29, 2008 Share Posted January 29, 2008 sience you have echo'd the $eventImage i presume its a variable lool change $row['eventImage'])) to $row[$eventImage])) hope it helps PS Quote Link to comment https://forums.phpfreaks.com/topic/88376-solved-problem-with-file_exists/#findComment-452500 Share on other sites More sharing options...
lilbadger25 Posted January 29, 2008 Author Share Posted January 29, 2008 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++; } ?> Quote Link to comment https://forums.phpfreaks.com/topic/88376-solved-problem-with-file_exists/#findComment-452530 Share on other sites More sharing options...
trq Posted January 29, 2008 Share Posted January 29, 2008 Where is $row defined? Quote Link to comment https://forums.phpfreaks.com/topic/88376-solved-problem-with-file_exists/#findComment-452536 Share on other sites More sharing options...
valtido Posted January 29, 2008 Share Posted January 29, 2008 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> Quote Link to comment https://forums.phpfreaks.com/topic/88376-solved-problem-with-file_exists/#findComment-452547 Share on other sites More sharing options...
lilbadger25 Posted January 29, 2008 Author Share Posted January 29, 2008 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. Quote Link to comment https://forums.phpfreaks.com/topic/88376-solved-problem-with-file_exists/#findComment-452591 Share on other sites More sharing options...
rhodesa Posted January 29, 2008 Share Posted January 29, 2008 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/"; Quote Link to comment https://forums.phpfreaks.com/topic/88376-solved-problem-with-file_exists/#findComment-452598 Share on other sites More sharing options...
lilbadger25 Posted January 29, 2008 Author Share Posted January 29, 2008 That answers my question on another thread about mysql_result not being an efficient way of doing things...thanks Aaron. I made the changes and it still shows only default images. Quote Link to comment https://forums.phpfreaks.com/topic/88376-solved-problem-with-file_exists/#findComment-452617 Share on other sites More sharing options...
rhodesa Posted January 29, 2008 Share Posted January 29, 2008 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 } ?> Quote Link to comment https://forums.phpfreaks.com/topic/88376-solved-problem-with-file_exists/#findComment-452634 Share on other sites More sharing options...
lilbadger25 Posted January 29, 2008 Author Share Posted January 29, 2008 Thanks so much, Aaron! That worked beautifully. Quote Link to comment https://forums.phpfreaks.com/topic/88376-solved-problem-with-file_exists/#findComment-452649 Share on other sites More sharing options...
lilbadger25 Posted January 29, 2008 Author Share Posted January 29, 2008 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. Quote Link to comment https://forums.phpfreaks.com/topic/88376-solved-problem-with-file_exists/#findComment-452694 Share on other sites More sharing options...
rhodesa Posted January 29, 2008 Share Posted January 29, 2008 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? Quote Link to comment https://forums.phpfreaks.com/topic/88376-solved-problem-with-file_exists/#findComment-452714 Share on other sites More sharing options...
lilbadger25 Posted January 29, 2008 Author Share Posted January 29, 2008 Absolutely. I cannot be happy in the "well it works so I'll just copy and paste every time I need to do it" mindframe. I need to understand what I am doing. Thanks for explaining it to me. Quote Link to comment https://forums.phpfreaks.com/topic/88376-solved-problem-with-file_exists/#findComment-452727 Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.