Using the Laravel framework, I am using the File::get()
method to access the CSS file in the public/css
directory returns null for a file that both exists, and has content:
the PHP code is:
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):
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 $default
argument 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.
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.
the permissions on the CSS file are ok:
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
echo file_get_contents("/var/www/sites/public/css/main.css");
?>
here are the first few lines of the 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?
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()
.
Case closed!