2016-06-15 8 views
0

私はこのスクリプトを使ってすべてのファイルをデータベースからダウンロードしますが、データベースに保存された最初のファイルのみを取得します。zipとしてすべてのファイルを圧縮するPHPコード

以下

私はzipファイルに含めたい私は、データベースにあるファイルのリストです:

lginin 

logersutil.php 

lgininh.js 

Readme.md 

そして、私のコード:あなたの現在のコードで

<?php 
if(isset($_POST['download'])){ 
try{ 
$db_conn = new PDO('mysql:host='.DB_HOST.';dbname='.DB_NAME,DB_USERNAME,DB_PASSWORD); 
$db_conn->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION); 
$post_stmt = $db_conn->prepare("SELECT * FROM jailorgchild WHERE jailchildbasname = :BasNameJa"); 
$post_stmt->bindParam(':BasNameJa', basename(dirname(__FILE__))); 
$post_stmt->execute(); 
while ($rowss = $post_stmt->fetch(PDO::FETCH_ASSOC)){ 

    $files = array(''.$rowss["jailchillink"].'','codejail.cj'); 

    # create new zip opbject 
    $zip = new ZipArchive(); 

    # create a temp file & open it 
    $tmp_file = tempnam('.',''); 
    $zip->open($tmp_file, ZipArchive::CREATE); 

    # loop through each file 
    foreach($files as $file){ 

     # download file 
     $download_file = file_get_contents($file); 

     #add it to the zip 
     $zip->addFromString(basename($file),$download_file); 

    } 

    # close zip 
    $zip->close(); 

    # send the file to the browser as a download 
    header('Content-disposition: attachment; filename='.basename(dirname(__FILE__)).'.zip'); 
    header('Content-type: application/zip'); 
    readfile($tmp_file); 

} }catch (PDOException $e){ echo 'Connection failed: ' . $e->getMessage();} 
} 
?> 

答えて

0

、あなたは基本的に1つの新しいzipファイルを作り、それを各行に返す。クライアントは1つの応答しか読み取ることができないため、明らかに1行以上は表示されません。

あなたがしなければならないことは、にすべてのファイルを追加してから、を送信することです。これと同じように:?PDO :: __構築物():

<?php 
if(isset($_POST['download'])){ 
    try{ 
     $db_conn = new PDO('mysql:host='.DB_HOST.';dbname='.DB_NAME,DB_USERNAME,DB_PASSWORD); 
     $db_conn->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION); 
     $post_stmt = $db_conn->prepare("SELECT * FROM jailorgchild WHERE jailchildbasname = :BasNameJa"); 
     $post_stmt->bindParam(':BasNameJa', basename(dirname(__FILE__))); 
     $post_stmt->execute(); 

     // here we create a temporary array to hold all the filenames 
     // and fill it with the file you always want to have inserted 
     $files = array('codejail.cj'); 
     while ($rowss = $post_stmt->fetch(PDO::FETCH_ASSOC)){ 
      // now fill the array with your files 
      $files[] = $rowss['jailchillink']; 
     }   

     // and now we finally start processing them all, after having finished reading from the DB 
     # create new zip opbject 
     $zip = new ZipArchive(); 

     # create a temp file & open it 
     $tmp_file = tempnam('.',''); 
     $zip->open($tmp_file, ZipArchive::CREATE); 

     # loop through each file 
     foreach($files as $file){ 
      # download file 
      $download_file = file_get_contents($file); 

      #add it to the zip 
      $zip->addFromString(basename($file),$download_file); 
     } 

     # close zip 
     $zip->close(); 

     # send the file to the browser as a download 
     header('Content-disposition: attachment; filename='.basename(dirname(__FILE__)).'.zip'); 
     header('Content-type: application/zip'); 
     readfile($tmp_file); 

    } catch (PDOException $e){ 
     echo 'Connection failed: ' . $e->getMessage(); 
    } 

} >

+0

私は '警告このエラーメッセージが表示されますphp_network_getaddresses:失敗のgetaddrinfo:そのようなホストは知られていません。接続に失敗しました:SQLSTATE [HY000] [2002] php_network_getaddresses:getaddrinfo failed:このようなホストは認識されていません。 ' – Frank

+0

あなたの答えは助けてくれましたが、データベースからファイルを取り出すのに間違いがあります。 – Frank

+0

db-hostセクションには微妙な入力ミスがあります。あなたのdb接続コードが正確であると仮定していました。 あなたの提案された編集はまさに私があなたに*行うべきではないことをあなたに伝えたいものです。 zipパーツをdatabase-fetchセクションから削除する必要があります。最初にすべての情報を取得し、それを処理します。 – Tularis

関連する問題