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.

    评论

报告相同问题?

悬赏问题

  • ¥50 易语言把MYSQL数据库中的数据添加至组合框
  • ¥20 求数据集和代码#有偿答复
  • ¥15 关于下拉菜单选项关联的问题
  • ¥20 java-OJ-健康体检
  • ¥15 rs485的上拉下拉,不会对a-b<-200mv有影响吗,就是接受时,对判断逻辑0有影响吗
  • ¥15 使用phpstudy在云服务器上搭建个人网站
  • ¥15 应该如何判断含间隙的曲柄摇杆机构,轴与轴承是否发生了碰撞?
  • ¥15 vue3+express部署到nginx
  • ¥20 搭建pt1000三线制高精度测温电路
  • ¥15 使用Jdk8自带的算法,和Jdk11自带的加密结果会一样吗,不一样的话有什么解决方案,Jdk不能升级的情况