如何使用PHP显示文件夹中的图像 [英] How can one display images from a folder using PHP

查看:89
本文介绍了如何使用PHP显示文件夹中的图像的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

嘿所有



我的任务的一部分是显示图像。事情是图像在文件夹中,我必须从文件夹内拉出图像才能显示它,并且必须使用php完成。我必须能够从图像文件夹中提取图像才能显示它。



有人可以指出我正确的方向,或者至少让我知道如何做到这一点。我已经尝试了这组代码,但我不知道它是否会起作用,而另一件事我会收到错误



我尝试了什么:



hey all

one part of my assignment is displaying images. The things is that the images are in a folders that I have to pull the images from inside the folder for it to be display and it has to be done using php. I have to able to pull the images from the image folder in order for it to be display.

can someone point me in the right direction or at least give me an idea how to do this. I hve tried this set of codes but i have no idea if it would work and another thing i was getting errors

What I have tried:

<pre><?php

$dir_path = "folder/";
$extensions_array = array('jpg','png','jpeg');

if(is_dir($dir_path))
{
    $files = scandir($dir_path);
    
    for($i = 0; $i < count($files); $i++)
    {
        if($files[$i] !='.' && $files[$i] !='..')
        {
            // get file name
            echo "File Name -> $files[$i]<br>";
            
            // get file extension
            $file = pathinfo($files[$i]);
            $extension = $file['extension'];
            echo "File Extension-> $extension<br>";
            
           // check file extension
            if(in_array($extension, $extensions_array))
            {
            // show image
            echo "<img src='$dir_path$files[$i]' style='width:100px;height:100px;'><br>";
            }
        }
    }
}

推荐答案

dir_path = folder /;
dir_path = "folder/";


extensions_array = array(' jpg'' png'' jpeg');

if(is_dir(
extensions_array = array('jpg','png','jpeg'); if(is_dir(


dir_path))
{
dir_path)) {


这篇关于如何使用PHP显示文件夹中的图像的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