0

I'm using custom code to download images, as I need to check permissions and keep a trace of each download (inter alia).

Here's how I decided to go:

  • Simple link, triggering an AJAX call of my download page
  • As it's an AJAX called page, html--ajax.tpl.php is used
  • Same for page--ajax.tpl.php
  • My final file force the image download

...and the content of the 3 related files:

html--ajax.tpl.php:

<?php print $page; ?>

page--ajax.tpl.php:

<?php print render($page['content']); ?>

download.ajax.php:

<?php

// ...extra code to populate $path and $filename

header('Content-Description: File Transfer');
header('Content-Type: application/octet-stream'); 
header('Content-Disposition: attachment; filename='.$filename);
header('Expires: 0');
header('Cache-Control: must-revalidate');
header('Pragma: public');
header('Content-Length: ' . filesize($path));
readfile($path);
exit;

My problem is that I've an extra space in my downloaded image bytes

The downloaded image is corrupted, so I decided to compare its bytes with the original image with xxd, and here's the result where you can see an extra 20 byte at the very beginning:

00000000: 20ff d8ff e000 104a 4649 4600 0101 0000   ......JFIF.....
00000010: 0100 0100 00ff fe00 3b43 5245 4154 4f52  ........;CREATOR
00000020: 3a20 6764 2d6a 7065 6720 7631 2e30 2028  : gd-jpeg v1.0 (
00000030: 7573 696e 6720 494a 4720 4a50 4547 2076  using IJG JPEG v
00000040: 3632 292c 2071 7561 6c69 7479 203d 2037  62), quality = 7
00000050: 300a ffdb 0043 000a 0707 0807 060a 0808  0....C..........
00000060: 080b 0a0a 0b0e 1810 0e0d 0d0e 1d15 1611  ................
00000070: 1823 1f25 2422 1f22 2126 2b37 2f26 2934  .#.%$"."!&+7/&)4
00000080: 2921 2230 4131 3439 3b3e 3e3e 252e 4449  )!"0A149;>>>%.DI
00000090: 433c 4837 3d3e 3bff db00 4301 0a0b 0b0e  C<H7=>;...C.....

I used the same approach as the one described in this post to find the origin the the space. The better I could have is to determine that the space appears in page--ajax.tpl.php :

00000000: 5020 44ff d8ff e000 104a 4649 4600 0101  P D......JFIF...

P is printed before the render(), in page--ajax.tpl.php, and D before my readfile().

Any ideas to helps me to find the origin of this space?

Thanks

2 Answers 2

1

Vinodkumar's answer didn't solved my issue, but it pointed me in the right direction.

I still haven't found the extra space, but as Drupal already used ob_start() in bootstrap.inc, I can now call ob_clean() to ensure no extra characters are shown:

<?php

// ...extra code to populate $path and $filename

ob_clean();
header('Content-Description: File Transfer');
header('Content-Type: application/octet-stream'); 
header('Content-Disposition: attachment; filename='.$filename);
header('Expires: 0');
header('Cache-Control: must-revalidate');
header('Pragma: public');
header('Content-Length: ' . filesize($path));
readfile($path);
exit;
0

Remove extra space in the file download.ajax.php at the top. If that didn't work then try to call ob_start() function at the start of the file and check it. It will work.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.