我正在尝试使用PHP 5.4在SQL Server 2008数据库中上传文件。除了大小超过64KB的文件外,下载相同文件的代码工作正常。当我下载超过64 KB的文件时,它会被截断为64KB。
当我在本地尝试相同的事情时,它可以很好地工作,但只在开发服务器上提供文件截断问题。该网站托管在linux环境中。
我正在使用bin2hex()函数转换文件内容,如下所示。
$fileContent=file_get_contents($_FILES[$key]["tmp_name"]);
$fileContent="0x".bin2hex($fileContent);
然后在下载时我使用以下代码。
private $fileContents = '';
private $fileName = '';
private $contentType = '';
public function __construct()
{
//parent::__construct();
if (Tools::getPostVar('documentID')!="")
{
ini_set ('mssql.textlimit', 2147483647);
ini_set ('mssql.textsize', 2147483647);
$this->getFile();
$this->showFile();
}
else
Tools::ThrowError('Document ID not set', 'Document ID not set');
}
private function getFile()
{
$cn = new DbMobTool();
$cn->bIsConnPersist = false;
$cn->doQuery("exec stp_my_procedure ".Tools::getPostVar("documentID"));
$rs = $cn->fetchAll();
switch (Tools::getEnvironment()) {
case 'local':
$this->fileContents = hex2bin($rs[0]->document);
break;
case 'dev':
case 'stage':
case 'prod':
$this->fileContents = $rs[0]->document;
break;
}
$this->fileName = $rs[0]->document_name;
$s = strrchr($this->fileName, ".");
$s = substr($s, 1, 3);
switch (strtolower($s))
{
case 'pdf':
$this->contentType = 'application/pdf';
break;
case 'jpg':
$this->contentType = 'image/pjpeg';
break;
case 'xls':
$this->contentType = 'application/vnd.ms-excel';
break;
case 'ppt':
$this->contentType = 'application/vnd.ms-powerpoint';
break;
default:
$this->contentType = 'text/plain';
}
$cn->closeDbObject($rs);
}
private function showFile()
{
ob_clean();
ob_start();
//header('Content-Description: File Transfer');
//header('Expires: 0');
//header("Content-Transfer-Encoding: binary");
if ($this->contentType != '')
{
header ('Content-Type: ' . $this->contentType);
header ('Content-Title: ' . $this->contentType);
}
if(strpos($_SERVER['HTTP_USER_AGENT'], 'MSIE')===false)
header ('Content-Disposition: attachment; filename="' . $this->fileName . '"');
else
header ('Content-Disposition: attachment; filename="' . str_replace('+',' ',urlencode($this->fileName)) . '"');
header('Pragma: public');
header('Cache-Control: must-revalidate');
//clean all levels of output buffering
while (ob_get_level()) {
ob_end_clean();
}
echo $this->fileContents;
//ob_end_flush(); // Stops output buffering and 'flushes' output to client browser
}
我花了很多时间来解决这个问题。不确定这是配置问题还是我在发布文件时遗漏的内容。
提前致谢。