This topic is locked
[SOLVED]

 readfile doesn't work in button

12/8/2010 9:55:43 AM
PHPRunner General questions
S
sgchan author

I have a program where each row has a MS-WORD document uploaded (BLOB). In the Listing screen, I want to create a button to allow the user to download MS-WORD document of the selected rows. Hence I have the below code in the button:
$zip = new ZipArchive;

$res = $zip->open('resume.zip', ZipArchive::CREATE);

if ($res === TRUE) {

foreach($keys as $ind=>$val){

$zip->addFromString($val['File_Name'], $val["File"]);

}

}

$zip->close();

header("Content-type: application/octet-stream");

header("Content-disposition: attachment; filename=resume.zip");

if (fileexist('resume.zip')){readfile('resume.zip');}
For most parts the code works (I tested by commenting the code and letting the sample JavaScript pass the "Hello World" text in Before and After) except for the READFILE command. It will not work in the button at all but I know the code is correct. I do not understand why (I am not a PHP/JavaScript expert). Could somebody help advice me on how to force a download?
Many thanks in advance.

Sergey Kornilov admin 12/8/2010

This behaviour is by design. Button's PHP code is executed behind the scene via AJAX and you cannot use readfile() there.
The best approach is to set 'View as' type of this field to 'File'.

S
sgchan author 12/8/2010

Please correct me if I am wrong. Using View As - File only allows the downloading of one document at a time - not multiple at one go. So that's not quite what I want.
Are all buttons driven by AJAX? I was thinking of "hijacking" the DELETE button, setting the Return to FALSE (stopping the Delete) but executing the compiling, zipping and downloading.
Another way could be use the same approach as EXPORT SELECTED button. Collate all the Row ID keys, direct it to another page and perform the compiling, zipping and downloading from there.
Hope to hear from you and many thanks in advance.

S
sgchan author 12/9/2010

I've hijacked the <table>_export.php file, specifically the ExportToCSV function. Since it was passing the array of selected records, I decided it would be an ideal place to ZIP up attached files.