简体   繁体   中英

Wayward Thumbnail generation in dynamic PHP script

This script snippet checks to see if a file exists in a directory and the builds a thumbnail. For some reason before the _1.jpg thumbnail an empty thumbnails is created with just a .jpg? Where is this happening, I can track it down?

Here is a tinyurl of the issue to see...best to test in IE as firefox automatically removes it: http://tinyurl.com/c6o2yts

<?
                                    $image = "<br>";
                                    $ListingRid = $row['ListingRid'];                                   
                                    $img_cnt = 1;
                                    $image .= "<a href=/feeds/fmfl/rets_images/$ListingRid_1.jpg rel=enlargeimage::mouseover rev=loadarea><img src=/feeds/fmfl/rets_images/$ListingRid_1.jpg alt='' width='100' height='75' border='0' /></a>&nbsp;";
                                    for ($c=1;$c<10;$c++) {
                                        $c_ext = $c;
                                        if (file_exists("/var/www/vhosts/domain.com/httpdocs/feeds/fmfl/rets_images/{$ListingRid}_{$c_ext}.jpg"))
                                            $image .= "<a href=/feeds/fmfl/rets_images/{$ListingRid}_{$c_ext}.jpg rel=enlargeimage::mouseover rev=loadarea><img src=/feeds/fmfl/rets_images/{$ListingRid}_{$c_ext}.jpg alt='' width='100' height='75' border='0' /></a>&nbsp;";
                                        else
                                            $c=12;

                                        $img_cnt++;
                                        if ($img_cnt == 3) {
                                            $image .= "<br>";
                                            $img_cnt = 0;
                                        }

                                    }

                                    ?>  

The problem is here:

$image .= "<a href=/feeds/fmfl/rets_images/$ListingRid_1.jpg rel=enlargeimage::mouseover rev=loadarea><img src=/feeds/fmfl/rets_images/$ListingRid_1.jpg alt='' width='100' height='75' border='0' /></a>&nbsp;";

And more specifically here:

$ListingRid_1.jpg

PHP allows underscores and integers in variable names. So while you want it to find $ListingRid . "_1" $ListingRid . "_1" , PHP interprets the variable's name as $ListingRid_1 , which does not exist.

You could use brackets, so that it becomes

$image .= "<a href=/feeds/fmfl/rets_images/{$ListingRid}_1.jpg rel=enlargeimage::mouseover rev=loadarea><img src=/feeds/fmfl/rets_images/{$ListingRid}_1.jpg alt='' width='100' height='75' border='0' /></a>&nbsp;";



Also: As an aside, a better way to exit a for loop early is to use break .

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM