3

I'm wanting to display images. The user can add up to 7 images, but they can also add less than 7 if needed.

I searched around and saw that file_exists() is the function I'm after, but I've tried a couple of ways and it always gives me no image.

Here's one way I tried:

<?php if(file_exists(base_url() . "photos/" . $p['p_id'] . "_5.jpg")):?>
    <div class="single">
       <a href="<?php echo base_url();?>photos/<?php echo $p['p_id'];?>_5.jpg" rel="lightbox[image]">
         <img src="<?php echo base_url();?>photos/<?php echo $p['p_id'];?>_5.jpg" style="width:100px; height:100px;"/>
       </a>
    </div>
<?php endif; ?>

Then I searched around and found out that its not a good idea to define the path inside the function, so I defined it outside:

<?php $path = base_url() . "photos/" . $p['p_id'] . "_";?>
     <?php $img4 = $path . "4.jpg";?>
      <?php if(file_exists($img4)):?>

which didn't work either.I'm testing with 4 images being in the folder 'photos' so I know there should be 4 images being displayed. Any ideas on how I should fix it would be great. Thanks

a7omiton
  • 1,597
  • 4
  • 34
  • 61

2 Answers2

4

The function file_exists accepts file path as its parameter and not the url like what you're using now which is CodeIgniter's base_url().

You should use the FCPATH constant which points to your codeigniter app's base directory. This is what you should assign to your path (depending on where your image files are located in your application directory):

<?php $path = FCPATH . "htdocs/images/" . $product[0]['product_id'] . "_";?> 

But for some reason if really you need to use the base_url(). You can use get_headers() which checks the headers returned by the url in an associative array and false on failure. From the headers returned you can determine whether the image url is valid or not. More about get_headers().

Hope it helps.

Cheers,

Ardy

Ardy Dedase
  • 1,088
  • 9
  • 15
  • 2
    `BASEPATH` is the incorrect constant to use -- it points to the system folder path. For static assets, `FCPATH` is more appropriate, which is the path for the dir that `index.php` is in (FC stands for front controller). – Aken Roberts Apr 02 '13 at 17:37
  • @Cryode: Thanks for spotting that! – Ardy Dedase Apr 02 '13 at 17:40
1

File exists requires a directory path not a URL, a URL will always return false.

    <?php 
    for($i=1; $i < 7; $i++):
    {
    $img = $path . $i . ".jpg";
    $url=getimagesize($img);
    if(!is_array($url))
    {
    ?>
    <div class="single">
            <a href="<?php echo base_url();?>images/<?php echo $product[0]['product_id'];?>_<?php echo $i; ?>.jpg" rel="lightbox[productimage]">
                  <img src="<?php echo base_url();?>images/<?php echo $product[0]['product_id'];?>_<?php echo $i; ?>.jpg" style="width:100px; height:100px;"/>
                </a>
            </div>
    <?php
    }
    }
    ?>

Be forewarned this is a touch on the slow side, but it does work with URL's on the server.

Rick Calder
  • 18,310
  • 3
  • 24
  • 41
  • Hi Rick Calder, I don't understand why you're using getimagesize()? – a7omiton Apr 02 '13 at 21:48
  • Because as I said file_exists won't work on a URL, it requires an actual directory path, ie C:/my documents. getimagesize WILL work on url's, then if the response to that is an array you know the image does exist – Rick Calder Apr 02 '13 at 21:52
  • Ah I see, will try that out now – a7omiton Apr 02 '13 at 22:01
  • I tried it and it works, but if you don't mind I'm going to use Ardy's fix up, as that method is something I came up with and only needed a touch up. Thank you for your help, much appreciated as I learned a new way to it – a7omiton Apr 02 '13 at 22:20