2017-02-27 40 views
-2

我正在處理內部應用程序,用戶上傳一些圖像,使用它們並最終將所有文件作爲包下載。PHP - 將textarea內容添加爲zip作爲html

我是一個真正的PHP初學者,因此找到了一個創建上傳圖片的zip代碼。

<?php 

$dir = 'uploads/'; 
$zip_file = 'file.zip'; 

// Get real path for our folder 
$rootPath = realpath($dir); 

// Initialize archive object 
$zip = new ZipArchive(); 
$zip->open($zip_file, ZipArchive::CREATE | ZipArchive::OVERWRITE); 

// Create recursive directory iterator 
/** @var SplFileInfo[] $files */ 
$files = new RecursiveIteratorIterator(
    new RecursiveDirectoryIterator($rootPath), 
    RecursiveIteratorIterator::LEAVES_ONLY 
); 

foreach ($files as $name => $file) 
{ 
    // Skip directories (they would be added automatically) 
    if (!$file->isDir()) 
    { 
     // Get real and relative path for current file 
     $filePath = $file->getRealPath(); 
     $relativePath = substr($filePath, strlen($rootPath) + 1); 

     // Add current file to archive 
     $zip->addFile($filePath, $relativePath); 
    } 
} 

// Zip archive will be created only after closing object 
$zip->close(); 


header('Content-Description: File Transfer'); 
header('Content-Type: application/octet-stream'); 
header('Content-Disposition: attachment; filename='.basename($zip_file)); 
header('Content-Transfer-Encoding: binary'); 
header('Expires: 0'); 
header('Cache-Control: must-revalidate'); 
header('Pragma: public'); 
header('Content-Length: ' . filesize($zip_file)); 
readfile($zip_file); 

?> 

現在有一個textarea有一個代碼。

我想將該textarea的內容添加爲zip壓縮爲html。

+0

那麼是什麼阻止你創建一個html文件? – charlietfl

回答

0

您應該將文本文件的內容另存爲$dir = 'uploads/';中的.HTML文件。爲此,您可以使用file_put_contents方法(請記住正確的文件路徑)。 ZIP創建後,您可以刪除此文件。

+0

感謝您的回覆,實際上我通過一些研究來整理這些內容。我用addFromString()。現在我想要做的是在我的zip文件夾中創建一個名爲assets的文件夾(它將包含所有圖像),並且它已經包含index.html。 file.zip - assets img1.jpg index.html –