Пример #1
0
function pleiofile_add_folder_to_zip(ZipArchive &$zip_archive, ElggObject $folder, $folder_path = "")
{
    if (!empty($zip_archive) && !empty($folder) && elgg_instanceof($folder, "object", "folder")) {
        $folder_title = elgg_get_friendly_title($folder->title);
        $zip_archive->addEmptyDir($folder_path . $folder_title);
        $folder_path .= $folder_title . DIRECTORY_SEPARATOR;
        $file_options = array("type" => "object", "subtype" => "file", "limit" => false, "relationship" => "folder_of", "relationship_guid" => $folder->getGUID());
        // add files from this folder to the zip
        if ($files = elgg_get_entities_from_relationship($file_options)) {
            foreach ($files as $file) {
                // check if the file exists
                if ($zip_archive->statName($folder_path . $file->originalfilename) === false) {
                    // doesn't exist, so add
                    $zip_archive->addFile($file->getFilenameOnFilestore(), $folder_path . sanitize_file_name($file->originalfilename));
                } else {
                    // file name exists, so create a new one
                    $ext_pos = strrpos($file->originalfilename, ".");
                    $file_name = substr($file->originalfilename, 0, $ext_pos) . "_" . $file->getGUID() . substr($file->originalfilename, $ext_pos);
                    $zip_archive->addFile($file->getFilenameOnFilestore(), $folder_path . sanitize_file_name($file_name));
                }
            }
        }
        // check if there are subfolders
        $folder_options = array("type" => "object", "subtype" => "folder", "limit" => false, "metadata_name_value_pairs" => array("parent_guid" => $folder->getGUID()));
        if ($sub_folders = elgg_get_entities_from_metadata($folder_options)) {
            foreach ($sub_folders as $sub_folder) {
                pleiofile_add_folder_to_zip($zip_archive, $sub_folder, $folder_path);
            }
        }
    }
}
Пример #2
0
                 // doesn't exist, so add
                 $zip->addFile($file->getFilenameOnFilestore(), $file->originalfilename);
             } else {
                 // file name exists, so create a new one
                 $ext_pos = strrpos($file->originalfilename, ".");
                 $file_name = substr($file->originalfilename, 0, $ext_pos) . "_" . $file->getGUID() . substr($file->originalfilename, $ext_pos);
                 $zip->addFile($file->getFilenameOnFilestore(), $file_name);
             }
         }
     }
 }
 // add folder (and their content) to the zip
 if (!empty($folder_guids)) {
     foreach ($folder_guids as $folder_guid) {
         if ($folder = get_entity($folder_guid)) {
             pleiofile_add_folder_to_zip($zip, $folder);
         }
     }
 }
 // done adding content, so save the zip
 $zip->close();
 if (file_exists($zip_filename)) {
     // output the correct headers
     header('Pragma: public');
     header('Content-type: application/zip');
     header('Content-Disposition: attachment; filename="folder_contents.zip"');
     header('Content-Length: ' . filesize($zip_filename));
     ob_clean();
     flush();
     readfile($zip_filename);
     unlink($zip_filename);