I worked two hours to solve it but I need you mates. I need to make an array with which I get from db. Lets take a look at:
$conn_msg_attach = mysql_query("SELECT * FROM task_msg_attach WHERE msg_ID = '$msgID'");
while($get_msg_attach = mysql_fetch_array($conn_msg_attach)) {
$msg_attach_path = $get_msg_attach['msg_filepath'];
$msg_attach_fullpath = '../../../files/task_files/'.$msg_attach_path;
$files = array($msg_attach_path => $msg_attach_fullpath);
}
With above code I need to make is:
$files = array(
'something.ttf' => '../../../files/task_files/something.ttf',
'dsc.jpg' => '../../../files/task_files/dsc.jpg',
'hope.pdf' => '../../../files/task_files/hope.pdf'
);
It's not working exactly. Connected with question: Stackoverflow question file.zip will be invalid. I couldn't find what the problem is.
I changed my point of view and write below code. But still get the error "ZipArchive::addFile(): Invalid or uninitialized Zip object". puff!!
$zipname = 'task.zip';
$zip = new ZipArchive;
$zip->open($zipname, ZipArchive::OVERWRITE);
$conn_msg_attach = mysql_query("SELECT * FROM task_msg_attach WHERE msg_ID = '$msgID'");
while($get_msg_attach = mysql_fetch_array($conn_msg_attach)) {
$msg_attach_path = $get_msg_attach['msg_filepath'];
$msg_attach_fullpath = '../../../files/task_files/'.$msg_attach_path;
$zip->addFile($msg_attach_fullpath, $msg_attach_path);
}
$zip->close();
Thank you for any help.
FINAL SOLUTION FOR THE QUESTION:
$msgID = $_GET['msgID'];
$zipname = 'task.zip';
$zip = new ZipArchive;
$zip->open($zipname, ZipArchive::CREATE | ZipArchive::OVERWRITE);
$conn_msg_attach = mysql_query("SELECT * FROM task_msg_attach WHERE msg_ID = '$msgID'");
while($get_msg_attach = mysql_fetch_array($conn_msg_attach)) {
$msg_attach_path = $get_msg_attach['msg_filepath'];
$msg_attach_fullpath = '../../../files/task_files/'.$msg_attach_path;
$zip->addFile($msg_attach_fullpath, $msg_attach_path);
}
$zip->close();
header('Content-Description: File Transfer');
header('Content-Type: application/zip');
header('Content-disposition: attachment; filename='.$zipname);
header('Expires: 0');
header('Cache-Control: must-revalidate');
header('Pragma: public');
header('Content-Length: ' . filesize($zipname));
ob_clean();
readfile($zipname);
ob_flush();