使用WAMP 2.2和PHP 5.4.3下载0kb

时间:2013-02-19 01:03:30

标签: php apache download wamp

我似乎在从PHP站点下载文件时遇到问题。文件是PDF,每次我下载任何文件,它们都是0kb。我使用WAMP 2.2和PHP 5.4.3在本地计算机上运行它。下面列出了使用的两个主要php文件。但是,我不知道它是PHP问题还是WAMP问题。

文件1:getAttachment.php

<?php
if (isset($_REQUEST['PHPSESSID'])) 
{
    session_id($_REQUEST['PHPSESSID']);
    $sid = $_REQUEST['PHPSESSID'];
}

session_start();

if (isset($_SESSION['authUserId'])) 
{
    $username = $_SESSION['authUsername'];
    $contactTypeName = $_SESSION['authContactTypeName'];
}

require_once("./util/dbConn.php");
require_once("./util/util.php");

$attachId = getRequestNum('id');

$row = singleQuery("select * from attachment where id=$attachId");
$storedAs = $row['storedAs'];
$filename = $row['filename'];
$filesize = $row['filesize'];

header("Content-disposition: attachment; filename=\"$filename\"");
header("Content-type: application/force-download");
header("Content-Transfer-Encoding: binary");
header("Content-Length: $filesize");
header("Pragma: no-cache");
header("Expires: 0");

$fp = fopen($storedAs, "rb");
$fsize = filesize($storedAs);
$fileContent = fread($fp, $fsize);
fclose($fp);

echo $fileContent;
?>

文件2:util.php

<?php
// this file contains a bunch of helper utilities used by the other pages

// necessary in some files like print pages and the like
require_once("dbConn.php");

function singleQueryField($query, $field)
{
    $row = singleQuery($query);
    return $row[$field];
}

function singleQuery($query)
{
    $resultSet = dbQuery($query);
    return mysql_fetch_assoc($resultSet);
}

function displayPicklistCustomSQL($sql, $custom, $selectName, $default="null")
{
    $enumRS = dbQuery($sql);
    print '<select name="' . $selectName . '" id="' . $selectName . '" onChange="picklistChange()" id="' . $selectName . '">';
    print '<option value="null">--- choose value ---</option>';
    while ($rowEnum = mysql_fetch_assoc($enumRS))
    {
        $value = $rowEnum['id'];
        $name = $rowEnum[$custom];
        $selected = ($default == $value) ? "selected" : "";
        print '<option value="' . $value . '" ' . $selected .'>' . $name . '</option>';
    }
    print '</select>';
}

function displayPicklistSQL($sql, $selectName, $default)
{
    displayPickListCustomSQL($sql, 'name', $selectName, $default);
}


function displayPicklist($table, $selectName, $default="null")
{
    displayPicklistSQL("select * from " . $table . " order by name", $selectName, $default);
}

function displayList($listName, $default) {
    $listId = singleQueryField("select id from lists where name='" . $listName . "'", "id");
    print '<select name="' . $listName . '">';
    print '<option value="null">--- choose value ---</option>';
    $enumRS = mysql_query("select * from listEntries where list=" . $listId) or die(mysql_error());
    while ($rowEnum = mysql_fetch_assoc($enumRS))
    {
        $value = $rowEnum['id'];
        $name = $rowEnum['entry'];
        $selected = ($default == $value) ? "selected" : "";
        print '<option value="' . $value . '" ' . $selected .'>' . $name . '</option>';
    }
    print '</select>';
}

function displayNumberedPicklist($table, $selectName, $default) {
    $sql = "select id, name from $table";
    $enumRS = dbQuery($sql);
    print '<select name="' . $selectName . '" id="' . $selectName . '" onChange="picklistChange()" id="' . $selectName . '">';
    print '<option value="null">--- choose value ---</option>';
    while ($rowEnum = mysql_fetch_assoc($enumRS))
    {
        $value = $rowEnum['id'];
        $name = $rowEnum['name'];
        $selected = ($default == $value) ? "selected" : "";
        print '<option value="' . $value . '" ' . $selected .'>' . "$value. $name" . '</option>';
    }
    print '</select>';
}

