2013-10-25 157 views
1

我正在嘗試創建我下載的圖像的zip文件夾。 這是我的代碼。我沒有收到任何錯誤,但zip沒有得到下載。代碼正在編譯,我得到的輸出直到當前目錄的顯示部分,但在此之後,代碼似乎出了問題的地方,我不能得到任何Zip存檔。在php中創建zip文件夾

<?php 
$conn_id=ftp_connect("localhost") or die("Could not connect"); 
ftp_login($conn_id,"kishan","ubuntu");  //login to ftp localhost 
echo "The current directory is " . ftp_pwd($conn_id); //display current directory 

ftp_chdir($conn_id,'/var/www/test1'); //changing to the directory where my images are downloaded. 
echo "<br/><p> Changing to directory" . ftp_pwd($conn_id); 
$file_folder="."; 

echo "<br/> The content of the directory is <br/>"; 
print_r(ftp_rawlist($conn_id,".")); // display the contents of the directory 

if(extension_loaded('zip'))  //check whether extension is loaded 
{ 
$zip=new ZipArchive(); 
$zip_name="a.zip";  //some name to my zip file 

if($zip->open($zip_name,ZIPARCHIVE::CREATE)!==TRUE) 
    { 
    $error="Sorry ZIP creation failed at this time"; 
    } 

$contents=ftp_nlist($conn_id,"."); 

foreach($contents as $file) //addition of files to zip one-by-one 
    { 
    $zip->addFile($file_folder.$file); 
    } 
$zip->close();  //seal the zip 
} 

if(file_exists($zip_name)) //Content-dispostion of my zip file 
{ 
header('Content-type:application/zip'); 
header('Content-Disposition:attachment; filename="'.$zip_name.'"'); 
readfile($zip_name); 
unlink($zip_name); 
} 

?> 

回答

0

我剛剛發現傳遞給for循環的數據有問題。

$contents=ftp_nlist($conn_id,"."); 
foreach($contents as $file) //addition of files to zip one-by-one 
{ 
$zip->addFile($file_folder.$file); 
} 

我不確定究竟哪裏出問題了,但這是更新。使用HTML然後將文件傳遞到通過POST方法這個PHP腳本,如

foreach($post['files'] as $file){    
$zip->addFile($file_folder.$file);} // Adding files into zip 

此代碼是完全工作正常,我所做的就是寫標記,但現在我需要弄清楚爲什麼POST方法工作,壓縮我的文件,而我的正常循環文件不是!