Re: Problem with ocasional blank value
| From: | Jochem Maas | Date: | Fri, 18 Jan 2008 15:54:05 +0000 |
| Subject: | Re: Problem with ocasional blank value | ||
| References: | 1 | Groups: | php.general |
| Request: | Send a blank email to php-general+get-267555@lists.php.net to get a copy of this message | ||
Joey schreef:
Hi All, I have a problem that the below code which is supposed to display a random image and on occasion it shows NO image. I'm not sure what is happening. This is running on linux just in case that makes any difference. <% $image_folder = "new_random/"; $dir = $_SERVER['DOCUMENT_ROOT'] . "/" . $image_folder; $image_array = array(); # The user doesn"t need to see any errors $dir_handle = @opendir($dir); while (false !== ($file = readdir($dir_handle))) if (!is_dir($file))apart from what Eric said - which was all correct (and definitely use glob for this - and possibly even cache the results in a file, which you periodically refresh, so that you only have to read a single file rather than list a complete directory) - have you actually looked at the src attribute of the IMG tag when you get no image. no doubt you'll see very clearly why it is not showing an image (my guess it will be referencing a file that is not an image as Eric suggested)$image_array[] = $file;# closedir ($dir_handle); # # The following appears to make things more random srand((double)microtime()*1000000); $r = rand(0,sizeof($image_array)-1); # echo "<img src=\"{$image_folder}{$image_array[$r]}\" width = \"561\" height = \"400\" >"; %></td>
Thanks!