繁体   English   中英

php scandir不引发任何错误,但未列出dir内容

[英]php scandir not throwing any errors but is not listing dir contents

我正在尝试遵循此处http://php.net/manual/en/function.scandir.php列出的php文档上的代码

但是,当我运行代码时,不会显示任何内容。 有人可以告诉我我在哪里犯错吗?

我的密码

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

什么都没有显示。 我在Windows上使用xampp。 我还引用了这些问题,使用PHP列出了目录中的所有图像

获取目录中图像的文件名

我也尝试将“ c:/ xampp / htdocs / xyz / images /”,“ / xyz / images /”,“ xyz / images /”,“ / images /”作为我的目录。

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

?>

现在抛出错误分析错误:语法错误,意外的“。”


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

******感谢您的所有帮助和建议*********可以正常工作并列出当前目录的内容我目前在root / xyz / test中,因此应该扫描/ xyz / images
(scandir(“ qa / linewalk”)警告:scandir(qa / linewalk,qa / linewalk):系统找不到指定的路径-这是我尝试时得到的

线

$directory = "/xyz/images/";

表示您从服务器(计算机)根目录的xyz文件夹中获取文件。 我想没有这样的文件夹,并且您没有访问该文件夹的权限。

因此,您应该更仔细地设置路径,例如:

// 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/";

您应该将目录转换为文件系统路径:

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

另外,您可以使用RecursiveDirectoryIterator

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

foreach ($iterator as $file) {
    print_r($file);
}

该路径相对于php文件的位置。

因此,如果php文件和xyz目录位于同一父目录中,则只需使用

$directory=scandir(xyz/images/);

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM