在php中创建zip文件夹

时间:2013-10-25 21:50:56

标签: ftp zip

我正在尝试为下载的图片创建一个zip文件夹。 这是我的代码。我没有得到任何错误,但zip没有下载。代码正在编译,我得到输出,直到当前目录的显示部分,但在那之后代码似乎出错在某处,我无法获得任何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);
}

?>

1 个答案:

答案 0 :(得分: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方法正在工作并压缩我的文件,而我循环文件的常规不是!!