Can anyone tell me the best practice for including a set of files in a directory?
For instance, if I had a directory that was /javascript, and in there it had 10 javascript files, how would I go about loading the entire directory?
This question is moot now, but for future reference;
I use the following to read files in the same manner:
<?php
session_start();
// Auto import all js files in the directory.
$files = glob("*.js");
sort($files);
$jsFiles = array();
for($i = 0; $i < count($files); $i++){
error_log('Auto-Opening: '.$files[$i]);
$tempString = file_get_contents($files[$i]);
//error_log('Auto-Opened Content: '.$tempString);
array_push($jsFiles, $tempString);
}
?>
Sorry, I guess this needs clarification:
The code I have included is used in an entirely seperate part of my project, it includes the RAW string format of a directory of javascript files for my processing purposes. You do not have to worry about why I am doing this.
I could use this to add script
tags, and echo these files, but I am looking for a faster more elegant way to include a directory. Unlike the code above (whose javascript never gets executed by my code. I simply create the string from the files and send it to JS for parsing).
Please also note, as suggested:
$files = glob("{directory/*.js}",GLOB_BRACE);
for($i = 0; $i < count($files); $i++){
include($files[$i]);
}
Does nothing but echo those files to the screen, without script
tags.
Can anyone tell me the best practice for including a set of files in a directory?
For instance, if I had a directory that was /javascript, and in there it had 10 javascript files, how would I go about loading the entire directory?
This question is moot now, but for future reference;
I use the following to read files in the same manner:
<?php
session_start();
// Auto import all js files in the directory.
$files = glob("*.js");
sort($files);
$jsFiles = array();
for($i = 0; $i < count($files); $i++){
error_log('Auto-Opening: '.$files[$i]);
$tempString = file_get_contents($files[$i]);
//error_log('Auto-Opened Content: '.$tempString);
array_push($jsFiles, $tempString);
}
?>
Sorry, I guess this needs clarification:
The code I have included is used in an entirely seperate part of my project, it includes the RAW string format of a directory of javascript files for my processing purposes. You do not have to worry about why I am doing this.
I could use this to add script
tags, and echo these files, but I am looking for a faster more elegant way to include a directory. Unlike the code above (whose javascript never gets executed by my code. I simply create the string from the files and send it to JS for parsing).
Please also note, as suggested:
$files = glob("{directory/*.js}",GLOB_BRACE);
for($i = 0; $i < count($files); $i++){
include($files[$i]);
}
Does nothing but echo those files to the screen, without script
tags.
-
3
Is there a reason you 're reading the contents instead of outputting
<script src="...">
tags? – Jon Commented Sep 16, 2011 at 13:37 - 1 I imagine it's concatenation; serving one file instead on ten. – Alex Commented Sep 16, 2011 at 13:38
- If you wanna make a include for example to use some functions inside a file -> create a single_file.php and call everything inside this, with <script src="dir/file.js"> then make a call of single_file.php in your scripts, then all js files will be present there, and you can use functions from that file... – devasia2112 Commented Sep 16, 2011 at 13:40
- Maybe he wants to reduce the number of http request :) – xdazz Commented Sep 16, 2011 at 13:41
- Yeah, so neither of those three things. I am reading the directory as strings because I am doing some parsing with the raw JS (but that is separate from what I am doing here). I know to use php to include a bunch of files by statically coding them in, I want to do it on a by directory basis that is dynamic. – GAgnew Commented Sep 16, 2011 at 13:43
4 Answers
Reset to default 3I would simply call include()
on the files rather than doing a file_get_contents()
. This is for two main reasons/benefits:
- You are not loading the files into memory (in PHP anyway) before outputing them. This saves server resources (albeit a fairly small amount).
- You can put PHP code into your JS/CSS etc and have it be evaluated. This means you can predefine JS variables at the server side, apply colour themes to your CSS and a multitude of other things very simply through PHP.
PHP will not output <script>
tags for you. It has no idea what JS is, other than a chunk of text. Your glob version is close, but would have to be:
$files = glob("{directory/*.js}",GLOB_BRACE);
for($i = 0; $i < count($files); $i++){
echo '<script type="text/javascript">';
include($files[$i]);
echo '</script>';
}
I could use this to add script tags, and echo these files, but I am looking for a faster more elegant way to include a directory. Unlike the code above (whose javascript never gets executed by my code. I simply create the string from the files and send it to JS for parsing).
If you want the output of the PHP script to get executed as javascript, you need to add a proper header:
header("Content-type: text/javascript");
foreach (glob('/path/to/files/*.js') as $file)
{
readfile($file);
}
I am reading the directory as strings because I am doing some parsing with the raw JS (but that is separate from what I am doing here). I know to use php to include a bunch of files by statically coding them in, I want to do it on a by directory basis that is dynamic.
Unless you have some sort of file naming convention going on, remember that the order of the files is going to be basically limited to what you can do with php's array sorting functions on the file names. JS and CSS are often very dependent on code or files being executed or read in a certain order, so depending on what it is you're actually trying to acplish (still very unclear), this might be an awful idea.
Don't.
Compile your Javascript files into a single, minified source using the Closure piler, and have your HTML reference that instead.