扫描目录和打印文件无法正常工作

时间:2014-12-09 20:08:30

标签: php pdf directory

好的我有一个小问题而且我无法弄清楚它为什么会发生。我们网站上有很多月度通讯和内容,而不是每月手动更改页面,我们只是将pdfs转储到目录中,让PHP完成工作。我最近使用了一些我在至少其他三个地方使用的代码,并且它将所有文件打印为具有相同的月份(文件名的一部分),我无法弄清楚原因。文件命名结构是newsletter-2014-04-01.pdf或newsletter-year-month-day.pdf打印链接时,它链接到正确的文件,但将月份列为2月,并将月份作为日期。这个相同的代码在至少3个其他目录上运行得非常好......我唯一改变的是分钟到通讯和目录路径。有人可以告诉我为什么会这样做吗?

<?php

$PHP_SELF = '/'.current_path();

$base = DRUPAL_ROOT . "/assets/pdfs/cce/sbdc/newsletter";

$thisyear = date("Y");

# Strip out any HTML tags to eliminate cross-site scripting.
foreach ($_GET as $value) {
    $value = preg_replace("/<(.*?)>/","",$value);
}
# Then grab each GET value into a variable.
foreach ($_GET as $varname => $value) {
    $$varname = $value;
}

# If a year was not specified, use the current year by default.
if (!$year) {
    $year = $thisyear;
}

# List the year folders in the base directory (2001, 2002, et cetera).
if ($handle = opendir($base)) {
    while (false !== ($file = readdir($handle))) {
        // echo $file . '<br />';
        if( is_dir( $base . '/' . $file ) ){
            // echo $file . 'Is a directory <br />';
            if (preg_match("/\d\d\d\d/",$file)) {
                $years[count($years)] = $file;
            }
        }
    }

    closedir($handle);
    rsort($years);
}

# Is the specified year available in the list?
if (!in_array($year, $years)) {
    print "<p><font color=\"red\">Invalid year \"$year\".</font></p>";
    $year = $thisyear;
}


# List the target year directory.
$minutes = array();
$agendas = array();
$packets = array();
$directory = $base . '/' . $year;
if ($handle = opendir($directory)) {
    while (false !== ($file = readdir($handle))) {
        if (preg_match("/newsletter-\d\d\d\d-\d\d-\d\d\.pdf/",$file)) {
            $minutes[count($minutes)] = $file;
        }
            if (preg_match("/newsletter-\d\d\d\d-\d\d-\d\d\d\.pdf/",$file)) {
            $minutes[count($minutes)] = $file;
        }

    }
    closedir($handle);
    if (count($minutes)) {
        sort($minutes);
    }

    if (count($agendas)) {
        sort($agendas);
    }

}

print "<table width=\"100%\" id=\"content_table\" cellpadding=\"5\" border=\"1\" summary=\"Newsletters\"><tr width=\"600\" valign=\"top\">";

# Print the year index in the first column.
print "<td width=\"175px\" align=\"center\"><p><span class=\"bold\">Year index</span></p>\n";
for ($i = 0; $i < count($years); $i++) {
    if ($years[$i] == $year) {
        print "<b>";
    }
    if ($years[$i] == $thisyear) {
        print "<a href=$PHP_SELF>";
    } else {
        print "<a href=$PHP_SELF?year=$years[$i]>";
    }
    print "$years[$i]</a><br />\n";
    if ($years[$i] == $year) {
        print "</b>";
    }
}
print "</td>";

# Print the list of minutes in the second column.
print "<td width=\"250px\" align=\"center\"><p><span class=\"bold\">$year Newsletters</span></p>\n";
if (count($minutes)) {
    for ($i = 0; $i < count($minutes); $i++) {
        $y = substr($minutes[$i],8,4);
        $m = substr($minutes[$i],13,2);
        $d = substr($minutes[$i],16,2);
        print '<a href="/assets/pdfs/cce/sbdc/newsletter/' . $year . '/' . $minutes[$i] . '">';
        print date("F j", mktime(0,0,0,$m,$d));
        print "</a><br />\n";
    }
} else {
    print "No newsletters";
}
print "<br />\n";
print "</td></tr></table>\n";
    ?>

目录中的文件:  通讯,2014-2-8.pdf  通讯-2014-4-1.pdf

期望的输出是Febraury 8                   4月1日

Currecntly:2月2日                     2月4日

0 个答案:

没有答案