文件未添加到ZIP

时间:2014-05-13 06:45:54

标签: php ftp

开发一个代码,用于从文件夹中选择的文件创建ZIP文件。该文件夹是使用sessiontime作为名称创建的,但文件未添加到ZIP文件中。文件的权限是777.文件被选中,由下面的输出指示。我还通过替换行$ zip-> addFile($ file_folder)来检查代码;它下载了一个空白的zip存档,因此它没有问题。

<?php
ob_start();
$error = "";        //error holder
if(isset($_POST['createpdf']))
{
    $post = $_POST;     
    $file_folder = "test/".$_SESSION["dir_name"]."/";   // folder to load files
    echo $file_folder;
    if(extension_loaded('zip'))

    {   // Checking ZIP extension is available
        echo "checking";
        if(isset($post['files']) and count($post['files']) > 0)
        {   echo "/checking2";// Checking files are selected
            $zip = new ZipArchive();            // Load zip library 
            $zip_name = time().".zip";          // Zip name

            if($zip->open($zip_name, ZIPARCHIVE::CREATE)!==TRUE)
            {   
                // Opening zip file to load files
                echo "* Sorry ZIP creation failed at this time<br/>";
            }
            foreach($post['files'] as $file)
            {   echo "added";           
                $zip->addFile($file_folder.$file);          // Adding files 
into zip
            }
            $zip->close();
            if(file_exists($zip_name))
            {
                // push to download the zip
                header('Content-type: application/zip');
                header('Content-Disposition: attachment; filename="'.$zip_name.'"');
                readfile($zip_name);
                // remove zip file is exists in temp path
                unlink($zip_name);

我得到的输出是行:test // checking / checking2addedaddedadded

1 个答案:

答案 0 :(得分:0)

打开session_start()代码后忘记了<?php,因此$_SESSION['dir_name']变量为空