示例#1
0
/**
 * Add a folder to a zip file
 *
 * @param ZipArchive &$zip_archive the zip file to add files/folder to
 * @param ElggObject $folder       the folder to add
 * @param string     $folder_path  the path of the current folder
 *
 * @return void
 */
function file_tools_add_folder_to_zip(ZipArchive &$zip_archive, ElggObject $folder, $folder_path = "")
{
    if (!empty($zip_archive) && !empty($folder) && elgg_instanceof($folder, "object", FILE_TOOLS_SUBTYPE)) {
        $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" => FILE_TOOLS_RELATIONSHIP, "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 . $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 . $file_name);
                }
            }
        }
        // check if there are subfolders
        $folder_options = array("type" => "object", "subtype" => FILE_TOOLS_SUBTYPE, "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) {
                file_tools_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)) {
            file_tools_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);
示例#3
0
/**
 * Add a folder to a zip file
 *
 * @param ZipArchive &$zip_archive the zip file to add files/folder to
 * @param ElggObject $folder       the folder to add
 * @param string     $folder_path  the path of the current folder
 *
 * @return void
 */
function file_tools_add_folder_to_zip(ZipArchive &$zip_archive, ElggObject $folder, $folder_path = '')
{
    if (!$zip_archive instanceof ZipArchive || !elgg_instanceof($folder, 'object', FILE_TOOLS_SUBTYPE)) {
        return;
    }
    $folder_title = elgg_get_friendly_title($folder->title);
    $zip_archive->addEmptyDir($folder_path . $folder_title);
    $folder_path .= $folder_title . DIRECTORY_SEPARATOR;
    $file_options = ['type' => 'object', 'subtype' => 'file', 'limit' => false, 'relationship' => FILE_TOOLS_RELATIONSHIP, 'relationship_guid' => $folder->getGUID()];
    // add files from this folder to the zip
    $files = new ElggBatch('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 . $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 . $file_name);
        }
    }
    // check if there are subfolders
    $folder_options = ['type' => 'object', 'subtype' => FILE_TOOLS_SUBTYPE, 'limit' => false, 'metadata_name_value_pairs' => ['parent_guid' => $folder->getGUID()]];
    $sub_folders = new ElggBatch('elgg_get_entities_from_metadata', $folder_options);
    foreach ($sub_folders as $sub_folder) {
        file_tools_add_folder_to_zip($zip_archive, $sub_folder, $folder_path);
    }
}