Laravel 的 File::get() 为现有文件返回 null

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/16752055/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-14 07:49:14  来源:igfitidea点击:

File::get() for Laravel returns null for existing file

phplaravellaravel-3

提问by msturdy

Using the Laravelframework, I am using the File::get()method to access the CSS file in the public/cssdirectory returns null for a file that both exists, and has content:

使用Laravel框架,我使用该File::get()方法访问public/css目录中的 CSS 文件,为既存在又具有内容的文件返回 null:

the PHP code is:

PHP代码是:

private static $css_files = array(
                        "main"   => 'css/main.css',
                        "layout" => 'css/layout.css'
                        );

public static function get_css ($vars) {

    $file     = self::$css_files[$vars["file_name"]];
    $contents = File::get(path('public').$file, "File Not Found ($file)");

    return json_encode(array("file" => path('public').$file,
                         "contents" => $contents));

}

which returns to my webpage (from the js console in Chrome):

返回到我的网页(从 Chrome 中的 js 控制台):

Object {file: "/var/www/sites/public/css/main.css", contents: null} 

The file exists:

该文件存在:

user@pc:~$ ls /var/www/sites/public/css/main.css
/var/www/sites/public/css/main.css

has content:

有内容:

user@pc:~$ wc -l /var/www/sites/public/css/main.css
441 /var/www/sites/public/css/main.css

and the method doesn't trigger the $defaultargument in File::get()that it should if the file doesn't exist (tested with other valid and invalid names), so it has definitely found the file.

并且该方法不会触发$default参数File::get(),因为如果文件不存在(使用其他有效和无效名称进行测试),它应该触发该参数,因此它肯定找到了该文件。

I've tried other files in the same PHP method (get_css()), from other target directories, and the same directory, and it returns the content of those files with no problem.

我已经在相同的 PHP 方法 ( get_css()) 中尝试了来自其他目标目录和相同目录的其他文件,并且它毫无问题地返回了这些文件的内容。

the permissions on the CSS file are ok:

CSS文件的权限没问题:

user@pc:~$ ls -hal /var/www/sites/public/css/main.css 
-rw-r--r-- 1 www-data root 6.5K May 25 14:01 /var/www/sites/public/css/main.css

a separate PHP script, run from the command line returns the contents:

一个单独的 PHP 脚本,从命令行运行返回内容:

<?php    
  echo file_get_contents("/var/www/sites/public/css/main.css");
?>

here are the first few lines of the main.css

这是前几行 main.css

user@pc:~$ head /var/www/sites/public/css/main.css

/* Estilos principais*/

    body{
    background-color:#c5c5c5;
    font-family:sans-serif;
    font-size:1.5em;
    color:#999;
    text-align:center;
    margin:0;

Is this a bug? Are there any reasons (that I can't see right now) for it not liking the contents of the CSS file?

这是一个错误吗?是否有任何原因(我现在看不到)使它不喜欢 CSS 文件的内容?

回答by msturdy

Got it, two of the files were not utf-8, and json_encode()was choking on the non-utf-8 content, and return null, the issue was not in File::get(), but in json_encode().

心动不如行动,有两个文件是不是UTF-8,和json_encode()被窒息的非UTF-8的内容,和返回null,这个问题是不是在File::get(),但在json_encode()

Case closed!

案件结案!