duanjiaren8188 2014-12-02 22:34
浏览 89

使用php显示目录中的随机图像

I found this questions which helps me with what I am doing, but the problem that I'm running into is - I keep getting an Undefined index error for this $images = glob($imagesDir. '*.{jpg,jpeg,png,gif}', GLOB_BRACE);

Here is my full code (it is a part of an if/elseif):

elseif($result['avatar_type'] == 'Random'){ 
    $images = array(); //Initialize once at top of script
    $imagesDir = 'avatar/random/';
    if(count($images)==0){
      $images = glob($imagesDir. '*.{jpg,jpeg,png,gif}', GLOB_BRACE);
      shuffle($images);
      }
    $avatar = array_pop($images);
}

What I am trying to do is if the database has the avatar_type set to Random then display a random image from the Random directory, but like I said above I keep getting an Undefined index error.

Does anyone see anything wrong with what I am doing and why I would be receiving this error?

  • 写回答

1条回答 默认 最新

  • donglong7338 2014-12-02 23:38
    关注

    A few suggestions:

    This isn't neccessary as glob will return an array:

    $images = array(); //Initialize once at top of script
    

    see http://php.net/manual/en/function.glob.php

    This will cause a warning (but not an error) if glob previously returned false:

    $avatar = array_pop($images);
    

    http://php.net/manual/en/function.array-pop.php

    If you make sure to check return types in the manual you will know what to check for in your code.

    if (empty($var)) is great because it checks for false, null, or undefined without throwing an error.

    Also, as array_pop returns the last element and glob is likely to return the elements in the same order it will not be as random as array_rand would be.

    $avatarKey = array_rand($images, 1); //return 1 random result's key
    $avatar = $images[$avatarKey]; //set the random image value (accessed w/ the rand key)
    

    Your error message shouldn't be caused by the glob line, it's actually probably from this:

    elseif($result['avatar_type'] == 'Random'){ 
    

    If avatar_type isn't set on the result array or the result array is empty you will get an undefined index.

    To prevent that error from happening you would check the array exists before trying to access the avatar_type key:

    function example:

    function getRandomAvatar($result)
    {
        if (empty($result) || empty($result['avatar_type'])) {
            return;  //quit execution if the data is bad
        }
        //rest of code here -
    }
    

    inline code example:

    if (empty($result) || empty($result['avatar_type'])) {
        //do nothing, render an error, whatever - stops execution of the next statement
    } else {
        //this code will only run if $result and $result['avatar_type 
        //are set and wont cause errors
        if ('$result['avatar_type'] == 'Random') {
            //do code here
    

    Your error should have a line number. Check that line and the line right before it.

    评论

报告相同问题?

悬赏问题

  • ¥15 matlab 用yalmip搭建模型,cplex求解,线性化处理的方法
  • ¥15 qt6.6.3 基于百度云的语音识别 不会改
  • ¥15 关于#目标检测#的问题:大概就是类似后台自动检测某下架商品的库存,在他监测到该商品上架并且可以购买的瞬间点击立即购买下单
  • ¥15 神经网络怎么把隐含层变量融合到损失函数中?
  • ¥15 lingo18勾选global solver求解使用的算法
  • ¥15 全部备份安卓app数据包括密码,可以复制到另一手机上运行
  • ¥20 测距传感器数据手册i2c
  • ¥15 RPA正常跑,cmd输入cookies跑不出来
  • ¥15 求帮我调试一下freefem代码
  • ¥15 matlab代码解决,怎么运行