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?

9
  • 1
    $data["id"] and $data["base64_str"] put the column name inside double quote Commented Jun 11, 2015 at 11:51
  • 1
    you can download one file only Commented Jun 11, 2015 at 11:53
  • think again fpj you are passing header values in loop Commented Jun 11, 2015 at 11:54
  • If you can, you should stop using mysql_* functions. They are no longer maintained and are officially deprecated. Learn about prepared statements instead, and consider using PDO, it's really not hard. Commented Jun 11, 2015 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 Commented Jun 11, 2015 at 12:01

2 Answers 2

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"];
Sign up to request clarification or add additional context in comments.

4 Comments

@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.
@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.
@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.
@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...
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']));
}

1 Comment

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.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.