I have a bunch of JavaScript files that I want to minify and then cache it to a file. This file is saved to a location I provide as argument.
How can I make sure that location has is accessible from the web?
What I’ve tried so far:
$test = array
(
'/Users/www/htdocs/test/nhecos/cache/test.js',
'/Users/www/foo.js'
);
$host = 'http://localhost/';
foreach($test as $t) {
$url = str_replace($_SERVER['DOCUMENT_ROOT'], $host, $t, $count);
$perms = substr(sprintf('%o', fileperms($t)), -4);
$p = substr($perms, -1);
if ($count > 0 && $p >= 4)
print $url;
else
throw new \Exception(sprintf("File %s not accessible from Web!", $t));
}
This works most of the times but fails if, for instance, that file is in a Virtual Host directory or in a linked folder. Is there any better way?
NOTE: my problem is ensuring that
“The files I generate are in a location that is known to the web
server and is mapped to a URL.”
To be accessible from the web is one thing, and to have “public read access” is another. You don’t need one to do the other.
Typically “public read access” means OS-specific filesytem permissions. Accessible from the web means:
It is in a directory that is known to a web server – that is, the file system location is mapped by the web server to a URL.
The permissions for the file (and enclosing directory) are such that the web server process can read the file (for static files), and can execute the files (for scripts that are run from a handler that runs as the same user as the web server).
So, in your case you want to make sure that:
The script runs as the same user as the web server – this is normally the case, but in some situations your scripts may be running as a different system user.
The files you generate are in a location that is known to the web server and is mapped to a URL.
Finally, the permissions of the file you have generated (especially if the user that executes your script is not the same as the web server process), has the correct permissions based on the system’s layout.
On most unix-like systems, the web server runs as
wwworapache. On debian itswww-data.Generally, static files need octal permissions
644, that is owner torw, and the rest to only read. Inls -llisting, this is-rw-r--r--for files if the directory isn’t owned by the server process.For troubleshooting access permissions, the apache wiki has tips.