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?

4
  • Are you sure that file "/path/to/file/".$result["pdf"] really exists? Commented Apr 8, 2013 at 23:18
  • yep - i have echoed it then copy and pasted into web browser and the file downloads Commented Apr 8, 2013 at 23:19
  • did you tried header("Location: http://www.server.com/path/to/file/".$result["pdf"]""); ? Commented Apr 8, 2013 at 23:21
  • Try looking here stackoverflow.com/questions/4679756/… Commented Apr 8, 2013 at 23:21

1 Answer 1

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"]);
Sign up to request clarification or add additional context in comments.

6 Comments

aha right ok - so do i use header("Location:... or something else?
@charliejsford there's two different approaches for your task. In your case you can use header("Location....
so itll be header("Location: /path/to/file/".$result["pdf"].""); ??
@charliejsford yes, Location header should contain URL to file.
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���
|

Your Answer

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