Welcome to Dream.In.Code
Getting PHP Help is Easy!

Join 132,694 PHP Programmers for FREE! Get instant access to thousands of PHP experts, tutorials, code snippets, and more! There are 1,311 people online right now. Registration is fast and FREE... Join Now!




If file exist - not working

 
Reply to this topicStart new topic

If file exist - not working

morcomm
post 27 May, 2008 - 01:28 AM
Post #1


New D.I.C Head

*
Joined: 27 Mar, 2008
Posts: 49


My Contributions


Hi,

I am trying to create a page that displays images that exist in a folder. I am using the following code

CODE
  <?php
$filename = '/gallery/thumbs/001.jpg';

if (file_exists($filename)) {
    echo "<img src="$filename" />";
} else {
    echo "";
}
?>


I plan to add a whole string of these to a page so that the image will show up when I ftp it to a folder, like this.

CODE
  
<?php
$filename = '/gallery/thumbs/001.jpg';

if (file_exists($filename)) {
    echo "<img src="$filename" />";
} else {
    echo "";
}
?>
<?php
$filename = '/gallery/thumbs/002.jpg';

if (file_exists($filename)) {
    echo "<img src="$filename" />";
} else {
    echo "";
}
?>
<?php
$filename = '/gallery/thumbs/003.jpg';

if (file_exists($filename)) {
    echo "<img src="$filename" />";
} else {
    echo "";
}
?>


So if the image exists it will display it, and if it does not it will display nothing. I got the code from here: http://www.php.net/file_exists.

The problem that I am haveing is that when I upload the file to the server and test it, it just generates a blank page. The source code of the page is this:

CODE
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.0 Transitional//EN">
<HTML><HEAD>
<META http-equiv=Content-Type content="text/html; charset=utf-8"></HEAD>
<BODY></BODY></HTML>


It has ignored all content that is PHP????

When I take out the file_exists code, it renders fine. What am I doing wrong
User is offlineProfile CardPM

Go to the top of the page

joeyadms
post 27 May, 2008 - 05:32 AM
Post #2


D.I.C Head

Group Icon
Joined: 4 May, 2008
Posts: 145



Thanked 6 times

Dream Kudos: 600

Expert In: PHP, Web Security

My Contributions


You are not escaping quotes in your echo statements, see this line here

CODE

echo "<img src="$filename" />";


This should be
CODE

echo "<img src=\"$filename\" />";


Or even better
CODE

echo "<img src='$filename' />";


On another note, however, you do not need else statements when you do not need to do anything, so your code should be like this
CODE

<?php
$filename = '/gallery/thumbs/001.jpg';

if (file_exists($filename)) {
    echo "<img src='$filename' />";
}

$filename = '/gallery/thumbs/002.jpg';

if (file_exists($filename)) {
    echo "<img src='$filename' />";
}

$filename = '/gallery/thumbs/003.jpg';

if (file_exists($filename)) {
    echo "<img src='$filename' />";
}
?>


Or you could go wild
CODE

<?php

$fileList = array('/gallery/thumbs/001.jpg',
                  '/gallery/thumbs/002.jpg',
                  '/gallery/thumbs/003.jpg');

foreach ($fileList as $filename){
    if (file_exists($filename)) {
        echo "<img src='$filename' />";
    }
}
?>



Hope that helps

This post has been edited by joeyadms: 27 May, 2008 - 05:33 AM
User is offlineProfile CardPM

Go to the top of the page

morcomm
post 28 May, 2008 - 06:02 AM
Post #3


New D.I.C Head

*
Joined: 27 Mar, 2008
Posts: 49


My Contributions


Ok that worked great. I did have to, however, remove the / at the beginning of the url for it to work. I have also tweeked this some more and this is what I have got.

gallery_index.php
CODE
<DIV id="gallerymain">
<?php
$filename = 'gallery/image_bridges/I001.jpg';
if (file_exists($filename)) {
    include ("gallery_include_.php");
}
?>
<?php
$filename = 'gallery/image_bridges/I002.jpg';

if (file_exists($filename)) {
    include ("gallery_include_.php");
}
?>
<?php
$filename = 'gallery/image_bridges/I003.jpg';

if (file_exists($filename)) {
    include ("gallery_include_.php");
}
?>
</DIV>


gallery_include_.php
CODE
<div id="gallery"><?php echo "<a href='#'><img src='$filename' width='100' height='75' class='ImageArticleGallery' />
</a>" ?> <p class="caption"><?php include ("$desc"); ?>
</p>
</div>


Now the next challenge. I want each of these to have its own url to open up a larger view e.g. www.yoursite.com/gallery/large_view.php?image=I001

I understand that I need to have a page to display the large image (large_view.php) and on that page there must be this bit of code at the top:
CODE
<?php $image=$_GET['image']; ?>


What I can't seem to get my head around is how, when all the images are displayed as thumbnails, I could get them to have the correct link to pass the variable through the url.

Hope I make sense, can anyone help.

This post has been edited by morcomm: 28 May, 2008 - 11:14 AM
User is offlineProfile CardPM

Go to the top of the page

JBrace1990
post 28 May, 2008 - 11:44 AM
Post #4


D.I.C Regular

Group Icon
Joined: 9 Mar, 2008
Posts: 474



Thanked 21 times

Dream Kudos: 350
My Contributions


I assume you're putting the thumbnail data into a database?
php

<?php
$image = $_GET['image'];
// you'll want to make sure the variable is escaped, otherwise someone could do some harm to your site.
$image = mysql_real_escape_string($image);
$sql = mysql_query("SELECT * FROM db WHERE image = '$image'")or die(mysql_error());
while($row=mysql_fetch_array($sql))
{
echo "<a href=large_view.php?image=".$row['id']."><img src=IMAGE_LOCATION>";
}

the above will give you the thumbnails if you keep a small picture of the image somewhere else..... there's a function I have around here somewhere that makes the image a set size, but i don't know exactly where right now...
User is offlineProfile CardPM

Go to the top of the page

Fast ReplyReply to this topicStart new topic
Time is now: 11/23/08 07:46AM

Live PHP Help!

PHP Tutorials

Reference Sheets

PHP Snippets

Bye Bye Ads

Free DIC T-Shirt

T-Shirt Example

Related Sites

Monthly Drawing

Thumb Drive

Partners

Top Contributors

Top 10 Kudos This Month