Loading... ## Handsome主题随机图片无法显示 发现博客的文章随机头图和边栏随机图无法显示的问题。图片是挂掉的,挺丑的。 ![随机图片挂了.png][1] ## 问题排查 不能忍,要找原因。第一直觉就是路径权限的问题。 ### 路径权限? 翻了翻代码,发现随机图片存放在handsome主题文件夹下的`/usr/img/sj`和`/usr/img/sj2`文件夹中,相应的读取代码在主题文件夹下的`/libs/Utils.php`的`getSj1ImageNum`和`getSj2ImageNum`这两个函数中。下面这个是`getSj2ImageNum`函数的代码。 ```php $basedir = dirname(dirname(__FILE__))."/usr/img/sj2"; $arr = scandir($basedir); $image = count(preg_grep("/\.jpg$/", $arr)); return $image; ``` 这里`$image`最后返回的是0,导致主题加载了`0.jpg`,而`/usr/img/sj2`文件夹下是从`1.jpg`开始编号的。`0.jpg`文件不存在,使得Handsome主题加在图片挂掉了。check了一下代码,`$basedir`的值是正确的,`$arr`的值是`null`。那么问题就出在`scandir`这个函数这里了。 我第一感觉是权限不够,可是调整了`/usr/img/sj2`目录的权限之后`scandir`这个函数返回的依然是`null`。 ### scandir函数被禁用! 没办法,我把`$arr = scandir($basedir);`这一行单独抠出来,`$basedir`换成完整路径,单独放到`test.php`文件中,在命令行执行`php test.php`,没想到直接抛出了一个warning: ``` PHP Warning: scandir() has been disabled for security reasons in test.php on line 2 ``` 原来是`scandir`函数被php禁用了。这就好办了,编辑`php.ini`将`scandir`函数从`disable_functions`中删除,执行`/etc/init.d/php-fpm reload`命令生效就好啦 <img src="https://cdn.jsdelivr.net/gh/ihewro/handsome-static@8.2.0.2/assets/img/emotion/aru/cheer.png" class="emotion-aru"> ## 总结 LNMP的默认配置中禁用了部分存在危险的PHP函数,包括`passthru, exec, system, chroot, scandir, chgrp, chown, shell_exec, proc_open, proc_get_status, ini_alter, ini_alter, ini_restore, dl, pfsockopen ,openlog, syslog, readlink, symlink, popepassthru, stream_socket_server, fsocket, fsockopen`。 Handsome主题中使用了`scandir`这个函数,PHP中禁用的`scandir`函数导致随机图404。 所以解决方法就是: 1. 编辑`php.ini` 2. 搜索`disable_functions`,将`scandir`函数从`disable_functions`中删除并保存 3. 执行`/etc/init.d/php-fpm reload`命令,让配置生效 PS: LNMP的php配置文件在`/usr/local/php/etc/php.ini`这里。 ## Ref - https://gist.github.com/zhyunfe/6af8cd44619a20d786d4b590489d1f02 [1]: https://tva1.sinaimg.com/large/007oxmFJly1fwskkurbg6j306n0d3mxi.jpg Last modification:January 9, 2020 © Allow specification reprint Like If you think my article is useful to you, please feel free to appreciate