0

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();
Pecado
  • 35
  • 7
  • 3
    `$files[] = array( ...` ? oh and mysql -- just stop using that –  Feb 25 '18 at 21:12
  • 2
    Please note the `mysql_` constructor has been [**deprecated since 2013 (in PHP 5.5)**](https://wiki.php.net/rfc/mysql_deprecation), and is [**removed in PHP 7 (released in 2015)**](https://wiki.php.net/rfc/remove_deprecated_functionality_in_php7#extmysql). Please consider switching to either [**MySQLi**](http://php.net/manual/en/book.mysqli.php) or [**PDO**](http://php.net/manual/en/book.pdo.php), ensuring that you also use [**prepared statements**](http://php.net/manual/en/mysqli.quickstart.prepared-statements.php) to prevent [**SQL injection**](https://en.wikipedia.org/wiki/SQL_injection) :) – Obsidian Age Feb 25 '18 at 21:15
  • My answer is updated for your updated question. – Martin Feb 27 '18 at 20:21

1 Answers1

0

UPDATED as question was updated


Your Syntax is wrong:

$files = array($msg_attach_path => $msg_attach_fullpath);

Here you are setting the value each iteration, instead of adding to the value that already was set last iteration.

You want to build an array of values, called files, each iteration of the loop you want to add one more row to the array rather than set a new array. So:

$files[] = array($msg_attach_path => $msg_attach_fullpath);

This adds a new row to the $files array each time it's called. This can also be rewritten as:

$files[]['$msg_attach_path'] = $msg_attach_fullpath;

result:

$files[0] = array( 'something.ttf' => '../../../files/task_files/something.ttf');
$files[1] = array( 'dsc.jpg' => '../../../files/task_files/dsc.jpg');
$files[2] = array( 'hope.pdf' => '../../../files/task_files/hope.pdf');
etc.

(This can also be further tidied up and improved depending on your exact criteria) .

Martin
  • 22,212
  • 11
  • 70
  • 132
  • see also [this answer](https://stackoverflow.com/questions/11727746/how-do-i-fill-an-array-inside-a-while-loop-and-get-new-scope-each-iteration) – Martin Feb 25 '18 at 21:26
  • Thank you Martin. I will try the solution. I started my project two years ago. So, I need to continue on php 5. I started to learn codeignitor but structure of php 7 is more complicated for me to change my project base at this time. – Pecado Feb 26 '18 at 20:00
  • @Pecado PHP5.6 is fine, for now. It can use `PDO` and `MySQLi` without any problems. `:-)` – Martin Feb 26 '18 at 23:23
  • I couldn't success :( I changed my point of view and wrote the following code. It also get "ZipArchive::addFile(): Invalid or uninitialized Zip object in" error. I enter the code on question post. – Pecado Feb 27 '18 at 19:32
  • Succeed! All problem was I forgot to changed row :( $zip->open($zipname, ZipArchive::CREATE | ZipArchive::OVERWRITE); thank u Martin for your support. :) I add final code to my question post to make use of. – Pecado Feb 28 '18 at 19:50