function displayStaffPicklist($default = "")
{
    $staffId = listIdByName('accountTypes', 'staff');
    $sql = "SELECT username, id FROM users WHERE accountType=$staffId ORDER BY username";
    displayPicklistCustomSQL($sql, "username", "assignedTo", $default);
}

function getListEntryName($listName, $id)
{
    $listId = singleQueryField("select id from lists where name='" . $listName . "'", "id");
    $entryName = singleQueryField("select entry from listEntries where list=$listId AND id=$id", "entry");
    return $entryName;
}

function getListEntryId($listName, $entryName)
{
    $listId = singleQueryField("select id from lists where name='" . $listName . "'", "id");
    $entryName = singleQueryField("select id from listEntries where list=$listId AND entry='$entryName'", "id");
    return $entryName;
}

function getListId($listName)
{
    $listId = singleQueryField("select id from lists where name='$listName'", "id");
    return $listId;
}

function listIdByName($list, $name)
{
    $val = singleQueryField("select id from $list where name='$name'", "id");
    return $val;
}

function listNameById($list, $id)
{
    if (!is_numeric($id)) return "";

    $val = singleQueryField("select name from $list where id=$id", "name");
    return $val;
}

if (isset($_REQUEST['debugSQL'])) debug(stripslashes($_REQUEST['debugSQL']));

// validates the parameter is a number from 0-255
function validateNumber255($num)
{
    if (!ereg("^[0-9]+$", $num))
        return false;

    if ($num < 0 || $num > 255)
        return false;

    return true;
}

function validateFormString($str) { return (strlen($str) > 0) ? true : false; }

function validateTimeStamp($str)
{
    if (ereg("^[0-9]+-[0-9]+-[0-9][0-9] [0-9]+:[0-9][0-9] [AP][mM]", $str))
        return true;
    else
        errorMessage("Invalid date / time.");

    return false;
}

function validateTime($str)
{
    if (ereg("^[0-9]+:[0-9][0-9] [AaPp][mM]", $str))
        return true;
    else if (strlen($str)>0)
        errorMessage("Invalid time.");
    else
        return false;
}

function validateDate($str)
{
    if (ereg("^[0-9]+-[0-9]+-[0-9][0-9]", $str))
        return true;
    else if (strlen($str) > 0)
        errorMessage("Invalid date.");
    else
        return false;
}

function debug($sql)
{
    $rs = dbQuery($sql);
    $numRows = mysql_num_rows($rs);
    echo "<h3>$sql</h3>";
    echo "($numRows rows found)<br>";
    echo '<table class="panel">';
    $first = 0;
    while ($row = mysql_fetch_assoc($rs))
    {
        if ($first == 0) {
            echo '<tr class="headerListing">';
            foreach($row as $colName=>$value)
                echo "<td>$colName</td>";
            echo "</tr>";
            $first++;
        }

        $i++;
        $rowId = "t" . $i;
        $className = (($i%2)==0) ? "even" : "odd";
        ?> <tr id="<?=$rowId?>" class="<?=$className?>" onMouseOver="rowHighlight('<?=$rowId?>');" onMouseOut="rowUnhighlight('<?=$rowId?>');"> <?

        foreach($row as $colName=>$value)
        {
            echo "<td>$value</td>";
        }
        echo "</tr>";
    }
}

function convertToMySQLDateTime($str)
{
    if (validateTimeStamp($str)) {
        list($month, $day, $year, $hour, $minute, $ampm) = split('[-:\ ]', $str);
        $year += 2000;
        if ($ampm == "AM" && $hour == "12")
            $hour = 0;
        if ($ampm == "PM" && $hour != "12")
            $hour += 12;
        $newStr = $year . "-" . $month . "-" . $day . " " . $hour . ":" . $minute;
        return $newStr;
    } else return null;
}

