1

我正在尝试为我下载的图像创建一个 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);
}

?>
4

1 回答 1

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 方法正在工作并压缩我的文件,而我的正常循环文件却不是!

于 2013-10-28T21:06:36.023 回答