I'm trying to build a directory tree with specific files from an array and push it to download. This part is working fine.
The problem is when I try to fill the specific files with content.
After the download is finished the files are empty.
However the switch case statement is executed and all the methods are working.
The problem I think is around my zip function, but I don't really know what should I search for (or maybe the code design is not the best one).
<?php
include_once 'class.file.php';
class CreateComponent{
function __construct(){
if (isset($_POST['name'])){
$this->createDirectoryTree($_POST['name']);
}
else{
echo '<h1>error!</h1>';
}
}
function createDirectoryTree($component_name){
$component_name="com_".$component_name;
$this->create_zip($component_name);
}
function create_zip($component) {
$jcomp=str_replace("com_", "", $component);
$dirs= array(
$jcomp.'.xml',
'site/'.$jcomp.'.php',
'site/views/index.html',
'site/views/to_change/index.html',
'site/views/to_change/tmpl/index.html',
'site/views/to_change/tmpl/default.xml',
'site/views/to_change/tmpl/default.php',
'site/models/index.html',
'site/controllers/index.html',
'site/tables/index.html',
'site/index.html',
'admin/index.html',
'admin/views/index.html',
'admin/models/index.html',
'admin/controllers/index.html',
'admin/sql/updates/index.html',
'admin/sql/updates/mysql/0_1.sql'
);
$file = tempnam("tmp", "zip");
$zip = new ZipArchive();
foreach ($dirs as $dir){
$zip->open($file, ZIPARCHIVE::CREATE);
$zip->addEmptyDir($dir);
switch ($dir){
case (substr( $dir, strrpos( $dir, '/' )+1 ) == "index.html"):
$zip->addFromString($dir, '<html><body bgcolor="#FFFFFF"></body></html>');
break;
case "site/$jcomp.php":
$main_php= new File();
// echo $main_php->main_controller($jcomp);
$zip->addFromString($dir,$main_php->main_controller($jcomp));
$zip->addFromString($dir, '');
break;
case "$jcomp.xml":
$main_php= new File();
$zip->addFromString($dir,$main_php->main_xml($jcomp));
break;
}
$zip->addFromString($dir, '');
}
$zip->close();
// Stream the file to the client
header('Content-Type: application/zip');
header('Content-Length: ' . filesize($file));
header('Content-Disposition: attachment; filename= "'.$component.'.zip"');
readfile($file);
unlink($file);
}
}
$component= new CreateComponent();
At a first glance I would change the following lines
$zip = new ZipArchive();
foreach ($dirs as $dir){
$zip->open($file, ZIPARCHIVE::CREATE);
$zip->addEmptyDir($dir);
to
$zip = new ZipArchive();
$zip->open($file, ZIPARCHIVE::CREATE);
foreach ($dirs as $dir){
$zip->addEmptyDir($dir);
as you are opening the file everytime you add a directory and it seems wrong to me.
On the other hand I would set a component name policy and check for it in the constructor in order to prevent input exploitation.
I don't know, maybe a good name policy would be - just alpha chars, numbers and dashes are allowed into a module name -
Related
How can I download multiple files as a zip-file using php?
You can use the ZipArchive class to create a ZIP file and stream it to the client. Something like:
$files = array('readme.txt', 'test.html', 'image.gif');
$zipname = 'file.zip';
$zip = new ZipArchive;
$zip->open($zipname, ZipArchive::CREATE);
foreach ($files as $file) {
$zip->addFile($file);
}
$zip->close();
and to stream it:
header('Content-Type: application/zip');
header('Content-disposition: attachment; filename='.$zipname);
header('Content-Length: ' . filesize($zipname));
readfile($zipname);
The second line forces the browser to present a download box to the user and prompts the name filename.zip. The third line is optional but certain (mainly older) browsers have issues in certain cases without the content size being specified.
This is a working example of making ZIPs in PHP:
$zip = new ZipArchive();
$zip_name = time().".zip"; // Zip name
$zip->open($zip_name, ZipArchive::CREATE);
foreach ($files as $file) {
echo $path = "uploadpdf/".$file;
if(file_exists($path)){
$zip->addFromString(basename($path), file_get_contents($path));
}
else{
echo"file does not exist";
}
}
$zip->close();
Create a zip file, then download the file, by setting the header, read the zip contents and output the file.
http://www.php.net/manual/en/function.ziparchive-addfile.php
http://php.net/manual/en/function.header.php
You are ready to do with php zip lib,
and can use zend zip lib too,
<?PHP
// create object
$zip = new ZipArchive();
// open archive
if ($zip->open('app-0.09.zip') !== TRUE) {
die ("Could not open archive");
}
// get number of files in archive
$numFiles = $zip->numFiles;
// iterate over file list
// print details of each file
for ($x=0; $x<$numFiles; $x++) {
$file = $zip->statIndex($x);
printf("%s (%d bytes)", $file['name'], $file['size']);
print "
";
}
// close archive
$zip->close();
?>
http://devzone.zend.com/985/dynamically-creating-compressed-zip-archives-with-php/
and there is also php pear lib for this
http://www.php.net/manual/en/class.ziparchive.php
I try to develop something like dropbox(very basic one). For one file to download, it's really easy. what i want is: when client asks me big file, i zip file in server side then send to user. But if file is too big it takes too many times to zip them and send to user.
is there any way to send files while they are compressing?
thanks for your help. Here is my simple sample code
<?php
function createzip($files, $zip_file) {
$zip = new ZipArchive;
if($zip->open($zip_file, ZipArchive::CREATE) === TRUE) {
foreach($files as $file){
$zip->addFile($file);
}
$zip->close();
return true;
}
else return false;
}
$ss=array("jj.mp4");
createzip($ss,'archiv.zip');
if(filesize("archiv.zip")>3000){
$vv=array("archiv.zip");
createzip($vv,"bbb.zip");
}
?>
use do while loop in if statement. Until file size become a desired value. I don't understand how to use do while loop and does it make it that type of compresses. Please help me out.
I use a method like this but I get file names from database if you wanna check and adapt to your codes..
$FList = $dl->select('dosyaYolu0, dosyaYolu1, dosyaYolu2, dosyaYolu3, dosyaYolu4')->table('tb_gelenEvrak')->where('mesajKodu', $filesnorm)->get();
$files = array(''.$FList->dosyaYolu0.'', ''.$FList->dosyaYolu1.'', ''.$FList->dosyaYolu2.'',''.$FList->dosyaYolu3.'', ''.$FList->dosyaYolu4.'');
$zipname = $filesnorm.'.zip';
$zip = new ZipArchive;
$zip->open($zipname, ZipArchive::CREATE);
foreach ($files as $file) {
$zip->addFile($file);
}
$zip->close();
header('Content-Type: application/zip');
header('Content-disposition: attachment; filename='.$zipname);
header('Content-Length: ' . filesize($zipname));
readfile($zipname);
I'd like download multiple files in a zip but it doesn't work: the downloaded zip folder is still empty and is like opt/lampp/htdocs/MYPROJECT/... which one I'd like simple like /filename.zip
my controller, I just get all the file names from the DB and concatenate them with the directory name where they are physically saved:
$response = new Response();
// Get the entity manager
$em = $this->getDoctrine()->getManager();
//Collect all the result from the database and zip all the files related
$files = $em->getRepository('IballotCmsBundle:Result')->findAll();
$directory = __DIR__.'/../../../../web/uploads/pinkSheet';
//$directory = __DIR__.'/../temp/de.jpg';
$filename = 'all.zip';
$zip = new ZipArchive;
$zip->open($filename, ZipArchive::CREATE);
foreach ($files as $file) {
$zip->addFile($directory.'/'.$file->getImage());
}
$zip->close();
$response->headers->set('Cache-Control', 'private');
$response->headers->set('Content-type', mime_content_type($filename));
$response->headers->set('Content-Disposition', 'attachment; filename="' . basename($filename) . '"');
$response->headers->set('Content-length', filesize($filename));
// Send headers before outputting anything
$response->sendHeaders();
$response->setContent(readfile($filename));
return $response;
I think it has to do with this line of code:
$filename = 'all.zip';
$zip = new ZipArchive;
$zip->open($filename, ZipArchive::CREATE);
You are trying to create a zip file called all.zip but you haven't specified the actual path. $filename should probably be something like this:
$filename = '/absolute/path/to/all.zip'
As for the structure of the zip file you'll need to read the docs which has a great example:
$zip = new ZipArchive;
if ($zip->open('test.zip') === TRUE) {
$zip->addFile('/path/to/index.txt', 'newname.txt');
$zip->close();
echo 'ok';
} else {
echo 'failed';
}
This will add the /path/to/index.txt file to the test.zip with the name newname.txt instead of /path/to/index.txt. You can use the second argument to control the directory (have a look at the comments in the docs for more info)
To put it simply, I would like my users to be able to download their website files, so I created a "Download Website" button which uses this script to add all files/folders in their directory which is in the variable $direc and archive those files/folders.
<?
///////// DOWNLOAD ENTIRE WEBSITE:
if(isset($_POST['download_site'])){
// define some basics
$rootPath = '../useraccounts/'.$direc.'';
$archiveName = ''.$direc.'.zip';
// initialize the ZIP archive
$zip = new ZipArchive;
$zip->open($archiveName, ZipArchive::CREATE);
// create recursive directory iterator
$files = new RecursiveIteratorIterator (new RecursiveDirectoryIterator($rootPath), RecursiveIteratorIterator::LEAVES_ONLY);
// let's iterate
foreach ($files as $name => $file) {
$filePath = $file->getRealPath();
$zip->addFile($filePath);
}
// close the zip file
if (!$zip->close()) {
echo '<p>There was a problem writing the ZIP archive.</p>';
} else {
echo '<p>Successfully created the ZIP Archive!</p>';
}
}
?>
To my surprise, this code works. Although, there are a few hiccups:
It doesn't automatically force a download of that archive.
It adds the archive in to my main directory rather than moving it to a separate directory of my choice such as site_downloads or deletes it up on completed download.
Are these problems at all fixable or if not, is there a better way to do it so my main directory does not get filled with constant downloads? I guess it will cause a problem once a Archive is created more than once, as it uses the Directory name.
Solved this by using a few different combinations:
<?
///////// DOWNLOAD ENTIRE WEBSITE:
if(isset($_POST['download_site'])){
// define some basics
$rootPath = '../useraccounts/'.$direc.'';
$archiveName = ''.$direc.'.zip';
// initialize the ZIP archive
$zip = new ZipArchive;
$zip->open($archiveName, ZipArchive::CREATE);
// create recursive directory iterator
$files = new RecursiveIteratorIterator (new RecursiveDirectoryIterator($rootPath), RecursiveIteratorIterator::LEAVES_ONLY);
// let's iterate
foreach ($files as $name => $file) {
$filePath = $file->getRealPath();
$zip->addFile($filePath);
}
// close the zip file
if (!$zip->close()) {
echo '<p>There was a problem writing the ZIP archive.</p>';
} else {
rename($archiveName, 'user_archives/'.$archiveName.'');
$yourfile = "user_archives/".$archiveName."";
$file_name = basename($yourfile);
header("Content-Type: application/zip");
header("Content-Disposition: attachment; filename=$file_name");
header("Content-Length: " . filesize($yourfile));
readfile($yourfile);
ignore_user_abort(true);
if (connection_aborted()) {
unlink('user_archives/'.$archiveName.'');
} else {
unlink('user_archives/'.$archiveName.'');
}
echo '<p>Successfully created the ZIP Archive!</p>';
}
}
?>
This seems to fix all problems.
How can I download multiple files as a zip-file using php?
You can use the ZipArchive class to create a ZIP file and stream it to the client. Something like:
$files = array('readme.txt', 'test.html', 'image.gif');
$zipname = 'file.zip';
$zip = new ZipArchive;
$zip->open($zipname, ZipArchive::CREATE);
foreach ($files as $file) {
$zip->addFile($file);
}
$zip->close();
and to stream it:
header('Content-Type: application/zip');
header('Content-disposition: attachment; filename='.$zipname);
header('Content-Length: ' . filesize($zipname));
readfile($zipname);
The second line forces the browser to present a download box to the user and prompts the name filename.zip. The third line is optional but certain (mainly older) browsers have issues in certain cases without the content size being specified.
This is a working example of making ZIPs in PHP:
$zip = new ZipArchive();
$zip_name = time().".zip"; // Zip name
$zip->open($zip_name, ZipArchive::CREATE);
foreach ($files as $file) {
echo $path = "uploadpdf/".$file;
if(file_exists($path)){
$zip->addFromString(basename($path), file_get_contents($path));
}
else{
echo"file does not exist";
}
}
$zip->close();
Create a zip file, then download the file, by setting the header, read the zip contents and output the file.
http://www.php.net/manual/en/function.ziparchive-addfile.php
http://php.net/manual/en/function.header.php
You are ready to do with php zip lib,
and can use zend zip lib too,
<?PHP
// create object
$zip = new ZipArchive();
// open archive
if ($zip->open('app-0.09.zip') !== TRUE) {
die ("Could not open archive");
}
// get number of files in archive
$numFiles = $zip->numFiles;
// iterate over file list
// print details of each file
for ($x=0; $x<$numFiles; $x++) {
$file = $zip->statIndex($x);
printf("%s (%d bytes)", $file['name'], $file['size']);
print "
";
}
// close archive
$zip->close();
?>
http://devzone.zend.com/985/dynamically-creating-compressed-zip-archives-with-php/
and there is also php pear lib for this
http://www.php.net/manual/en/class.ziparchive.php