Reputation: 31
I'm storing files in a LONGBLOB field in MySQL. When I retrieve the file using PHP, it has one extra space at the beginning of the file. I have checked my blob data in the MySQL Workbench viewer, the extra space is not in the raw data. Every file I download has an extra space at the beginning. The space does not exist in the $file['file'] variable, I have written it to a log and checked. It gets added somewhere in the handoff between PHP and the browser.
Here's the PHP:
<?php
include("log.php");
$log = new log;
$dbserver='localhost';
$dbid='xxxx';
$dbpw='xxxx';
$dbname='xxxx';
$mydb=mysqli_connect($dbserver,$dbid,$dbpw);
mysqli_set_charset($mydb,'utf8');
mysqli_select_db($mydb, $dbname) or die($log->log("Unable to select database"));
$q = "SELECT * FROM files WHERE id=$fileID";
$r = mysqli_query($mydb, $q) or die ($log->log(mysqli_error($mydb) . $q));
$file = mysqli_fetch_array($r, MYSQLI_ASSOC);
header("Content-Type: {$file['fileType']}");
header("Content-Disposition: filename=\"{$file['fileName']}\"", true);
header("Content-Length: {$file['fileSize']}");
echo trim($file['file']);
?>
I have seen other people identify extra spaces on the PHP page or the BOM in UTF-8 encoding, but I can find nothing like that on my page.
I'm using Notepad++ and UTF-8 w/o BOM. I also tried creating a new file on the server using vim through a terminal and no change.
Trim doesn't help either.
I've used Notepad++ to "Show all characters" hoping to find that extra something somewhere and there does not appear to be any extra characters hiding in my file.
The PHP open carat is the first character of the file and the closing carat is the last character of the file.
What else can there be?
Thanks, Bernn
Upvotes: 2
Views: 615
Reputation: 31
I'm so sorry, but the problem was in my log class which I did not show you. I removed the include and now the empty space is gone. It had one trailing space after the PHP close tag in that file. I appreciate the effort. Thanks, Bernn
Upvotes: 1
Reputation: 4349
Try taking out the closing tag ("?>") at the end. This closing tag isn't necessary, and if you have a newline after the ">" then that newline will get sent to the browser.
See Why would one omit the close tag? for additional info on PHP close tags.
Upvotes: 0
Reputation: 53597
UTF8 doesn't use a BOM, so you should never have to add one. However, you're adding headers with newlines, which if you look at the header() documentation for PHP, you should not be doing.
Upvotes: 0