Oh no! Where's the JavaScript?
Your Web browser does not have JavaScript enabled or does not support JavaScript. Please enable JavaScript on your Web browser to properly view this Web site, or upgrade to a Web browser that does support JavaScript.
Not a member yet? Click here to register.
Forgot Password?

Pack files to zip and download function

Asked Modified Viewed 9,656 times
H
Homdax
H
Homdax 10
  • Fusioneer, joined since
  • Contributed 2,247 posts on the community forums.
  • Started 108 threads in the forums
  • Started this discussions
asked
Fusioneer

I am interested in input towards a function that would pack files in a web directory to a zip file, upon user request.

Let me try to describe it step by step.

- a php zip function (?)
- that upon user input zips files in a defined directory
- and preps a download link for user to click on and download those files
- delete zip package, either after download or at a given time, or by scanning a directory for changes in files (possible?)

The reason I would need this is for theme files. Currently working a lot on different themes, yet according to licensing they must be available for download, and frankly I find it a tad tedious to manually prep a new zip for every codechange, so why not try to automate the process? Someone pops in and want the theme files for current default theme, clicks a button "Prep current fileset in the theme directory for download" wait wait wait, present a download link.

Good idea?
Security concerns?
0 replies

20 posts

T
Tyler
T
Tyler 10
Helping, would be pointing you in the right direction, not doing it all for you.
  • Member, joined since
  • Contributed 198 posts on the community forums.
  • Started 3 threads in the forums
answered
Member

I had a worked a section into an infusion that grabbed the db and all the files and zipped it and prompted a download.

Not sure if I can find it or not. This is a great idea even though I've done it before - integrating another projects code.

BTW the "generate button" wouldn't be needed unless the zip hasn't been created before... Could always just have an automated scan check each theme file against a zip file name where the dls will be stored. If it doesn't exists then create the zip and save it so users will already have the dl available.

I think something in a more dynamic manor would be nice but with an api where you could do/include almost anything in the zip.

I won't say I'll do it only because I've got a few things on my plate.
0 replies
H
Homdax
H
Homdax 10
  • Fusioneer, joined since
  • Contributed 2,247 posts on the community forums.
  • Started 108 threads in the forums
  • Started this discussions
answered
Fusioneer

