簡體   English   中英

內拉鏈(PHP)

[英]Zip inside zip (php)

大家好,我已經嘗試了幾天而沒有任何回應,我對php並不滿意,但是我正在努力。

我想自動下載,而不在服務器上保存名為Bigzip的zip文件

在此Bigzip內部:-另一個名為Smallzip的拉鏈

但是打開下載的zip時出現錯誤,它已損壞

<?php

//Big and Small Archives names
$BzipN='Bigzip.zip';
$SzipN='Smallzip.zip';

//Big and Small Archives
$Bzip = new ZipArchive();
$Szip = new ZipArchive();




//File path
$file_path=$_SERVER['DOCUMENT_ROOT'].'/PF/';




zipFilesAndDownload($BzipN,$SzipN,$Bzip,$Szip,$file_path);

function zipFilesAndDownload($BzipN,$SzipN,$Bzip,$Szip,$file_path)
{


//create the file and throw the error if unsuccessful
if ($Bzip->open($BzipN, ZIPARCHIVE::CREATE )!==TRUE) {
exit("cannot open <$BzipN>\n");
}
if ($Szip->open($SzipN, ZIPARCHIVE::CREATE )!==TRUE) {
exit("cannot open <$SzipN>\n");
}



//Add Smallzip to BigZip
$Bzip->addFile($file_path.$SzipN,$Szip);


$Szip->close();
$Bzip->close();
//then send the headers to foce download the Big zip file
header("Content-type: application/zip"); 
header("Content-Disposition: attachment; filename=$BzipN");
header("Content-length: " . filesize($BzipN));
header("Pragma: no-cache"); 
header("Expires: 0"); 
readfile("$BzipN");
exit;
}
?>

如果您有其他選擇,想法和建議,我將很樂意接受。

謝謝

問題編號1:
您無法在空的zip檔案中創建空的zip檔案。 這將導致文件損壞。

問題編號2:
在您尚未關閉第一個zip存檔的情況下,請勿嘗試將其添加到另一個zip存檔。

問題編號3:
$Bzip->addFile($file_path.$SzipN,$Szip); 那么,為什么要嘗試將對象設置為文件名呢? => $Szip = new ZipArchive();

解:

<?php

//Big and Small Archives names
$BzipN='Bigzip.zip';
$SzipN='Smallzip.zip';

//Big and Small Archives
$Bzip = new ZipArchive();
$Szip = new ZipArchive();

//File path
$file_path=$_SERVER['DOCUMENT_ROOT'].'/PF/';


function zipFilesAndDownload($BzipN,$SzipN,$Bzip,$Szip,$file_path){
    // Create the file Smallzip.zip and throw the error if unsuccessful
    if ($Szip->open($SzipN, ZIPARCHIVE::CREATE )!==TRUE) {
        exit("cannot open <$SzipN>\n");
    }

    // Add a txt file to Smallzip so it isn't empty
    $Szip->addFromString("testfilephp.txt", "#1 This is a test string added as testfilephp.txt.\n");

    // Close Smallzip.zip as we're done with it
    $Szip->close();

    // Create the file Bigzip.zip and throw the error if unsuccessful
    if ($Bzip->open($BzipN, ZIPARCHIVE::CREATE )!==TRUE) {
        exit("cannot open <$BzipN>\n");
    }

    // Add Smallzip.zip to Bigzip.zip with a valid name
    $Bzip->addFile($file_path.$SzipN,$SzipN);

    // Close Bigzip.zip as we're done with it
    $Bzip->close();

    //then send the headers to foce download the Big zip file
    header("Content-type: application/zip"); 
    header("Content-Disposition: attachment; filename=$BzipN");
    header("Content-length: " . filesize($BzipN));
    header("Pragma: no-cache"); 
    header("Expires: 0"); 
    readfile("$BzipN");

    // Delete the files from the server, even if the user cancels the download
    ignore_user_abort(true);
    unlink($file_path.$SzipN);
    unlink($file_path.$SzipN);
    exit;
}

zipFilesAndDownload($BzipN,$SzipN,$Bzip,$Szip,$file_path);

?>

如果您不想在服務器上保存文件,只需回顯創建的文件。 的index.php

 <form action="test.php" method="post"> To file: <input type="text" name="tofile" /> <input type="submit" /> </form> 

test.php的

 <?php $filename = 'test-download.zip'; $htmlcode1 = "<HTML> \\n <BODY>"; $htmlcode2 = "</BODY> \\n <HTML>"; $somecontent = $htmlcode1.$_POST["tofile"].$htmlcode2; !$handle = fopen($filename, 'w'); fwrite($handle, $somecontent); fclose($handle); header("Cache-Control: public"); header("Content-Description: File Transfer"); header("Content-Length: ". filesize("$filename").";"); header("Content-Disposition: attachment; filename=$filename"); header("Content-Type: application/octet-stream; "); header("Content-Transfer-Encoding: binary"); readfile($filename); ?> 

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM