Re: Problem with ocasional blank value

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

 



On Fri, January 18, 2008 9:12 am, Joey wrote:
> 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

While the USER does not need to see any errors, YOU need to catch
them, log them, and deal with them, rather than just ignoring them.

Try using:
http://php.net/set_error_handler instead.

There are MANY other ways to handle errors, but that is probably the
easiest lowest-impact one you can implement quickly without modifying
your codebase too much.

> $dir_handle = @opendir($dir);
>
>
>
> while (false !== ($file = readdir($dir_handle)))
>
>   if (!is_dir($file))
>
>     $image_array[] = $file;
>
> #
>
> closedir ($dir_handle);
>
>
>
> #
>
> # The following appears to make things more random
>
> srand((double)microtime()*1000000);

You may have fooled yourself into thinking it looks more random, but
the rand function is auto-seeded at PHP startup for a long time now,
and the srand() is not needed.

> $r = rand(0,sizeof($image_array)-1);
>
>
>
> #
>
> echo "<img src=\"{$image_folder}{$image_array[$r]}\" width = \"561\"
> height
> = \"400\" >";

Have you looked at the source in the browser when it fails?

What's in the src="..." bit?

Is it something you expect or what?

-- 
Some people have a "gift" link here.
Know what I want?
I want you to buy a CD from some indie artist.
http://cdbaby.com/from/lynch
Yeah, I get a buck. So?

-- 
PHP General Mailing List (http://www.php.net/)
To unsubscribe, visit: http://www.php.net/unsub.php


[Index of Archives]     [PHP Home]     [Apache Users]     [PHP on Windows]     [Kernel Newbies]     [PHP Install]     [PHP Classes]     [Pear]     [Postgresql]     [Postgresql PHP]     [PHP on Windows]     [PHP Database Programming]     [PHP SOAP]

  Powered by Linux