OS2 World Community Forum
OS/2, eCS & ArcaOS - Technical => Programming => Topic started by: Greggory Shaw on February 04, 2015, 09:00:42 am
-
Need for help with PHP for my site !
I have some php code for a Hobbes listing on my site that needs some help:
http://os2notes.net/#ftplist
1) I would like to filter out the .txt files in column one.
2) Add a html link for the filter out .txt item to column two.
2b) Parse the 'Short description' from the README.TEMPLATE & display it !
3) Filter an item by name or partial name.
4) limit the items to be printed.
It would also help me with a new news format I'm working on, RSS to html format.
http://os2notes.net/pin.php
Thanks
Greggory
<?php
$filetypes = array('-'=>'file', '1'=>'directory', 'l'=>'link');
// little helper functions
function byteconvert($bytes) {
$symbol = array('B', 'KB', 'MB', 'GB', 'TB', 'PB', 'EB', 'ZB', 'YB');
$exp = floor( log($bytes) / log(1024) );
return sprintf( '%.1f ' . $symbol[ $exp ], ($bytes / pow(1024, floor($exp))) );
}
$c = ftp_connect('128.123.34.6');
ftp_login($c, 'anonymous', 'anonymous@anon.com');
$rawList = ftp_rawlist($c, '/pub/incoming');
function itemize_dir($contents) {
foreach ($contents as $file) {
if(ereg("([-dl][rwxstST-]+).* ([0-9]*) ([a-zA-Z0-9]+).* ([a-zA-Z0-9]+).* ([0-9]*) ([a-zA-Z]+[0-9: ]*[0-9])[ ]+(([0-9]{2}:[0-9]{2})|[0-9]{4}) (.+)", $file,$regs)) {
$type = (int) strpos("-dl", $regs[1]{0});
$tmp_array['line'] = $regs[0];
$tmp_array['type'] = $type;
$tmp_array['rights'] = $regs[1];
$tmp_array['number'] = $regs[2];
$tmp_array['user'] = $regs[3];
$tmp_array['group'] = $regs[4];
$tmp_array['size'] = $regs[5];
$tmp_array['date'] = date("m-d",strtotime($regs[6]));
$tmp_array['time'] = $regs[7];
$tmp_array['name'] = $regs[9];
}
$dir_list[] = $tmp_array;
}
return $dir_list;
}
$buff = $rawList;
$items = itemize_dir($buff);
//$items = array_sort($items, 'date', SORT_DESC);
//the data you supplied. normally just an array
$data = $items;
//show what we got going into sort
//echo '<pre>'.print_r($data, 1).'</pre>';
function cmp($a,$b){
//get which string is less or 0 if both are the same
$cmp = strcasecmp($b['date'], $a['date']);
//if the strings are the same, check name
if($cmp == 0){
//compare the name
$cmp = strcasecmp($a['name'], $b['name']);
}
return $cmp;
}
//sort using a custom function
usort($data, 'cmp');
//show what we got after sort
//echo '<pre>'.print_r($data, 1).'</pre>';
//foreach ($data as $filename => $fileinfo) {
// echo $fileinfo['date'] . " - " . $fileinfo['name'] . " - " . $fileinfo['time'] . " | " . //byteconvert($fileinfo['size']) . " " . "<br>";
// }
$dir = array();
$file = array();
foreach ($data as $k => $v) {
if ($v['rights']{0} == "d") {
$dir[$k] = $v;
} elseif ($v['rights']{0} == "-") {
$file[$k] = $v;
}
}
foreach ($file as $filename => $fileinfo) {
echo "
<table width=\"100%\">
<col style=\"width:69%\">
<col style=\"width:31%\">
<tbody>
<tr>
<td>". "<li>"
. $fileinfo['date'] . " - " . "<a href=\"http://hobbes.nmsu.edu/download/pub/incoming/"
. $fileinfo['name'] ."" . $item->id_str ."\" target=\"_blank\">"
. $fileinfo['name'] ."</a>"
."</li>" . "
</td>
<td float=\"right\">"
. "<a class=\"various\" data-fancybox-type=\"iframe\" href=\"http://hobbes.nmsu.edu/download/pub/incoming/"
. $fileinfo['name'] .""
. $item->id_str ."\" target=\"_blank\">Download</a>" ." | " . "<a href=\"#\" class=\"togglelink\">Details</a>"
. " <div class=\"toggle\" style=\"display: block;\">"
. $fileinfo['line'] ." </div>" . " " ." | "
. byteconvert($fileinfo['size']) . "
</td>
</tr>
<tbody>
</table>";
}
echo "directories: ";
foreach ($dir as $dirname => $dirinfo) {
echo "
<td>". "<li>"
. "<a href=\"http://hobbes.nmsu.edu/download/pub/incoming/"
. $dirinfo['name'] . ""
. $item->id_str . "\" target=\"_blank\">"
. $dirinfo['name'] . "</a>" . " - "
. $dirinfo['time'] . "</li>" . "
</td>
[ $dirname ] "
. $dirinfo['name'] . " | "
. $dirinfo['type'] . " | "
. $dirinfo['group'] . " | "
. $dirinfo['month'] . " "
. $dirinfo['day'] . " "
. $dirinfo['time'] . "<br>";
}
?>
-
1) I would like to filter out the .txt files in column one.
2) Add a html link for the filter out .txt item to column two.
2b) Parse the 'Short description' from the README.TEMPLATE & display it !
3) Filter an item by name or partial name.
...
Well, one was easy:
// parse short description
$myFile = $myFile = 'http://hobbes.nmsu.edu/download/pub/incoming/' . $fileinfo['name'];
$lines = file($myFile); //file in to an array not recommeded for large files, use $lines[4] to print short desc.
// get extension
$ext = pathinfo($fileinfo['name'], PATHINFO_EXTENSION);
// filter txt files
if ($ext !== 'txt') {
// rename extension
$textName = preg_replace('"\.(zip|wpi)$"', '.txt', $fileinfo['name']); // rename zip/wpi to txt
// split short description line in two, to print $pieces[1]
$pieces = explode(":", $lines[4]);
// print file listing here
}
// filter item
$string = $item['title'];
if(stristr($string, 'netlabs.org newsletter') === FALSE) {
// filter in match
}
Funny note: always work with proofed data ! While I was working on this Hobbes processed the incoming directory. I went over the code 3 or 4 times wondering why only the README.TEMPLATE was showing.