i want to display my image from a folder but its not work what i change in code to work

 <?php

$dir_path = "/xampp/htdocs/image/";
$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>";
            }
        }
    }
}

Dani AI

Generated

As noted, the usual cause is mixing a server filesystem path with the URL the browser needs. The PHP functions (scandir/is_dir) must point to the filesystem (for example the folder inside XAMPP's htdocs), while the IMG src must be a web-accessible URL path (for example /image/photo.jpg or http://localhost/image/photo.jpg). The simplest, robust pattern is: use the document root for filesystem checks and a separate URL base for output.

$fsDir = rtrim($_SERVER['DOCUMENT_ROOT'], '/\\') . '/image/';   // filesystem path
$urlDir = '/image/';                                           // URL path seen by browser

if (is_dir($fsDir)) {
    foreach (glob($fsDir . '*.{jpg,jpeg,png,gif}', GLOB_BRACE) as $filePath) {
        $name = basename($filePath);
        echo '<img src="' . htmlspecialchars($urlDir . $name, ENT_QUOTES) . '" width="100" height="100" alt="' . htmlspecialchars($name, ENT_QUOTES) . '">';
    }
} else {
    echo 'Directory not found: ' . htmlspecialchars($fsDir, ENT_QUOTES);
}

Notes and quick troubleshooting: verify realpath($fsDir) or var_dump(file_exists($fsDir)) to confirm PHP can see the folder. After the page renders, view source and copy an img URL into the browser — if it 404s, the URL is wrong. Use the browser Network panel to see HTTP status codes. On Windows/XAMPP the filesystem path often starts with C:\xampp\htdocs\... while the URL seen by the browser is /... under http://localhost/. Also watch case sensitivity (convert extensions to strtolower() when matching), file permissions, and any open_basedir restrictions. Enable error_reporting(E_ALL) while debugging.

Checklist: run the script via http://localhost/... (not file://), confirm Apache/XAMPP is running, separate filesystem vs URL paths as above, and test a direct image URL in the browser. These steps will quickly show whether the problem is PHP-level or just an incorrect src URL.

Firstly, I'd check that the is_dir command is workinf correctly. Which folder are you running the script from? I doubt '/xampp/htdocs/image' is relative to where the script is running and it isn't an absolute path.
Add more echos to ascertain which code is running and where it stops. At least that way you'll be able to narrow down where the proble is.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.