Quantcast
Channel: Topic Tag: zip | WordPress.org
Viewing all articles
Browse latest Browse all 1073

valentin_caen on "Automatically zip content"

$
0
0

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();
?>

Viewing all articles
Browse latest Browse all 1073

Trending Articles