Adding document link under thumbnail; file_exists function

I would like to be able to add a document link under thumbnails if there is a matching name with a different extension. For instance, if there is both a "myphoto.jpg" and a "myphoto.pdf" I would like a hyperlink to appear under the thumbnail of "myphoto.jpg" that links to the pdf version of the file. I don't really know php but I made a guess as to what the code might look like. Can someone help me out with this? Thank you! (The main problem is figuring out how to ask if the particular file exists. I can't get even a hard-coded url to work.)

<!-- Add in description NB-->
<?php echo getBareImageTitle(); ?>

    

<?php if (file_exists(htmlspecialchars(substr(getImageLinkURL,0,-3).pdf))) {
echo '<a href="' . htmlspecialchars(substr(getImageLinkURL,0,-3)) . '.pdf"';
echo " ";
}
?>

Comments

  • Looks like there is a basename(path,suffix) function that would maybe work better than the substring function...
  • Another try that is probably just as bad...

    `
    <?php if (file_exists(htmlspecialchars(basename(getImageLinkURL(),'.jpg')) . ".pdf")) {
    echo '<a href="' . htmlspecialchars(substr(getImageLinkURL(),0,-3)) . 'pdf">View pdf';
    }
    ?>
    `
  • You do not want to be using WEB links since the file will actually be on the server. What you need is `$image->localpath()` to get the filesystem path to the image file.
  • ah, I see. I will try that. Thanks much!
  • I tried
    `<?php echo $image->localpath(); ?>`
    in album.php just to see what it returned and got an error.
    http://www.test-gallery.mysitecreations.com/bookmarks/English/
    I don't think I know enough about zenphoto or coding to do this.... if I'm missing something obvious I'd be glad to know.

    Thanks.
  • `$image` is a standin for whatever image you are interested in. In your case I suspect it should be `$_zp_current_image`, presuming you are useing it within the `next_image()` loop.

    Not sure what `$image` was for you, probably undefined.
  • Well, I'm back on this as I think this would best suit my needs, if you don't mind...
    `<? php echo $_zp_current_image->localpath(); ?>`
    does not work but
    `<? php echo $_zp_current_album->getFolder(); ?>`
    does.

    I get the error
    Fatal error: Call to undefined method _Image::localpath() in /home6/mysitecr/public_html/test-gallery/themes/effervescence-nb/album.php on line 292

    I think I'm within the image loop because the folder name is repeated for each thumb, and it is located immediately after the printImageThumb command. I don't know php (beyond a few tutorials in w3schools, although I'm javaScript certified) so maybe this is something simple I'm overlooking?

    Thanks for your patience.

    I'm working with the test site at
    http://test-gallery.gospelriver.com/bookmarks/English/
  • `localpath` is a property, not a method, so it should actually be `$_zp_current_image->localpath` instead. http://www.zenphoto.org/documentation/classes/_Image.html#var$localpath
  • Success!!! Thank you!
  • Ok, I'm making good progress now. Is there a cheat sheet on all the menthods and classes I would need to reference the actual file location?

    This is what I have so far. The if statement works, but I have the file location hard-coded because I don't know the best way to get that.

    Also, I don't think getBareImageTitle() is what I want-- I want the file name, not the title I give it in Zenphoto.

    `
    <?php if (file_exists(substr($_zp_current_image->localpath,0,-3)."pdf")) {
    echo "PDF";
    `
    The final issue is getting the bookmarks to go beneath the PDF link... that must be a CSS thing?

    http://test-gallery.gospelriver.com/bookmarks/English

    Thanks again.
  • Edit: I got the bookmarks to flow by adding in some line breaks... probably not the "right" thing to do but seems to work...

    Edit2: Page 4 doesn't work with line breaks either... any ideas?
    http://test-gallery.gospelriver.com/bookmarks/English/page/4/

    Maybe the best thing to do would be to just create a link on the image page...?
  • acrylian Administrator, Developer
    You get the filename by using `$_zp_current_image->filename`. Your other problem with the line breaks, I don't understand actually.
  • Thanks acrylian for the tip-- this almost works for me.
    The problem with using $_zp_current_image->filename is that it gives me
    http://test-gallery.gospelriver.com/bookmarks/English/apple-delight.pdf
    instead of
    http://test-gallery.gospelriver.com/albums/bookmarks/English/apple-delight.pdf
    and thus gives me a broken link. I need the location of the actual file, since I am referencing the matching pdf of the jpg, which is in the albums folder. Zenphoto skips over the albums folder for some reason when making its links. If I had to, I could do a replace like "gospelriver.com/" with "gospelriver.com/albums/" but then I would have to hardcode this for all my sites.

    Regarding the line breaks- I took them out. Check my site and see if you see a problem with the formatting. I need a way to make the bookmarks wrap under the other bookmarks on the next line, not wrap only to the pdf link.

    http://test-gallery.gospelriver.com/bookmarks/English/
  • acrylian Administrator, Developer
    `$_zp_current_image->filename` itself gives you only the filename, no path. For the full image there is `getFullImageLinkURL()`. See the documentation.

    No, Zenphoto does not skip the "albums" folder. The albums folder is not used in normal page links therefore it is not included.

    You can create a link to a file manually using:
    `getAlbumFolder().$_zp_current_album->name."/".$_zp_current_image->filename;`
    (you might need to add some slashes, quickly typed)
  • @acrylian:
    getFullImageLinkURL() returned an undefined function error. I have tried getFullImageURL() before but it does not work for me because I cannot change the extension to .pdf and have the link still work. It does not recognize the pdf file I think because it is not indexed by zenphoto.

    The problem with your second suggestion is it gives me the following:
    http://test-gallery.gospelriver.com/home6/mysitecr/public_html/test-gallery/albums/bookmarks/English/apple-delight.pdf

    the /home6/mysitecr/public_html/test-gallery is the link on the server and makes this link invalid- I don't know how to get a link without it...

    Thanks for your help.
  • acrylian Administrator, Developer
    Well, I meant getFullImageURL() (always good to refer to the doc as I not always look there myself). Of course the pdf is not indexed if you did not make a plugin similar to the textobject one.

    Ok, I got myself confused with "getAlbumsFolder()" - haven't used this in ages - as that means the folder of hte album, not the main albums folder. Use this:
    `FULLWEBPATH.'/'.ALBUMFOLDER.'/'.$_zp_current_album->name.'/'.$_zp_current_image->filename;`
  • ah, perfect. Works great; Thanks much for this.

    Finally found the documentation that addresses this... there's so much information to wade through, it's hard to find the function that works for you.
    http://www.zenphoto.org/documentation/elementindex.html

    I didn't find $_zp_current_album listed there though. Maybe that's because it is just a variable?

    Did you have any input on how I can get the bookmarks to go all the way over to the left on the second line without stopping at the first "PDF" link?
  • acrylian Administrator, Developer
    Exactly, as it is a global variable and listed on its own article:
    http://www.zenphoto.org/2008/07/zenphotos-global-variables/

    To use it you have to look at the object model tutorial:
    http://www.zenphoto.org/2010/02/zenphotos-object-model-framework/

    Regarding your bookmarks, you will have to change the css a little to leave room for them. Look at the source code.
  • Got it. Thanks for the links.

    Problem solved. Much appreciated.
Sign In or Register to comment.