Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations Chris Miller on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

PHP and Word Document size limit

Status
Not open for further replies.

evergrean100

Technical User
Dec 1, 2006
115
US
I have a word doc that works great where it shows database info. The issue is when the records increase over 10 records the Word Doc doesnt come up and just shows all the results in a php web page. Please advise if there is a buffer or length or something I can change to make this work where it will show the Word Document all the time no matter if I am above a certain size of records.

Code:
//database query and listing here

<?php
    $fname="report.doc";
    $handle = fopen( $fname, "rb" );
    $buf = fread( $handle, filesize( $fname ));
    fclose( $handle );
    $len = strlen( $buf );
    header( "Pragma: public" );
    header( "Cache-Control: private" );
    header( "Connection: close" );
    header( "Content-Type: application/msword" );
    header( "Content-Length: $len" );
    header( "Content-Disposition: inline; filename=\"$fname\"" );
        print $buf;
?>
 
there are no limits that I am aware of that would make a difference in this set up.

i would tweak your code a bit though
Code:
<?php
    ob_end_clean(); //kill output buffering just in case
    $file="report.doc";
    if (!is_readable($file)){   
      die ("cannot read the file or file does not exist");
    }
    $len = filesize($file);
    header( "Pragma: public" );
    header( "Cache-Control: private" );
    header( "Content-Type: application/msword" );
    header( "Content-Length: $len" );
    header( "Content-Disposition: inline; filename=\"$fname\"" );
    readfile ($file);
    exit(); //just to prevent continued execution, output of blank lines etc
?>
 
Thanks, I tried your suggestion and it still shows web page and not the Word Document.

I cant understand why it wont show anything past 10 records in a Word Document and instead goes right to a web page where it shows all the records. I am only showing 10 small field values per record.

Any other suggestions?
 
can you clarify your meaning where you say "goes right to a web page"?

does the word file download fine if you change the content type to application/octet-stream and the disposition to attachment?
 
Thanks again for all the help.

I still have same issue if I change to application/octet-stream and the disposition to attachment.

I saved this php file as docReport.php and then link to it. If I have more then 10 records it doesnt bring up a Word Document and instead goes to a web page called docReport.php where it shows all the Database results.

Here is the whole page with the Database connection and info:

Code:
<table border="1">
<tr>
<td>FieldOneHeader</td>
<td>FieldTwoHeader</td>
<td>FieldThreeHeader</td>
...
<td>FieldTenHeader</td>
</tr>
<?php
//Oracle DB Connection username and password etc here

$s = OCIParse($connection. "select * from theTable");
OCIExecute($s, OCI_DEFAULT);
while (OCIFetch($s)) {
   print '<tr><td>' . ociresult($s, "FIELDONENAME") . '</td>';
   print '<td>' . ociresult($s, "FIELDTWONAME"). '</td>';
   print '<td>' . ociresult($s, "FIELDTHREENAME"). '</td>';
   //shows up to 10 fields with one or two word values
   print '<td>' . ociresult($s, "FIELDTENNAME"). '</td></tr>';
}
print '</table>';

  $fname="report.doc";
    $handle = fopen( $fname, "rb" );
    $buf = fread( $handle, filesize( $fname ));
    fclose( $handle );
    $len = strlen( $buf );
    header( "Pragma: public" );
    header( "Cache-Control: private" );
    header( "Connection: close" );
    header( "Content-Type: application/msword" );
    header( "Content-Length: $len" );
    header( "Content-Disposition: inline; filename=\"$fname\"" );
    print $buf;
?>


 
well, the code you posted above will output to the screen. i would expect the download of a file afterwards to fail as the headers for screen based html output have already been sent.

i can see nothing in your code that would create a limit of 10 records or similar.

note that you cannot output to the screen and to a download in the same "go" (for want of a better word).

can we back up a couple of steps and perhaps you can describe the process that you are intending your script to perform. use pseudo code for the description but be as thorough and precise as you can.
 
Thanks for all your replies!

I got it to work where I moved the DB connection after the opening of the Word Doc:
Code:
<?php 
  $fname="report.doc"; 
    $handle = fopen( $fname, "rb" ); 
    $buf = fread( $handle, filesize( $fname )); 
    fclose( $handle ); 
    $len = strlen( $buf ); 
    header( "Pragma: public" ); 
    header( "Cache-Control: private" ); 
    header( "Connection: close" ); 
    header( "Content-Type: application/msword" ); 
    header( "Content-Length: $len" ); 
    header( "Content-Disposition: inline; filename=\"$fname\"" ); 
    print $buf; 




<table border="1"> 
<tr> 
<td>FieldOneHeader</td> 
<td>FieldTwoHeader</td> 
<td>FieldThreeHeader</td> 
... 
<td>FieldTenHeader</td> 
</tr> 
<?php 
//Oracle DB Connection username and password etc here 

$s = OCIParse($connection. "select * from theTable"); 
OCIExecute($s, OCI_DEFAULT); 
while (OCIFetch($s)) { 
   print '<tr><td><font size="-1">' . ociresult($s, "FIELDONENAME") . 
'</font></td>'; 
   print '<td><font size="-1">' . ociresult($s, "FIELDTWONAME"). '</ 
font></td>'; 
   print '<td><font size="-1">' . ociresult($s, "FIELDTHREENAME"). '</ 
font></td>'; 
   print '<td><font size="-1">' . ociresult($s, "FIELDFOURNAME"). '</ 
font></td>'; 
   print '<td><font size="-1">' . ociresult($s, "FIELDFIVENAME"). '</ 
font></td>'; 
   //rest here ....... 
   print '<td><font size="-1">' . ociresult($s, "FIELDNINENAME"). '</ 
font></td>'; 
   print '<td><font size="-1">' . ociresult($s, "FIELDTENNAME"). '</ 
font></td></tr>'; 


} 

print '</table>';

It works great where I click on a link and it brings up the Word Document with all the records. The wierd part is if I only put font size <font size="-1"> in the first 5 fields of the PHP while loop it works but if I try and put it in more than 5 fields, the Word Document comes up blank. If I take out the <font size="-1"> part it works great and the Word Document comes up with all the Records.

Any ideas or comments on this issue?
Any ideas on this
 
i don't understand how your code can work at all. sfaik you cannot output to a download and to the browser in the same. browsers do not accept multipart/mixed content types.

I suspect you are not showing us all of your code.

i do not know the effect of including <font size="-1">. probably a question for the html/css forum.
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top