Sample code
| The code is as follows: |
Copy code |
Include ("data/common. inc. php"); // load the database $ Conn = mysql_connect ($ pai_dbhost, $ pai_dbuser, $ pai_dbpwd); // or die (mysql_error ()); Mysql_select_db ($ pai_dbname, $ conn ); Mysql_query ("set Names '$ pai_db_language '"); $ Id = intval (isset ($ _ GET ['id'])? $ _ GET ['id']: 0 ); If ($ id) { $ ZipUrl = 'uploads/zip/'.$id.'.zip '; If (file_exists ($ zipUrl) // checks whether the object exists. { Echo '<script language = "javascript"> location. href = "'. $ zipUrl. '"; </script> '; Exit; } Else { $ SQL = "select url from". $ cfg_dbprefix. "uploads where arcid = $ id "; $ Query = mysql_query ($ SQL); // or die (mysql_error ()); If (mysql_num_rows ($ query )) { $ Array = array (); While ($ rs = mysql_fetch_array ($ query )) { $ Array [] = substr ($ rs ['URL'], 1, strlen ($ rs ['URL'])-1 ); } // Print_r ($ array ); Create_zip ($ array, $ zipUrl, true); // Create a compressed file here Echo '<script language = "javascript"> location. href = "'. $ zipUrl. '"; </script>'; // download the file after it is created. Exit; } Else { Echo 'parameter error '; Exit; } } } Else { Echo 'parameter error '; Exit; } // Query the data table /* Create a zip file */ Function create_zip ($ files = array (), $ destination = '', $ overwrite = false ){ If (file_exists ($ destination )&&! $ Overwrite) {// check whether the zip file exists Return false; } If (is_array ($ files) {// check whether the file exists Foreach ($ files as $ file) {// loop through each file If (file_exists ($ file) {// confirm that this file exists $ Valid_files [] = $ file; } } } If (count ($ valid_files )){ $ Zip = new ZipArchive (); // Create a zip file If ($ zip-> open ($ destination, $ overwrite? ZIPARCHIVE: OVERWRITE: ZIPARCHIVE: CREATE )! = True ){ Return false; } Foreach ($ valid_files as $ file) {// add a file $ Zip-> addFile ($ file, $ file ); } $ Zip-> close (); Return file_exists ($ destination ); } Else { Return false; } } |
The previous code is to connect to the dedecms database and then search for and compress the data based on the file ID. After packaging, download the data using the js output, if you download this file next time, it will automatically call this file instead of packing it again to find the database. This will reduce the server load.
Site original reprint must pay attention to from http://www.111cn.net