source: pro-violet-viettel/www/deploy/20150304/application/modules/ajax/controllers/download.php @ 862

Last change on this file since 862 was 862, checked in by quyenla, 10 years ago

dowanload

  • Property svn:executable set to *
File size: 5.0 KB
Line 
1<?php
2
3if (!defined('BASEPATH'))
4    exit('No direct script access allowed');
5
6class Download extends MX_Controller {
7
8    public function __construct() {
9        parent::__construct();
10        $user = $this->session->userdata('userInfo');
11        $this->vservices->setApiUrl($this->config->item('api_url'));
12        $this->vservices->setConnection($this->curl);
13        $this->vservices->setUserId($user['us_id']);
14    }
15
16    public function getFile() {
17        $user = $this->session->userdata('userInfo');
18        $data = $this->input->post('data');
19        $treeArray = json_decode($data, TRUE);
20
21        if ((count($treeArray) == 1) && ($treeArray[0]['type'] == 'file')) {
22            $file = $treeArray[0];
23            $local_file = "downloads/" . $file['name'];
24            $download_file = $file['name'];
25            $filecontent = file_get_contents($file['fileurl']);
26            file_put_contents($local_file, $filecontent);
27        } else {
28            $download_file=$user['username']."_".date("d-m-Y");
29            $folder = "downloads/" . $download_file;
30            mkdir($folder, 0777);
31            foreach ($treeArray as $tree)
32            {
33                if($tree['type']=='file')
34                {
35                    $path=$folder."/".$tree['name'];
36                    $filecontent = file_get_contents($file['fileurl']);
37                    file_put_contents($path, $filecontent);
38                }
39                if($tree['type']=='directory')
40                {
41                    mkdir($folder."/".$tree['name']);
42                    $childDirs = $this->aasort($tree['childDirs'],"id");
43                    foreach ($childDirs as $index=>$dir)
44                    {
45                        if($dir['parentID']==$tree['id'])
46                        {
47                           
48                        $path=$folder."/".$tree['name']."/".$dir['name'];
49                        mkdir($path,0777);
50                        $childDirs[$index]['path']=$path;
51                        }
52                        else
53                        {
54                            foreach ($childDirs as $index2=>$dir2)
55                            {
56                                if($dir2['id']==$dir['parentID'])
57                                {
58                                   $path=$childDirs[$index2]['path']."/".$dir['name'];
59                                   mkdir($path,0777);
60                                   $childDirs[$index]['path']=$path;
61                                }
62                            }
63                       
64                        }
65                    }
66                    $childFiles = $tree['childFiles'];
67                    foreach ($childFiles as $index=>$file)
68                    {
69                        if ($file['parentID']==$tree['id'])
70                        {
71                            $path=$folder."/".$tree['name']."/".$file['name'];
72                           
73                            $filecontent = file_get_contents($file['fileurl']);
74                            file_put_contents($path, $filecontent);
75                        }
76                        else
77                        {
78                            foreach ($childDirs as $index2=>$dir2)
79                            {
80                                if($dir2['id']==$file['parentID'])
81                                {
82                                   $path=$childDirs[$index2]['path']."/".$file['name'];
83                                   $filecontent = file_get_contents($file['fileurl']);
84                                   file_put_contents($path, $filecontent);
85                                }
86                            }
87                        }
88                    }
89                }
90            }
91            $local_file = $folder.".zip";
92            $download_file = $download_file.".zip";
93            exec("zip -r ".$folder.".zip"." $folder/*");
94           
95        }
96
97        header('Cache-control: private');
98        header('Content-Type: application/zip');
99        header('Content-Length: ' . filesize($local_file));
100        header('Content-Disposition: filename=' . $download_file);
101        $download_rate = 512;
102
103        // flush content
104        flush();
105
106        // open file stream
107        $file = fopen($local_file, "r");
108
109        while (!feof($file)) {
110
111            // send the current file part to the browser
112            print fread($file, round($download_rate * 1024));
113
114            // flush the content to the browser
115            flush();
116
117            // sleep one second
118            sleep(1);
119        }
120
121        // close file stream
122        fclose($file);
123
124        exit;
125    }
126
127    public function aasort($array, $key) {
128        $sorter = array();
129        $ret = array();
130        reset($array);
131        foreach ($array as $ii => $va) {
132            $sorter[$ii] = $va[$key];
133        }
134        asort($sorter);
135        foreach ($sorter as $ii => $va) {
136            $ret[$ii] = $array[$ii];
137        }
138        $array = $ret;
139        return $array;
140    }
141
142}
143
144/* End of file privatecontent.php */
145/* Location: ./application/modules/ajax/controllers/privatecontent.php */
Note: See TracBrowser for help on using the repository browser.