简体   繁体   English

如何修复PHP中的“警告:scandir:无法打开目录”错误

[英]How to fix “Warning: scandir: failed to open dir” error in PHP

I scan the folder to display all the pictures from it. 我扫描文件夹以显示其中的所有图片。 How to set the folder absolute path? 如何设置文件夹的绝对路径?

Pictures are in the folder //localhost/img/gallery/. 图片位于文件夹// localhost / img / gallery /中。

There are two index.php files. 有两个index.php文件。 The first - in the root directory. 第一个-在根目录中。 The second is in the folder "en". 第二个在文件夹“ en”中。 In the first, the function works well. 首先,该功能运行良好。 In the second function adds the folder "en" to the url. 在第二个函数中,将文件夹“ en”添加到URL。 And instead of "localhost/img/gallery/" - it turns out "localhost/en/img/gallery/". 而不是“ localhost / img / gallery /”-原来是“ localhost / en / img / gallery /”。 If you add the site address to the directory, the path is correct. 如果将站点地址添加到目录,则路径正确。 But an error appears: "Warning: scandir (//localhost/img/gallery/): failed to open dir: not implemented". 但是出现错误:“警告:scandir(// localhost / img / gallery /):无法打开dir:未实现”。

// in function.php:

function GetImages($path){
   $wimg = "";
   $fimg = "";
   $images = scandir($path);
   if ($images !== false) {
      $images = preg_grep("/\.(?:png|gif|jpe?g)$/i", $images);
      if (is_array($images)) {
         foreach($images as $image) {
            $fimg .= '
                  <a href="'.$path.htmlspecialchars(urlencode($image)).'">
                     <img src="'.$path.htmlspecialchars(urlencode($image)).'">
                  </a>
            ';
         }
         $wimg .= $fimg;
      } else {
         $wimg .= "<p>No images in directory!</p>";
      }
   } else {
      $wimg .= "<p>Error.</p>";
   }
   echo $wimg;
}

// in index.php:

<?php GetImages("img/gallery/");?> // it's Ok

// in en/index.php:

<?php GetImages("img/gallery/");?> // it turns out "localhost/en/img/gallery/". But there are no pictures.

// if in en/index.php:
<?php GetImages("localhost/img/gallery/");?> // it turns out "Warning: scandir(//localhost/img/gallery/): failed to open dir: not implemented in D:\OpenServer\domains\localhost\function.php on line XXX"

So an error too: 所以也是一个错误:

// in function.php
function GetImages(){
   $wimg = "";
   $fimg = "";
   $path = $_SERVER['SERVER_NAME'] . "/img/gallery/";
// in index.php and in en/index.php
<?php GetImages();?> // "Warning: scandir(localhost/img/gallery/,localhost/img/gallery/): ������� �� ������ ����� �������� ���. (code: 3) in D:\OpenServer\domains\localhost\function.php on line XXX"

but

// in function.php
function GetImages(){
   $wimg = "";
   $fimg = "";
   $path = "/img/gallery/";
// in index.php
<?php GetImages();?> // it's Ok
// in en/index.php
<?php GetImages();?> // it turns out "localhost/en/img/gallery/". But there are no pictures.

dirname( FILE ) . dirname( FILE )。 is error too. 也是错误的。

尝试这个,

scandir(__DIR__ .$path); 

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

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