0

I am using php/mysql in my table I have a column called 'PDF'.

Each row has a path to a PDF file on my web server, I am using this PHP code:

$sql="SELECT * from table1 where customer_sequence = '53' and invoice_number = '1234' and sequence = '7839' ";
$rs=mysql_query($sql,$conn) or die(mysql_error());
$result=mysql_fetch_Array($rs);

header('Content-disposition: attachment; filename=/path/to/file/'.$result["pdf"].'');
header('Content-type: application/pdf');
readfile('/path/to/file/'.$result["pdf"].'');

The SQL is working ok, but once it tries to download its not doing anything.

I have also tried:

header("Location: /path/to/file/".$result["pdf"]."");

but still no luck - any ideas what I can do?

1 Answers1

1

Okay, let's sort it out. At first, you shouldn't output full path to your file in header. Just use this:

header('Content-disposition: attachment; filename='.$result["pdf"]);

filename in that header just tells browser a filename which it should use to save the file.

Second, readfile() doesn't follow path that browser uses when you paste URL. readfile() uses DOCUMENT_ROOT. For more information you can read this answer for example: Document Root PHP

EDIT Your code should look something like this:

$sql="SELECT * from table1 where customer_sequence = '53' and invoice_number = '1234'     and sequence = '7839' ";
$rs=mysql_query($sql,$conn) or die(mysql_error());
$result=mysql_fetch_Array($rs);

header('Content-disposition: attachment; filename='.$result["pdf"]);
header('Content-type: application/pdf');
readfile($_SERVER['DOCUMENT_ROOT'] . '/path/to/file/'.$result["pdf"]);
Community
  • 1
  • 1
barbashov
  • 542
  • 3
  • 9
  • aha right ok - so do i use header("Location:... or something else? –  Apr 08 '13 at 23:27
  • @charliejsford there's two different approaches for your task. In your case you can use `header("Location...`. – barbashov Apr 08 '13 at 23:29
  • so itll be header("Location: /path/to/file/".$result["pdf"].""); ?? –  Apr 08 '13 at 23:31
  • @charliejsford yes, `Location` header should contain URL to file. – barbashov Apr 08 '13 at 23:32
  • ok, i just used that and its display loads of the following %PDF-1.5 %���� 1 0 obj <>>> endobj 2 0 obj <> endobj 3 0 obj <>/ExtGState<>/XObject<>/ProcSet[/PDF/Text/ImageB/ImageC/ImageI] >>/MediaBox[ 0 0 595.32 841.92] /Contents 4 0 R/Group<>/Tabs/S/StructParents 0>> endobj 4 0 obj <> stream x��\mo�F�n��a��A,b�������.�^�z84�AQ�WK��r{�=��73��H�KR]�P��ffggv�����c_~y����9�N�N�lR,N�~�|�{}~�^_�~��i�����#�2����0'�4�j~|��k<|{|���%�bW����ٛ�����1>uʙJ�#�uz�'�N��?K��s|dD�5gژ�r&�N3�M�f����_�<]R� ()�F�T(y��� –  Apr 08 '13 at 23:33
  • @charliejsford this happens because your webserver doesn't send `Content-disposition: attachment` or `Content-type: application/pdf` like you do in your question. And browser handles your PDF like regular HTML text file. You can try two ways: 1. Configure your webserver to send headers properly; 2. Achieve success with code in your question (and my answer). – barbashov Apr 08 '13 at 23:34