I have multiple hidden files in a directory and would like to open and/or cat开发者_开发技巧 each one of them, extract each value and display on a web page. Each file has a value already embedded in a format I need. So no need to parse anything except grab the value from the file.
I would like your input on a good programmatic approach to doing this either using perl or php. Would it be best to do it all in php or use both? How would you do it? Thanks for your input.
The files are named like so:
.stat_run_dir
.stat_file_sent.domain1
.stat_file_sent.domain2
Values would look like this respectively:
/absolute/path/category/testfeed_dom_2010120806.abc
domain1_sub.dat.2010120804.abc
domain2_sub.dat.2010120805.abc
In PHP, sending the contents of a file to the browser is one line:
<?php readfile('.stat_run_dir'); ?>
You can read an entire file into a variable just as easily:
<?php
$text = file_get_contents('.stat_file_sent.domain1');
echo $text;
?>
I would go by dirty solution, use grep
instead
system('grep YOUR_MATCHING_PATTERN YOUR_TARGET_FILES');
To answer "Perl or PHP", the answer is (all other things being equal) whichever one you're most comfortable in, especially - in case of web pages - in terms of debugging.
In Perl, to get the values is trivial:
use File::Slurp;
my %file_contents;
my @files = glob(".stat_*");
foreach my $file (@files) {
$file_contents{$file} = File::Slurp::read_file($file);
}
Displaying on web page depends on what you have available (ideally, a templating module which works pretty much the same way PHP code does, e.g. EmbPerl, HTML::Template etc...). Worst case scenario, do a simple CGI.pm page, though templating stuff is way easier.
An EmbPerl example would be (in the absence of example of how you want to print, I chose something):
[$ foreach $file @files $]
[+ $file +] : [+ $file_contents{$file} +]<br>
[$ endforeach $]
精彩评论