1

First post:

I have a MySQL database, which is created by this command:

CREATE TABLE IF NOT EXISTSCKox(aTEXT NOT NULL,bTEXT NOT NULL );

Below is its structure:

+----------+-----------------+
|    id    |   base64_str    |
+----------+-----------------+
|  file_1  |  base64_str_1   |
|  file_2  |  base64_str_2   |
|  file_3  |  base64_str_3   |
|  file_4  |  base64_str_4   |
+----------+-----------------+

To save these Base64 strings (in database) to files (on my web server), I have run this PHP code:

<?php

    // database connection
    // ...

    $cmd = mysql_query("SELECT * FROM `MyTable`");
    while($data = mysql_fetch_array($cmd)) {

    $filename = $data[id];
    $filedata = $data[base64_str];

    header('Content-Description: File Transfer');
    header("Content-type: application/octet-stream");
    header("Content-disposition: attachment; filename= ".$filename."");

    echo base64_decode($filedata);

    }

    // ...

?>

It return only one file, and there is no data in this saved file. But, I want to save these Base64 strings to files, on server disk.


After getting some helps from people (on StackOverlow), I have updated my code, it is full:

// database connection
// ...

$cmd = mysql_query("SELECT * FROM `MyTable`");

while ($data = mysql_fetch_array($cmd)) {

file_put_contents($data['id'], base64_decode($data['base64_str']));
echo $data['base64_str'];

}

// ...

But, I still get one file only. What is the main problem? How to solve it?

fpj
  • 315
  • 1
  • 14
  • 1
    $data["id"] and $data["base64_str"] put the column name inside double quote – Goikiu Jun 11 '15 at 11:51
  • 1
    you can *download* one file only – venca Jun 11 '15 at 11:53
  • think again fpj you are passing header values in loop – Ajay Chaudhary Jun 11 '15 at 11:54
  • If you can, you should [stop using `mysql_*` functions](http://stackoverflow.com/questions/12859942/why-shouldnt-i-use-mysql-functions-in-php). They are no longer maintained and are [officially deprecated](https://wiki.php.net/rfc/mysql_deprecation). Learn about [prepared](http://en.wikipedia.org/wiki/Prepared_statement) [statements](http://php.net/manual/en/pdo.prepared-statements.php) instead, and consider using PDO, [it's really not hard](http://jayblanchard.net/demystifying_php_pdo.html). – Jay Blanchard Jun 11 '15 at 11:59
  • in whatever case HTTP response can contain a single header and a single content, hence you can send only one file at a time – Ajay Chaudhary Jun 11 '15 at 12:01
  • in whatever case you cannot send more than one file(content), however you can simulate multiple download by opening multiple windows using JavaScript on client side and each window send a request to server for separate file – Ajay Chaudhary Jun 11 '15 at 12:16
  • You have to first pack all your files to zip, tar... and then serve to download – venca Jun 11 '15 at 12:27
  • ok, on which disk? server disk or local disk? – venca Jun 11 '15 at 12:31
  • @fpj you should have mentioned you wanted to save on server side. What were you trying to accomplish with setting headers and echoing the content? that is for outputting information to the client side, not server side...unclear questions lead to unwanted answers... – Miguel Mesquita Alfaiate Jun 11 '15 at 12:40

2 Answers2

1

you cannot do that file send logic in a while, since you are sending the contents of the several files back to the client, and it will append all the files one after another, into one single file on client side.

You can either get all files, create temporary files in your server, compact them into a tgz, zip file or whatever and then send that back to the user, or you will only be able to send a single file.

This is also wrong:

$filename = $data[id];
$filedata = $data[base64_str];

You need quotes (single or double):

$filename = $data["id"];
$filedata = $data["base64_str"];
Miguel Mesquita Alfaiate
  • 2,851
  • 5
  • 30
  • 56
  • @fpj using fopen will not change anything. You have logic that won't work. You cannot send several files to a user, except if you're compressing them into one. – Miguel Mesquita Alfaiate Jun 11 '15 at 11:59
  • @fpj first off, don't keep changing your text or comments, or the answers of other people will stop making sense. using fopen or fwrite won't magically solve your problem. you can only send one file to a user at a time through the browser. – Miguel Mesquita Alfaiate Jun 11 '15 at 12:24
  • @fpj I don't know if me or the other commenters of your question are speaking another language, so I will try again, using my previous comment: you can only send ONE file to a user through the browser. – Miguel Mesquita Alfaiate Jun 11 '15 at 12:32
  • @fpj it seems like you want server side file handling, which can be accomplished. Since you never mentioned that before, it is hard to guess... – Miguel Mesquita Alfaiate Jun 11 '15 at 12:41
1

For dump files from db to disk use file_put_contents

$cmd = mysql_query("SELECT * FROM `MyTable`");

while ($data = mysql_fetch_array($cmd)) {
    file_put_contents($data['id'], base64_decode($data['base64_str']));
}
venca
  • 1,196
  • 5
  • 18
  • my code dumps all files to server disk. You can download only one file. It is http restriction. You u want download all files, you have to compress them to archive file first. – venca Jun 11 '15 at 13:16