Tutorial: Storing files and retrieving them as compressed files, using PHP & MySQL.
I had a requirement to do as getting a few parameters and according to them, compressing a set of files and give it to users for download. So, the best way would be compressing it into zip files. For those, who don't know what are zip files, you can see this definition, taken from WinZip's official website:
First and foremost, we need a MySQL Database, with the following tables. A simplified way is this:
“Zip files (.zip or .zipx) are single files, sometimes called “archives”, that contain one or more compressed files. Zip files make it easy to keep related files together and make transporting, e-mailing, downloading and storing data and software faster and more efficient. The Zip format is the most popular compression format used in the Windows environment, and WinZip is the most popular compression utility.”Okay, as now we are clear in what we are doing, lets work on creating the application.
– winzip.com
First and foremost, we need a MySQL Database, with the following tables. A simplified way is this:
select * from plugins; +----+---------------+-----------------------------------+------------------+ | id | name | desc | filename | +----+---------------+-----------------------------------+------------------+ | 1 | jQuery | jQuery is a cross-browser JavaScr | jquery.js | | 2 | MooTools | MooTools (My Object-Oriented Tool | mootools.js | | 3 | Scriptaculous | script.aculo.us is a JavaScript l | scriptaculous.js | | 4 | PrototypeJS | Provides class-style Object Orien | prototypejs.js | | 5 | Lightbox 2 | Lightbox 2 is a simple, unobtrusi | lightbox2.js | +----+---------------+-----------------------------------+------------------+
select * from downloads; +----+----------+----------+ | id | pluginid | username | +----+----------+----------+ | 1 | 1 | Praveen | | 2 | 4 | Praveen | +----+----------+----------+The queries for the same are as follows:
CREATE TABLE `download` ( `id` int(11) NOT NULL AUTO_INCREMENT, `pluginid` int(11) NOT NULL, `username` varchar(255) NOT NULL, PRIMARY KEY (`id`) ) ENGINE=InnoDB ; CREATE TABLE `plugins` ( `id` int(11) NOT NULL AUTO_INCREMENT, `name` varchar(255) NOT NULL, `desc` varchar(255) NOT NULL, `filename` varchar(255) NOT NULL, PRIMARY KEY (`id`) ) ENGINE=InnoDB ; INSERT INTO `plugins` (`id`, `name`, `desc`, `filename`) VALUES ( NULL, 'jQuery', 'jQuery is a cross-browser JavaScript library designed to simplify the client-side scripting of HTML.', 'jquery.js' ), ( NULL, 'MooTools', 'MooTools (My Object-Oriented Tools) is a lightweight, object-oriented, web-application framework for JavaScript, written in JavaScript', 'mootools.js' ), ( NULL, 'Scriptaculous', 'script.aculo.us is a JavaScript library built on the Prototype JavaScript Framework, providing dynamic visual effects and UI.', 'scriptaculous.js' ), ( NULL, 'PrototypeJS', 'Provides class-style Object Oriented and AJAX, freely distributable under the terms of an MIT-style license.', 'prototypejs.js' ), ( NULL, 'Lightbox 2', 'Lightbox 2 is a simple, unobtrusive script used to overlay images on the current page. It''s a snap to setup and works on all modern browsers.', 'lightbox2.js'); INSERT INTO `download` (`id`, `pluginid`, `username`) VALUES ( NULL, '1', 'Praveen' ), ( NULL, '4', 'Praveen' );
0