php - Download photos in zip file -


hi guys , can give. here in code in uploading img mysql data base using form,this data base.

database

create table `user_pic` (     `id` int(11) unsigned not null auto_increment,     `img` longblob not null,     `img_name` varchar(200) not null,     `upload_date` datetime not null default current_timestamp,     `user_id` int(11) null default null,     primary key (`id`) ) collate='latin1_swedish_ci' engine=innodb 

here load photos in variable

load_img.php

$couple_login_id = $_session['users']['id']; $statement = $conexion->prepare("select * user_pic user_id = $couple_login_id"); $statement->execute(); $fotos = $statement->fetchall(); 

an here loop images , display images on html

gallery.php

    <?php foreach($fotos $foto):?>       <div class="col-xs-12 col-sm-4 col-md-3 item-photo">         <div class="photo">           <?php echo '<img class="img-responsive" src="data:image/jpeg;base64,'.base64_encode( $foto['img'] ).'"/>';?>           <?php echo '<a class="search zoom fancybox" href="data:image/jpeg;base64,'.base64_encode( $foto['img'] ).'"><span class="icon-search"></span></a>';?>           <?php if($_session['users']["user_rol"] == 1):?>             <a class="star" href="#"><span class="icon-star"></span></a>           <?php endif;?>           <a class="download" href="#"><span class="icon-download"></span></a>         </div>       </div>     <?php endforeach;?> 

this code download photo on zip file. download_zip.php

  $zip = new ziparchive();    # create temp file & open   $tmp_file = tempnam('.','');   $zip->open($tmp_file, ziparchive::create);    # loop through each file   foreach($fotos $file){        # download file       $download_file = file_get_contents($file['img']);        #add zip       $zip->addfile($download_file);    }    # close zip   $zip->close();        # send file browser download      header("content-type: application/zip");       header("content-disposition: attachment; filename=wedding_photos.zip");      header("content-length: " . filesize('wedding_photos.zip'));      header("pragma: no-cache");       header("expires: 0");       readfile('wedding_photos.zip'); 

the problemis when create zip file , download it, can open because sed file has unknow format or damaged. don´t if there wrong code or because the way photos stored on data base. can give me. :d

when you're using binary data instead of files should use addfromstring method. can change code this:

$zip = new ziparchive();  // create temp file & open $tmp_file = tempnam('.', ''); $zip->open($tmp_file, ziparchive::create);  // loop through each file foreach ($fotos $file) {     //add zip     $zip->addfromstring($file['img_name'], $file['img']); }  // close zip $zip->close(); 

Comments

Popular posts from this blog

javascript - Clear button on addentry page doesn't work -

c# - Selenium Authentication Popup preventing driver close or quit -

tensorflow when input_data MNIST_data , zlib.error: Error -3 while decompressing: invalid block type -