【发布时间】:2018-03-01 22:35:09
【问题描述】:
我正在尝试从 php 中的 Microsoft SQL varbinary 字段中检索文件。这些文件在插入数据库之前在 Visual Studio 程序中使用 IO.Compression.DeflateStream() 进行了压缩。
我可以从数据库中取出文件,但我不确定如何解压文件:
<?php
//assume that I already made the connection
$sql = "SELECT [FileData], [contentType] FROM dbo.Files where [FileGUID]='DAB88A50-E2C3-E311-A2D2-005056930304'";
$stmt= sqlsrv_query($conn , $sql);
if( $stmt === false ) {
if( ($errors = sqlsrv_errors() ) != null) {
foreach( $errors as $error ) {
echo "SQLSTATE: ".$error[ 'SQLSTATE']."<br />";
echo "code: ".$error[ 'code']."<br />";
echo "message: ".$error[ 'message']."<br />";
}
}
}
$result = sqlsrv_fetch_array($stmt, SQLSRV_FETCH_ASSOC);
$filename = "test.pdf";
$X = $result['FileData'];
$contentType= $result['ContentType'];
$filename= $result['Filename'];
header("Content-Type: $contentType; charset=utf-8");
header("Content-Disposition: attachment; filename=$filename"); //File name extension was wrong
header("Expires: 0");
header("Cache-Control: must-revalidate, post-check=0, pre-check=0");
//$X = unpack('H*hex', $X);
echo $X;
?>
上面的PHP获取了文件,但是还是压缩格式的。
在 VB 中,我可以使用以下代码解压缩 get 文件:
Dim Decompressed As Byte()
Using ms As New IO.MemoryStream(b)
Dim msOut As New IO.MemoryStream()
Dim ds As New IO.Compression.DeflateStream(ms, IO.Compression.CompressionMode.Decompress)
ds.CopyTo(msOut)
Dim strPath As String = "d:\fsout\" + strDBName
If (Not System.IO.Directory.Exists(strPath)) Then
System.IO.Directory.CreateDirectory(strPath)
End If
Dim file As New FileStream(strPath + "\" + strFilename, FileMode.Create, FileAccess.Write)
msOut.WriteTo(file)
file.Close()
ms.Close()
ds.Close()
不过,我希望能够在 PHP 中做到这一点。任何人都知道在 PHP 中解压缩文件的技巧吗?我感觉这与 php 中的 'unpack()' 有关,但不确定要使用哪些参数。
【问题讨论】:
标签: php sql-server