Dedecms automatically packs and downloads images in articles

Source: Internet
Author: User

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

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.