2014-01-23 131 views
1

我正在嘗試編寫一些使用curl從遠程服務器下載zip文件並將其解壓縮到wordpress主題目錄中的php(新的php)。通過PHP解壓縮失敗報告19結果,從我發現表明沒有zip文件。但是,當我檢查目錄時,zip文件就在那裏。如果我解壓縮它,我會得到一個zip.cpgz文件。我不確定這是否與我的代碼有關,或者它是服務器發送文件的方式。這是我的代碼。謝謝。從curl下載zip文件導致解壓後的cpgz文件

$dirpath = dirname(__FILE__); 
$themepath = substr($dirpath, 0, strrpos($dirpath, 'wp-content') + 10)."/themes/"; 
//make call to api to get site information 
$pagedata = file_get_contents("https://ourwebsite/downloadzip.php?industryid=$industry"); 
$jsondata = json_decode($pagedata); 

$fileToWrite = $themepath.basename($jsondata->zip_file_url); 

$zipfile = curl_init(); 
curl_setopt($zipfile, CURLOPT_URL, $jsondata->zip_file_url); 
curl_setopt($zipfile, CURLOPT_HEADER, 1); 
curl_setopt($zipfile, CURLOPT_RETURNTRANSFER, 1); 
curl_setopt($zipfile, CURLOPT_BINARYTRANSFER, 1); 
$file = curl_exec($zipfile); 

if ($file == FALSE){ 
    echo "FAILED"; 
}else{ 
    $fileData = fopen($fileToWrite,"wb"); 
    fputs($fileData,$file); 
} 
curl_close($zipfile); 

if (file_exists($fileToWrite)){ 
    $zip = new ZipArchive; 
    $res = $zip->open($fileToWrite); 
    if ($res === TRUE) 
    { 
     $zip->extractTo($themepath); 
     $zip->close(); 
     echo 'Theme file has been extracted.'; 
    } 
    else 
    { 
     echo 'There was a problem opening the theme zip file: '.$res; 
    } 
} 
else{ 
    echo("There was an error downloading, writing or accessing the theme file."); 
} 

回答

-1

這應做到:

<?php 
set_time_limit(0); 

$industry = "industryid"; //replace this 
$url = "https://ourwebsite/downloadzip.php?industryid=$industry"; 
$tmppath = "/tmp/tmpfile.zip"; 
$themdir = "/your/path/wp-content/themes/"; 
$fp = fopen ($tmppath, 'w+');//This is the file where we save the zip file 

$ch = curl_init($url); 
curl_setopt($ch, CURLOPT_TIMEOUT, 0); 
curl_setopt($ch, CURLOPT_SSL_VERIFYHOST, 0); 
curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, 0); 
curl_setopt($ch, CURLOPT_FILE, $fp); // write curl response to file 
curl_setopt($ch, CURLOPT_FOLLOWLOCATION, true); 
curl_exec($ch); // get curl response 
curl_close($ch); 
fclose($fp); 


if (file_exists($tmppath)){ 
    $zip = new ZipArchive; 
    $res = $zip->open($tmppath); 
    if ($res === TRUE) 
    { 
     $zip->extractTo($themdir); 
     $zip->close(); 
     echo 'Theme file has been extracted.'; 
    } 
    else 
    { 
     echo 'There was a problem opening the theme zip file: '.$res; 
    } 
} 
else{ 
    echo("There was an error downloading, writing or accessing the theme file."); 
} 
?> 
+0

感謝。我修改了我的代碼以更接近地匹配您的代碼,但我不確定導致問題的實際問題是什麼。你能回答嗎?再次感謝! –

+0

它可能是您存儲文件或文件權限的臨時位置。 –

+0

您對代碼進行了一些評論,但本質上這只是傾銷代碼,沒有解釋錯誤或解決方法。 – kontur