2013-07-06 53 views
0

可以說我有一個名爲和實例變量:從字符串到新的文件夾添加文件ZipArchive

$css = 'body {color:red;}';

目前,我建立我的zip文件如下:

ini_set("max_execution_time", 300); 
    // create object 
    $zip = new ZipArchive(); 
    // open archive 
    if ($zip->open("my-archive.zip", ZIPARCHIVE::CREATE) !== TRUE) { 
     die ("Could not open archive"); 
    } 
    // initialize an iterator 
    // pass it the directory to be processed 
    $jsFiles = new RecursiveIteratorIterator(new RecursiveDirectoryIterator("../js")); 
    $cssFiles = new RecursiveIteratorIterator(new RecursiveDirectoryIterator("../css")); 
    $images = new RecursiveIteratorIterator(new RecursiveDirectoryIterator("../_design")); 
    // iterate over the directory 
    // add each file found to the archive 
    foreach ($jsFiles as $key=>$value) { 
     $zip->addFile(realpath($key), $key) or die ("ERROR: Could not add file: $key"); 
    } 
    foreach ($cssFiles as $key=>$value) { 
     $zip->addFile(realpath($key), $key) or die ("ERROR: Could not add file: $key"); 
    } 
    foreach ($images as $key=>$value) { 
     $zip->addFile(realpath($key), $key) or die ("ERROR: Could not add file: $key"); 
    } 
    // close and save archive 
    $zip->close(); 
    echo "Archive created successfully."; 

讓說我已經有一個名爲color.css的文件位於/css/main/color.css/,我希望我的新字符串$css替換這個整個文件在檔案,我該怎麼做?我看過ZipArchive::addFromString,但是我看不到一種方法將它放在某個文件夾中。這是唯一需要在整個目錄中進行更改的文件。

任何幫助將是偉大的。

回答

0

如果你看一下http://www.php.net/manual/en/ziparchive.addfromstring.php的例子#2,你會發現ZipArchive :: addFromString的第一個參數實際上可以是你的zip文件中的任何路徑。

因此,在你的情況下,調用看起來就像這樣:

$zip->addFromString("css/main/color.css",$css); 
+0

啊,當然,嘗試有病啊! –

相關問題