function convertToMySQLTime($str)
{
    if (validateTime($str)) {
        list($hour, $minute, $ampm) = split('[-:\ ]', $str);
        $ampm = strtoupper($ampm);
        if ($ampm == "AM" && $hour == "12")
            $hour = 0;
        if ($ampm == "PM" && $hour != "12")
            $hour += 12;
        $newStr = $hour . ":" . $minute;
        return $newStr;
    } else return null;
}

function convertToMySQLDate($str)
{
    if (validateDate($str)) {
        list($month, $day, $year) = split('[-]', $str);
        if ($year > 50)
            $year += 1900;
        else
            $year += 2000;
        $newStr = $year . "-" . $month . "-" . $day;
        return $newStr;
    } else return "null";
}

function MySQLDateOrNull($str)
{
    if (strlen($str) > 0)
        return "'$str'";
    else return "null";
}

function formattedFilesize($filesize)
{
    $metric = "";
    if ($filesize >= 1024) {
        $filesize /= 1024;
        $metric = " KB";
        if ($filesize >= 1024) {
            $filesize /= 1024;
            $metric = " MB";
        }
    }
    return sprintf("%1.2f %s", $filesize, $metric);
}

function dbQuery($sql)
{
    $errorMessage = "<hr><h1>MySQL Error</h1>Attempting to execute a command that is generating an error in the ".
        "database.  This is most likely caused by either entering bad data or a due to a program bug. " .
        "If you continue to experience this problem please contact the developer." . 
        "<hr><h3>SQL Command</h3><div style='border: 1px solid #a0a0a0; background-color: #fffff0'>" . $sql .
        "</div><hr><h3>Error Message from database";
    $rs = mysql_query($sql) or die($errorMessage . " (" . mysql_errno() . ")</h3>" . 
        "<div style='border: 1px solid #a0a0a0; background-color: #fffff0'>" . mysql_error() . "</div><hr>");
    return $rs;
}

function rowCount($sql)
{
    $rs = dbQuery($sql);
    return mysql_num_rows($rs);
}

function getNumericRequestVar($varName)
{
    $value = (is_numeric($_REQUEST[$varName])) ? $_REQUEST[$varName] : "null";
    return $value;
}

function currency2String($amount)
{
    return sprintf("%4.2f", $amount);
}

function displayTristate($name, $value="")
{
    if ($value == 'u') $uSelected = "selected";
    else if ($value == 'y') $ySelected = "selected";
    else if ($value == 'n') $nSelected = "selected";
    print '<select name="' . $name . '" id="' . $name . '">';
    print '<option value="u" ' . $uSelected . '>unspecified</option>';
    print '<option value="y" ' . $ySelected . '>yes</option>';
    print '<option value="n" ' . $nSelected . '>no</option>';
}

function displayTristateRequired($name)
{
    if ($name == "needsDriver") $bgColor = 'style="background-color: #fff0f0"';
    print '<select name="' . $name . '" id="' . $name . '"' . $bgColor . '>';
    print '<option value="null">-- choice required --</option>';
    print '<option value="u">unspecified</option>';
    print '<option value="y">yes</option>';
    print '<option value="n">no</option>';
}

function tristate($value)
{
    if ($value == 'y') return "yes";
    else if ($value == 'n') return "no";
    else return "unspecified";
}

// returns the number or the string "null"
function getSQLNum($val) { return (is_numeric($val)) ? $val : "null"; }
function getSQLStr($val) { return mysql_real_escape_string($val); }

function getSQLDate($str)
{
    return (strlen($str) > 0) ? "'$str'" : "null";
}

function requestDateToDB($str)
{
    return getSQLDate(convertToMySQLDate($str));
}

function getRequestNum($whichVar) { return getSQLNum($_REQUEST[$whichVar]); }
function getRequestStr($whichVar) { return getSQLStr($_REQUEST[$whichVar]); }

function stringExists($str)
{
    if (strlen($str)>0)
        return true;
    else
        return false;
}
?>

0 个答案:

没有答案