Sounds like some that could work, the parameters that need define would be directory /root/themes/the_theme_in_question/*.*

I think that a generated zip for each download (every time a new one) would be the best way to go, then I wont have to bother about any zipped file storage (?) and always get the latest version.
0 replies
K
KasteR
K
KasteR 10
  • Senior Member, joined since
  • Contributed 290 posts on the community forums.
  • Started 1 thread in the forums
answered
Senior Member

Richard,
This is entirely possible. I do not see any immediate security risks. Although this may depend on how the design is. For example, if we are including a $_GET option to determine which files to ZIP, this may impose a security concern. Although obviously this may be addressed/secured.

This makes complete sense. I seriously think this should be taken with higher consideration towards 8 development. This would make a site's theme become a true open source. I like where your head is with this. I've been messing around with a ZIP function, needs some fine tuning.

The function that I've been working with does not leave a file on the server. It will create a temp zip, and remove the temp file. I'll try to put forth some time over the next few days. Anyone else, please weigh in..
0 replies
H
Homdax
H
Homdax 10
  • Fusioneer, joined since
  • Contributed 2,247 posts on the community forums.
  • Started 108 threads in the forums
  • Started this discussions
answered
Fusioneer

Temp file is where I was going as well. Great input, please catch me on Skype for any brainstorming or testing, but, as I always recommend, if you have any code examples. please put them here for general input.
0 replies
— 1 month later —
K
KasteR
K
KasteR 10
  • Senior Member, joined since
  • Contributed 290 posts on the community forums.
  • Started 1 thread in the forums
answered
Senior Member

Hey Richard, I realize it's been a while. However I do have something to offer.
[syntaxhighlighter brush=php,first-line=1,highlight=0,collapse=false,html-script=false]function ZipDir($dir, $filename)
{
// increase script timeout value
ini_set("max_execution_time", 300);
// create object
$zip = new ZipArchive();
// open archive
if ($zip->open($filename, ZIPARCHIVE::CREATE) !== TRUE) {
die ("Could not open archive"wink;
}
// initialize an iterator
// pass it the directory to be processed
$iterator = new RecursiveIteratorIterator(new RecursiveDirectoryIterator($dir));
// iterate over the directory
// add each file found to the archive
foreach ($iterator as $key=>$value) {
$zip->addFile(realpath($key), $key) or die ("ERROR: Could not add file: $key"wink;
}
// close and save archive
$zip->close();
echo "Archive created successfully.";
}[/syntaxhighlighter]Calling the function:
[syntaxhighlighter brush=php,first-line=1,highlight=0,collapse=false,html-script=false]ZipDir("directorytozip","output_filename.zip"wink[/syntaxhighlighter]This function will simply create a zip and archive the variable directory contents, and leave the newly created zip on the root of the variable directory. Not exactly as you asked, but a step towards it. Maybe someone else can build off of this, or I'll try to follow up.
0 replies
G
Grimloch
G
Energy can neither be created nor destroyed; only transformed !
  • Senior Member, joined since
  • Contributed 722 posts on the community forums.
  • Started 141 threads in the forums
  • Answered 2 questions
answered
Senior Member

Hey KasteR: I know that I'm not part of yours and Richards endeavor here but I'm very interested in this function; so I decided to test it and here are the results.

The Submit and Form:


if (!isset($_GET['upload_folder'])) { $upload_folder = INFUSIONS."news_letter_panel/attach/"; }

if (isset($_POST['archive'])) {
   ZipDir("upload_folder", "test_arch.zip");
}
// rest of my script code
// then the form

echo "<form name='do_archive' method='post' action='".FUSION_SELF.$aidlink."'>\n";
echo "<table style='width:500px' align='center' cellspacing='0' cellpadding='0'><tr>\n";
echo "<td align='center'><hr><br /></td>\n";
echo "</tr><tr>\n";
echo "<td align='center'><b>".$locale['nl_852']."</b><br /><br /></td>\n";
echo "</tr><tr>\n";
echo "<td align='center'><input class='button' type='submit' name='archive' value='".$locale['nl_244']."' /></td>\n";
echo "</tr></table></form>";


And this is the error that is produced in the 7.02.07 error log:
include/functions.php
RecursiveDirectoryIterator::__construct(upload_folder) [<a href='recursivedirectoryiterator.--construct'>recursivedirectoryiterator.--construct</a>]: failed to open dir: No such file or directory Line: 35


Not sure how to declare the dir to the function. Any ideas?
0 replies
K
KasteR
K
KasteR 10
  • Senior Member, joined since
  • Contributed 290 posts on the community forums.
  • Started 1 thread in the forums
answered
Senior Member

No problem Terry, if anything input from you is appreciated.

Are you wanting to have a form submitted, then retrieve a value to determine a variable directory path to zip?

One thing I should mention is that the path specified must take into account of the functions location on the server. Let's say "include/functions.php" is the location of the function, and the directory you are zipping is "../downloads". You notice the "../" for the zip path, because the referenced position is from the function's location.

With your snippet, try changing
ZipDir("upload_folder", "test_arch.zip");

to
ZipDir($upload_folder, "test_arch.zip");
0 replies
G
Grimloch
G
Energy can neither be created nor destroyed; only transformed !
  • Senior Member, joined since
  • Contributed 722 posts on the community forums.
  • Started 141 threads in the forums
  • Answered 2 questions
answered
Senior Member

Hey hey that actually works! It creates the zip file one directory up from the function code, in the infusion root folder however; what I would actually like to do if it's possible, is to create the zip in the folder where the files are. I would think the zip function should be able to handle that if the zip creation parameters are set correctly. I suppose I could experiment and see if it'll work that way unless you have an answer for me already??

B)
0 replies
K
KasteR
K
KasteR 10
  • Senior Member, joined since
  • Contributed 290 posts on the community forums.
  • Started 1 thread in the forums
answered
Senior Member

Yes, you can. Same rules apply. Just know from where to specify the proper file path. Here's an example which shows how one can archive the site's current theme, and place into the downloads directory.

ZipDir(THEME, DOWNLOADS.str_replace("../","",THEME).".zip")
0 replies
G
Grimloch
G
Energy can neither be created nor destroyed; only transformed !
  • Senior Member, joined since
  • Contributed 722 posts on the community forums.
  • Started 141 threads in the forums
  • Answered 2 questions
answered
Senior Member

Hmmmm... actually what I mean is can it create the zip in the SAME directory that it's recursing for the files?? Essentially zip files in place to a new zip archive??
0 replies
K
KasteR
K
KasteR 10
  • Senior Member, joined since
  • Contributed 290 posts on the community forums.
  • Started 1 thread in the forums
answered
Senior Member

Sorry I misunderstood. Staying with my example since I'm not entirely sure where this is headed for you. Although I'm sure this information helps or at least provides a better understanding.

$var = preg_replace("#/+#", "", THEME);
   $var = str_replace(".", "", $var);
   $var = str_replace("themes","", $var);
   $var = THEME.$var.".zip";

ZipDir(THEME, $var);


This will zip the site's current theme, and place it in the site's current theme directory. The file name will be that of the theme directory.

Since this is not creating the file as a temp file, multiple requests will simply add to the archive (assuming changes/files have been committed).

If this is not what you're looking for, the easiest way to explain is that the first field of the function is the INPUT, and the second is the OUTPUT. You may arrange variables accordingly.
0 replies
G
Grimloch
G
Energy can neither be created nor destroyed; only transformed !
  • Senior Member, joined since
  • Contributed 722 posts on the community forums.
  • Started 141 threads in the forums
  • Answered 2 questions
answered
Senior Member

OK well; thanks for the info though it's too cryptic for me to do anything with. What I was going to try to do is crazy anyway so I'm just going to abandon the idea. Thanks for your time and input though. :)
0 replies
H
Homdax
H
Homdax 10
  • Fusioneer, joined since
  • Contributed 2,247 posts on the community forums.
  • Started 108 threads in the forums
  • Started this discussions
answered
Fusioneer

Sorry I missed this, much obliged to your PM Kaster.

Cant wait to test this, will do and get back.

Ehmm, how would you suggest this to be implemented? What files get what code? A CP?
0 replies
K
KasteR
K
KasteR 10
  • Senior Member, joined since
  • Contributed 290 posts on the community forums.
  • Started 1 thread in the forums
answered
Senior Member

There must be some kind of control in play, since this is merely a function. This may be in the form of a panel. That's probably the most practical.

I can put a panel together, at least as a form of example. Maybe build a table of installed themes along with a download link. Similar to the downloads page. This can even be taken a step further to include infusions as well. Just a thought.

There are many ways this function can be used. It can be improved. And remember, this is only half of what you requested. There would still be a file remaining on the server. Figured the recursive zipping was the hard part. I'll get back with the panel.
0 replies
— 3 months later —
H
Homdax
H
Homdax 10
  • Fusioneer, joined since
  • Contributed 2,247 posts on the community forums.
  • Started 108 threads in the forums
  • Started this discussions
answered
Fusioneer

I am putting this to a test on a live site I soon will announce. The impact on the community I hope for is that the Theme used Live on any site should always be available for download, regardless of development status, unless some one removes the link to that code. Thus taking away the need to zip up the theme files and make them available via a separate download link.

Hence it would have to be included in the new theme engine as default on, with the option to turn it off. But that is another story that I may submit as road map.
0 replies
C
Chan
C
Chan 0
Lead Developer of PHP-Fusion
  • Super Admin, joined since
  • Contributed 3,842 posts on the community forums.
  • Started 232 threads in the forums
  • Answered 6 questions
answered
Super Admin

Kaster, very good function, but can you alter the codes to do such?

Quote


fusion_zip($src_directory, $target_directory, $filename=false);


Source directory is like THEMES constant

Target Directory is like IMAGES constant

Filename is a basic stripinput text filename , so if $filename is not

declared, just randomize it and stock into the target directory?

Here is a randomizer I can contribute:



function randomize($length = 10) {
        return substr(str_shuffle("0123456789abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ"), 0, $length);
    }



Usage:

$filename = ($filename) ? stripinput($filename) : randomize(15);


I'm very interested to put your class into official 8 includes... if you can do it as such. Thank you.
0 replies
K
KasteR
K
KasteR 10
  • Senior Member, joined since
  • Contributed 290 posts on the community forums.
  • Started 1 thread in the forums
answered
Senior Member

No worries, there has been developments since my last post. Forgive my delinquency, however I was able to change the function to not leave some ghost file on the server. It will simply push the request to one's browser. I have assembled a rough panel, more less modifying the Theme Switcher panel to include this functionality as a concept.

Richard, I completely agree. I'm sure many others here do as well.

Hien, shouldn't be a problem and thanks for that. Bare with me, as I haven't had a profusion of time lately. No pun intended.

Ref: http://themes.kaster.us (Theme Switcher panel, bottom right)
0 replies
H
Homdax
H
Homdax 10
  • Fusioneer, joined since
  • Contributed 2,247 posts on the community forums.
  • Started 108 threads in the forums
  • Started this discussions
answered
Fusioneer

:G
0 replies
F
Falk
F
Falk 148
Need help?, Having trouble?
• View our Documentation for Guides, Standards and Functions
• Name and Organize your Topics and Content correctly in the corresponding Forums for best support results
• Attaching Log Files and Screenshots when reporting issues will help
• Provide with an URL to live example if one exists
• Please read the How to Report an Error post
• Please read and comply with the Code of Conduct

(¯·._.·(¯°·._.·°º*[ Project Manager ]*º°·._.·°¯)·._.·¯)
  • Super Admin, joined since
  • Contributed 6,201 posts on the community forums.
  • Started 639 threads in the forums
  • Answered 12 questions
answered
Super Admin

Looks great, It is inline with my current project ( Check latest news ).
Perhaps we can use it here and in / with our new AddonDB, I would prefer a overlay for preview.
Like a new session to a targeted area. Perhaps forums. Just to save bandwidth not to mention misunderstandings on larger customized content.
0 replies

Labels

None yet

Statistics

  • Views 0 views
  • Posts 20 posts
  • Votes 0 votes
  • Topic users 6 members

6 participants

F
F
Falk 148
Need help?, Having trouble?
• View our Documentation for Guides, Standards and Functions
• Name and Organize your Topics and Content correctly in the corresponding Forums for best support results
• Attaching Log Files and Screenshots when reporting issues will help
• Provide with an URL to live example if one exists
• Please read the How to Report an Error post
• Please read and comply with the Code of Conduct

(¯·._.·(¯°·._.·°º*[ Project Manager ]*º°·._.·°¯)·._.·¯)
  • Super Admin, joined since
  • Contributed 6,201 posts on the community forums.
  • Started 639 threads in the forums
  • Answered 12 questions
H
H
Homdax 10
  • Fusioneer, joined since
  • Contributed 2,247 posts on the community forums.
  • Started 108 threads in the forums
  • Started this discussions
C
C
Chan 0
Lead Developer of PHP-Fusion
  • Super Admin, joined since
  • Contributed 3,842 posts on the community forums.
  • Started 232 threads in the forums
  • Answered 6 questions
G
G
Energy can neither be created nor destroyed; only transformed !
  • Senior Member, joined since
  • Contributed 722 posts on the community forums.
  • Started 141 threads in the forums
  • Answered 2 questions
T
T
Tyler 10
Helping, would be pointing you in the right direction, not doing it all for you.
  • Member, joined since
  • Contributed 198 posts on the community forums.
  • Started 3 threads in the forums
K
K
KasteR 10
  • Senior Member, joined since
  • Contributed 290 posts on the community forums.
  • Started 1 thread in the forums

Notifications

Track thread

You are not receiving notifications from this thread.

Related Questions

Not yet