2

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

Bernn
  • 31
  • 5

3 Answers3

1

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

Bernn
  • 31
  • 5
  • good that you found it - you can either accept your own answer tomorrow, or you can remove your question if you don't think future visitors to SO will benefit from finding it and its associated answer. – Mike 'Pomax' Kamermans Apr 02 '15 at 21:50
0

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.

Mike 'Pomax' Kamermans
  • 49,297
  • 16
  • 112
  • 153
0

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.

Community
  • 1
  • 1
Brian Showalter
  • 4,321
  • 2
  • 26
  • 29