php获取文件夹里的图片不显示的解决办法:1、通过readfile或者get_file_contents读取图片;2、修改HTML代码如“var pic="{:url('/home/showPics')}"+'?pic='+item;”。

本文操作环境:windows7系统、PHP7.1版、DELL G3电脑
php获取文件夹里的图片不显示怎么办?
PHP 读取文件夹(比如某共享文件夹)中的图片并显示:
1.获取文件夹下图片
立即学习“PHP免费学习笔记(深入)”;
public function albumList(){
$share_url = input('path');
$files = getImgList($share_url);
//json格式输出图片路径列表
}
//获取图片文件列表函数
function getImgList($dir){
$files = array();
if(is_dir($dir)){
$file_dir = scandir($dir);
foreach($file_dir as $file){
if($file == '.' || $file == '..'){
continue;
}elseif(is_dir($dir.$file.'/')){
$files = array_merge($files, getImgList($dir.$file.'/'));
}else{
if(isImage($dir.$file) !== false){
array_push($files, $dir.$file);
}
}
}
}
return $files;
}2.前端直接用显示图片路径时图片不能正常显示,但是直接打开src中路径图片正常显示

问题解决:
读取文件中的图片,需要readfile或者get_file_contents读取,解决如下:
php部分:
//正确共享图片
public function showPics(){
$pic = input('pic',''); //图片所在文件路径
header('Content-Type: image/jpeg');
readfile($pic);
}html部分:
var pic = "{:url('/home/showPics')}"+'?pic='+item; //item为图片路径 eg:\192.168.199.17600522153602501004911.png
<img src="+pic+">图片正常显示:

推荐学习:《PHP视频教程》
以上就是php获取文件夹里的图片不显示怎么办的详细内容,更多请关注php中文网其它相关文章!
PHP怎么学习?PHP怎么入门?PHP在哪学?PHP怎么学才快?不用担心,这里为大家提供了PHP速学教程(入门到精通),有需要的小伙伴保存下载就能学习啦!
Copyright 2014-2025 https://www.php.cn/ All Rights Reserved | php.cn | 湘ICP备2023035733号