Link to home
Start Free TrialLog in
Avatar of InspireTech
InspireTech

asked on

Extracting all binary files from MySQL at one stroke!

Hi...All

I have been trying so long to extract all the binary files from MySQL database at one shot. I have this MySQL database that contains binary files inside BLOB data field. What I want to do is I want to retrieve all the files into my harddisk at one shot.

It can be in zip format or I specify a folder path then all the files are extracted to that folder. Can anybody tell me how to do this? I have been trying to figure out so long but still can't get the answer yet.

I tried to do looping after selecting all the files from database. the example is below. But after appearing the first download dialog box. The loop stops. Please suggest me how to slove the problem.

<?

     //Establish connection with database
     require_once("includes/db_connect.php");    
     
      $conn = db_connect();
 
     if (!$conn){
             echo "Could not connect to database server - please try later.";    
          exit;
     }
     
     $sql = "SELECT File,FileName,FileSize,FileType FROM File";
     
     $result = mysql_query($sql);
     
     if(!$result){
          echo "Error in retrieving files";
          exit;
     }
     
     //echo mysql_num_rows($result);
     //exit;
     
     for($i=0;$i < mysql_num_rows($result);$i++) {
     
     $data = mysql_result($result,0,"File");
     $name = mysql_result($result,0,"FileName");
     $size = mysql_result($result,0,"FileSize");
     $type = mysql_result($result,0,"FileType");


     header("Content-type: $type");
     header("Content-length: $size");
     header("Content-Disposition: attachment; filename= $name");
     header("Content-Description: PHP Generated Data");
     echo $data;
     
     }


?>

Thanks all.
ASKER CERTIFIED SOLUTION
Avatar of VGR
VGR

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Avatar of InspireTech
InspireTech

ASKER

Hi VGR,

I am passionately thank you for giving me such an excellent answer to my quesion. Now I have finally solved the problem that I have been trying so long.Now,I do understand the usefulness of using forum.

Your answer did terrific help to me. I really thank you from the bottom of my heart. I am sure that your proficieny in PHP is far deep beyond more than others do. I am just a novice to PHP.

Thanks a lot
InspireTech
i've no merit
I've no extensive knowledge of PHP either
I only try to keep things simple and as efficient to run as they're efficient to write down 8-)
Hi..

I have another problem when I test this on server. What I want to do is that at first I will ask the user where he wants to write all the binary files to. For example: he will say C:/Test/

Then at the next page I wish to write all the binary files to previously specified directory (C:/Test/). But the server doesn't seems to know where is C:/Test/. So I could not any file.

Do you have any idea what should I do to solve this?
ok

the most probable trick is the slashes problem on your windows platform. Try either with forward slashes ('/Test') if the directory is under your DOCUMENT_ROOT (recommended) or with double-backwards slashes ('C:\\Test') - this should work.

For the user intput, you can use two techniques :
-either INPUT TYPE=text (I suppose that's what you have, thus the slashes problem)
-or a INPUT TYPE=file where people will have to select at least a file in the target directory. Then your script will take out the basename() to have the directory part. That's your target directory