1

I am trying to follow the code on the php docs listed here http://php.net/manual/en/function.scandir.php

However when I run the code nothing is displayed. Could someone please show me where I am making mistakes?

my code

<?php
$directory = "/xyz/images/";
$images = glob($directory . "*.jpg");
foreach($images as $image)
{
  echo $image;
}
error_reporting(E_ALL);
?>

Nothing is being displayed. I am using xampp as on windows. I have also referenced these questions Listing all images in a directory using PHP

Get filenames of images in a directory

I have also tried "c:/xampp/htdocs/xyz/images/", " /xyz/images/" , "xyz/images/", "/images/"as my directory.

<?php
error_reporting(E_ALL);
$directory = scandir("linewalk');
$images = glob($directory . "*.jpg");
foreach($images as $image)
{
  echo $image;
}

?>

its now throwing errors Parse error: syntax error, unexpected '.'


<?php
error_reporting(E_ALL);
foreach (scandir(".") as $file)
echo $file . "\n";
?>

****** thanks for all your help and suggestions********* works and lists content of current dir I am currently in root/xyz/test so to scan /xyz/images it should be
(scandir("qa/linewalk") Warning: scandir(qa/linewalk,qa/linewalk): The system cannot find the path specified - is what I get when I try

Community
  • 1
  • 1
liquidacid
  • 13
  • 6
  • try '/htdocs/xyz/images/' – Craig Sep 08 '16 at 14:52
  • Try checking the directory exists first http://php.net/manual/en/function.is-dir.php `if (!is_dir($directory)){ exit('Directory does not exists'); }`. Also try `glob($dir . "*.jpg", GLOB_ERR)` as this will cause glob to stop on errors such as unreadable directories etc. – R. Chappell Sep 08 '16 at 15:03
  • I might be wrong here, but wouldn't the errors not get reported since the error reporting is turned on after the errors happen? Try putting the error reporting line on before the actual code starts and see if that throws you errors – Brian Leishman Sep 08 '16 at 15:12

3 Answers3

2

You should convert your directory to a file system path:

$directory = dirname(__FILE__) . "/xyz/images/";

Also, you can use RecursiveDirectoryIterator

$dirIterator = new RecursiveDirectoryIterator($directory);
$iterator = new RecursiveIteratorIterator($dirIterator, RecursiveIteratorIterator::SELF_FIRST);

foreach ($iterator as $file) {
    print_r($file);
}
Sorin P
  • 36
  • 4
1

Line

$directory = "/xyz/images/";

means that you take files from xyz folder in the root of your server (computer). I suppose there's no such folder and you don't have permissions to access it.

So, you should set your path more carefully, for example:

// path comes from yor server docroot
$directory = $SERVER['DOCUMENT_ROOT'] . "/xyz/images/";

// path comes from current folder in which your script is run
$directory = "./xyz/images/";
u_mulder
  • 54,101
  • 5
  • 48
  • 64
0

The path is relative to the position of the php file.

Hence if the php file and the xyz directory are located in the same parent directory, then simply use

$directory=scandir(xyz/images/);

AtulBhatS
  • 181
  • 2
  • 12