簡體   English   中英

如何修復PHP中的“警告:scandir:無法打開目錄”錯誤

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

我掃描文件夾以顯示其中的所有圖片。 如何設置文件夾的絕對路徑?

圖片位於文件夾// localhost / img / gallery /中。

有兩個index.php文件。 第一個-在根目錄中。 第二個在文件夾“ en”中。 首先,該功能運行良好。 在第二個函數中,將文件夾“ en”添加到URL。 而不是“ localhost / img / gallery /”-原來是“ localhost / en / img / gallery /”。 如果將站點地址添加到目錄,則路徑正確。 但是出現錯誤:“警告: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"

所以也是一個錯誤:

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

// 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 )。 也是錯誤的。

嘗試這個,

scandir(__DIR__ .$path); 

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM