'PHP PDF display from SQL Server on IIS

I have this code that uploads a PDF into a SQL Server database

$attach_Name = $_FILES['aFile']['name'];
$attach_Name = str_replace("'","",$attach_Name);
$attach_Name = str_replace("\"","",$attach_Name);
$attach_Data = file_get_contents($_FILES['aFile']['tmp_name']);
$attach_Data = base64_encode($attach_Data);

And the SQL is:

INSERT INTO [dB].[dbo].[Doc_Data] ([DocInfoId],[DocData]) VALUES ('1',CAST('".$attach_Data."' AS 
VARBINARY(MAX)))

Then to retrieve and display it I use the following:

 $dbh = new PDO ('sqlsrv:server='.$dbServer.';database='.$dbName.'',''.$dbUN.'',''.$dbPass.'');
 $result = $dbh->prepare("SELECT [Doc_Data].[DocData]
                    FROM [dB].[dbo].[Doc_Data] 
                    WHERE [Doc_Info].[KeyID] ='1' 
 ");
 $result->execute();
 $row = $result->fetch(PDO::FETCH_ASSOC);

 $attach_Data = base64_decode($row['DocData']);
 $myfile = $row['DocName'];
 $myfile = str_replace(" ", "_", $myfile);
 $handle = fopen($myfile, "w+") ;//or die("Unable to open file!");
 $disp = fwrite($handle, $attach_Data);
 $type = filetype($myfile);
 $mode = "inline";
 header('Content-type: application/pdf');  
 header('Content-Disposition: inline; filename="' . $myfile . '"'); 
 header('Content-Transfer-Encoding: binary');
 header('Accept-Ranges: bytes');

// Read the file
@readfile($myfile);

fclose($handle);
unlink($myfile);
exit;

$dbh = null;

This works perfectly on Apache (XAMPP) but the client has moved to IIS and now my code fails to render the PDF anymore.

Any suggestions?

php


Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source