Or Smith
Or Smith

Reputation: 3616

PHP/ read from folder

I have the next code:

<?php 
    $path = 'imgsFor';
    $files_array = scandir($path);
    for ($x=0; $x<=4; $x++)
    {
    echo '<img src="imgsFor/$files_array[$x]"   <br>';
  }
?>

In order to display all images in the folder imgsFor.

For some reason, I see the just boxes and not the actual images.

What can be the reason?

Upvotes: 0

Views: 102

Answers (4)

TiMESPLiNTER
TiMESPLiNTER

Reputation: 5899

You messed up some things. Your correct script would be

<?php 

$path = 'imgsFor/';
$files_array = scandir($path);
foreach($files_array as $f) {
    if(is_dir($path . $f) === false)
        continue;

    echo '<img src="' , $path , $f , '"><br>';
}

/* EOF */

Upvotes: 0

Siraj Khan
Siraj Khan

Reputation: 2348

Please check you directory path and use is_dir which returns false when the file doesn't exist. you can try like this

$path = 'imgsFor';

$scan = scandir($path);

foreach($scan as $file)
{
    if (!is_dir($path))
    {
        echo $file.'\n';
    }
}

Upvotes: 0

hsz
hsz

Reputation: 152304

The best way for me is to use glob function:

foreach (glob($path) as $filename) {
    echo '<img src="' . $path . '/' . $filename . '"/><br/>';
}

Upvotes: 4

R&#225;pli Andr&#225;s
R&#225;pli Andr&#225;s

Reputation: 3923

The reason is that your URL is invalid. Your variable wont echo out if you use single quotes. You also forgot to end the tag. Try this:

 echo "<img src='http://yourwebsite.com/imgsFor/{$files_array[$x]}'/><br/>";

Upvotes: 0

Related Questions