WordPress.org

Ready to get started?Download WordPress

Forums

Automatically zip content (2 posts)

  1. valentin_caen
    Member
    Posted 8 months ago #

    Hi,

    I'm working on an artist site, who needs a press kit.

    I'll use advanced custom fields to let him upload some photos, logos, and pdf.

    That i want is when he upload a new file a script create automatically an updated zip file of his press kit.

    Do you know a plugin or a php script that can help me to do this ?

    Thanks,

    Valentin

  2. valentin_caen
    Member
    Posted 8 months ago #

    So, finally I did it simply with a little bit of PHP

    I past my code here if someone needs to do something similar ;-)

    <?php
    	$file = get_field('file'); // Get files from Advanced custom fields
    	$name_of_zip = get_field('name_of_zip'); // Get name of zip from Advanced custom fields
    
    	$current_path = getcwd(); // get the current path to where the file is located
    	$folder = explode("/", $current_path); // divide the path in parts (aka folders)
    	$blog = $folder[8]; // the blog's folder is the number 8 on the path
    	$root = realpath($_SERVER["DOCUMENT_ROOT"]); // $root = path without the blog installation folder.
    	$path = "$root/$blog"; //Get the path of the blog
    
    	$url = get_home_url()."/"; // Get the blog url plus /
    	$files_to_zip = str_replace($url, $path, $file['url']);// Convert files url to files path 
    
    	$zip = new ZipArchive();
    	$filename = $path."wp-content/uploads/press-kit/".$name_of_zip.".zip"; // Create the zip in wp-content/uploads/press-kit
    	$new_filename = substr($files_to_zip,strrpos($files_to_zip,'/') + 1); // Delete path for the zip
    
    	$zip->open($filename, ZipArchive::CREATE);
    	$zip->addFile($files_to_zip, $new_filename);
    
    	$zip->close();
    ?>

Reply

You must log in to post.

About this Topic