Take the 2-minute tour ×
WordPress Development Stack Exchange is a question and answer site for WordPress developers and administrators. It's 100% free, no registration required.

I'm using the latest timthumb script to generate my post thumbnails and include them along with the excerpt. Though I only want the thumbnails to load if their files actually exist, otherwise I don't even want the image placeholder to load.

I'm going to be using this on a multi-user network, which is still in the early stages of development. And I want to test if the posts author has neglected to properly add the "thumb" custom field, which if they have, would normally return a 404 not found error.

So I've put the following code in my loop which first determines if the post has any images then using the file path for the first image listed in the post checks if the file exists.

<?php
$content = $post->post_content;
$searchimages = '~<img [^>]* />~';
preg_match_all($searchimages, $content, $pics);
$iNumberOfPics = count($pics[0]);
if($iNumberOfPics > 0){
    $blogTemplate = get_bloginfo('template_directory');
    $imgSrc = get_post_meta($post->ID, "thumb", $single = true);
    $imgSrc = $blogTemplate . "/scripts/timthumb.php?src=" . $imgSrc . "&w=300&zc=1";

    if(file_exists($imgSrc)){ ?>
        <a href="<?php the_permalink(); ?>" class="thumb" rel="bookmark" title="Permanent Link to <?php the_title(); ?>"><img src="<?php echo $imgSrc; ?>" alt="<?php the_title(); ?>" /></a><?php              
    }
}
?>

I've tested the $imgSrc string that's outputted which is valid, it is an existing file though the my file_exists($imgSrc) function always returns false. I'm left wondering if the timthumb script is compatible with the file_exists() function?

share|improve this question
    
file_exists() is a PHP function. TimThumb is an independent, third-party script that is unrelated to WordPress. –  Chip Bennett Feb 6 '12 at 1:58
add comment

closed as off topic by Rarst Feb 6 '13 at 14:34

Questions on WordPress Development Stack Exchange are expected to relate to WordPress within the scope defined by the community. Consider editing the question or leaving comments for improvement if you believe the question can be reworded to fit within the scope. Read more about reopening questions here.If this question can be reworded to fit the rules in the help center, please edit the question.

2 Answers

The file_exists() function is a built in php function and should work everywhere. But with the way you're doing it, is wrong. You cannot use a query string within a php file to check if the file exists. Just because its a valid link, doesn't mean the file_exists(). There is no file on your server by the name of /scripts/timthumb.php?src=*

You need to do the following to your code:

<?php
$content = $post->post_content;
$searchimages = '~<img [^>]* />~';
preg_match_all($searchimages, $content, $pics);
$iNumberOfPics = count($pics[0]);
if($iNumberOfPics > 0){
    $blogTemplate = get_bloginfo('template_directory');
    $imgSrc = get_post_meta($post->ID, "thumb", $single = true);

    if(file_exists($imgSrc)){ 
    $imgSrc = $blogTemplate . "/scripts/timthumb.php?src=" . $imgSrc . "&w=300&zc=1";
    ?>
        <a href="<?php the_permalink(); ?>" class="thumb" rel="bookmark" title="Permanent Link to <?php the_title(); ?>"><img src="<?php echo $imgSrc; ?>" alt="<?php the_title(); ?>" /></a><?php              
    }
}
?>

This way you're actually checking for the real file name and then changing the url to the query string url later, after you know the file exists.

share|improve this answer
    
I've already tried removing the 2nd definition of $imgSrc altogether (the one with "scripts/timthumb.php?src=") but it still returns false. I've also echoed the $imgSrc string after I did that which the right file path.I've already tried removing the 2nd definition of $imgSrc altogether (the one with "scripts/timthumb.php?src=") but it still returns false. I've also echoed the $imgSrc string after I did that which returned the right file path. –  Ben Nov 8 '11 at 19:08
    
My mistake, file_exists should be using the local path not the url to the image. I wouldn't even check if the file_exists and I would just echo it out if the user has to have a post_meta set in order for the image source to even be available. It should be on the user to be sure the file is valid. You can either do that, or you can use the following pastebin.com/VpHK0kLb to check the http response you get from that URL. If you get a 400 code back, you're good. –  Kevin Langley Jr. Nov 8 '11 at 19:15
1  
Ok, bare with me while I implement your second suggestion as that'll come in very handy for something else I'm working on as well as this topic and if it works you'll get a very honourable mention :-) –  Ben Nov 8 '11 at 19:25
    
Sorry @Kevin it didn't work - "Fatal error: Call to undefined function pcntl_fork() ...... on line 24". I would really like that function to work though as it'd be amazingly useful for me. –  Ben Nov 8 '11 at 20:43
    
For that function to work you have to install the module for it. Do you have root access to your server? –  Kevin Langley Jr. Nov 8 '11 at 21:06
show 4 more comments
up vote 0 down vote accepted

file_exists(); and is_readable(); (which I've decided to go for instead as it detects if the file is readable as well as if it exists) both require the $_SERVER['DOCUMENT_ROOT']; variable to be called as the file is physically stored on the file server's HDD and therefore replaces the need to declare the domain name in the function. Do it like this:

is_readable($_SERVER['DOCUMENT_ROOT'].'/'.preg_replace('/(https?:\/\/)?(www\.)?+(example\.com)\/?/','',$imgSrc);

no need to write any special apache code.

share|improve this answer
add comment

Not the answer you're looking for? Browse other questions tagged or ask your own question.