简体   繁体   English

PHP scandir在Laravel中失败了

[英]PHP scandir failing in Laravel

So I'm trying to pass a directory to a model function in my Laravel application that uses PHP's scandir , and it can't seem to find the directory. 所以我试图将目录传递给我使用PHP的scandir Laravel应用程序中的模型函数,它似乎找不到该目录。 The string/path I'm supplying to scandir is /staffportal/public/public/documents/ra_docs/ . 我提供给scandir的字符串/路径是/staffportal/public/public/documents/ra_docs/ The directory I'm ultimately attempting to feed to scandir is ra_docs . 我最终尝试提供给scandirra_docs Here is the directory structure ( > means directory): 这是目录结构( >表示目录):

 >staffportal
     >public
         >documents
             >ra_docs
     >app
     >bootstrap
     >vendor

And just for the sake of being thorough, here is the function I'm running. 只是为了彻底,这是我正在运行的功能。

public static function directoryToArray($directory)
{
    $result = array(); 

    $currentDirectory = scandir($directory); 
    foreach ($currentDirectory as $key => $value) 
    { 
        if(!in_array($value, array(".",".."))) 
        { 
            if (is_dir($directory . DIRECTORY_SEPARATOR . $value)) 
            { 
                 $result[$value] = dirToArray($directory . DIRECTORY_SEPARATOR . $value); 
            } 
            else 
            { 
                $result[] = $value; 
            } 
        } 
   } 

   return $result; 
}

Any ideas? 有任何想法吗?

If I change your code a little, changing public static function directoryToArray to function dirToArray , to match the call to dirToArray seen inside the function, the function works as I would expect. 如果我稍微更改你的代码,将public static function directoryToArray更改为function dirToArray ,以匹配函数内部看到的dirToArray调用,该函数可以正常工作。

The problem is either that inconsistency or what you are passing to it. 问题是不一致或你传递给它的是什么。 I'll begin with the latter. 我将从后者开始。

For a start, as mentioned in a comment by The Shift Exchange, you are passing in public/public as part of your string, and the directory structure you show in your question does not have such a path. 首先,正如The Shift Exchange的评论中所提到的,您将作为字符串的一部分传入public/public ,并且您在问题中显示的目录结构没有这样的路径。

Second, you are passing an absolute path. 其次,你正在通过一条绝对的道路。 PHP is trying to open the directory staffportal in the root directory / , since you started the string with a / . PHP正在尝试在根目录/打开目录staffportal ,因为您使用/启动了字符串。 But presumably the staffportal directory is not directly in your filesystem root. 但可能是staffportal目录不直接在您的文件系统根目录中。

From your directory structure it would appear that staffportal is the root of your Laravel application. 从您的目录结构看, staffportal是Laravel应用程序的根目录。 You can get the path to that via Laravel's built in base_path helper function (see the Laravel docs ). 您可以通过Laravel内置的base_path辅助函数获取该路径(请参阅Laravel文档 )。 Or, since the directory you're looking for is inside public , you can use the function which gives you the path directly to there, public_path (from the same docs). 或者,由于您要查找的目录是public内部的,因此您可以使用直接提供路径的函数public_path (来自相同的文档)。

Try passing in the path like this: 尝试传递这样的路径:

dirToArray(public_path() . '/documents/ra_docs')

(You could use DIRECTORY_SEPARATOR instead of the slashes there if you have portability concerns and wanted to match the style of your function.) (如果您有可移植性问题并希望匹配函数的样式,则可以使用DIRECTORY_SEPARATOR而不是斜杠。)

I imagine, though, that you did intend that function to be a static function on your model. 不过,我想,您确实打算将该函数作为模型的静态函数。 So if you were in fact passing the path properly and so the above did not help, the error may be in your function where you attempt to recurse with a call to dirToArray -- this should be self::directoryToArray since you have declared this as a static function of a class. 因此,如果你实际上正确地传递了路径,所以上面没有帮助,错误可能在你的函数中,你试图通过调用dirToArray递归 - 这应该是self::directoryToArray因为你已经声明为一个类的静态函数。

I think it fails for 2 reasons: 我认为它失败有两个原因:

  1. use public_path() to be sure you access to public directory with the correct path 使用public_path()确保使用正确的路径访问公共目录
  2. you call dirToArray within your method, while your method is directoryToArray . 你在你的方法中调用dirToArray ,而你的方法是directoryToArray If this function is meant to be recursive, you should have the same name for both (and use self:: as a prefix) 如果此函数是递归的,那么两者应该具有相同的名称(并使用self::作为前缀)

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

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