Home  >  Article  >  Backend Development  >  What should I do if the pictures in the folder obtained by php are not displayed?

What should I do if the pictures in the folder obtained by php are not displayed?

藏色散人
藏色散人Original
2021-11-03 11:07:132574browse

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

What should I do if the pictures in the folder obtained by php are not displayed?

本文操作环境:windows7系统、PHP7.1版、DELL G3电脑

php获取文件夹里的图片不显示怎么办?

PHP  读取文件夹(比如某共享文件夹)中的图片并显示:

1.获取文件夹下图片

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.176\20200522153602501004911\1.png
<img src="+pic+">

 图片正常显示:

推荐学习:《PHP视频教程

The above is the detailed content of What should I do if the pictures in the folder obtained by php are not displayed?. For more information, please follow other related articles on the PHP Chinese website!

Statement:
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn