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

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

dowanload

  • Property svn:executable set to *
File size: 5.2 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           
92            $command="tar -C downloads -cvf ".$folder.".zip"." $download_file";
93           
94            exec($command);
95            $local_file = $folder.".zip";
96            $download_file = $download_file.".zip";
97           
98           
99        }
100
101        header('Cache-control: private');
102        header('Content-Type: application/zip');
103        header('Content-Length: ' . filesize($local_file));
104        header('Content-Disposition: filename=' . $download_file);
105        $download_rate = 512;
106
107        // flush content
108        flush();
109
110        // open file stream
111        $file = fopen($local_file, "r");
112
113        while (!feof($file)) {
114
115            // send the current file part to the browser
116            print fread($file, round($download_rate * 1024));
117
118            // flush the content to the browser
119            flush();
120
121            // sleep one second
122            sleep(1);
123        }
124
125        // close file stream
126        fclose($file);
127            //unlink($local_file);
128        if (isset($folder))
129        {
130            //exec("rm -fr $folder");
131        }
132        exit;
133    }
134
135    public function aasort($array, $key) {
136        $sorter = array();
137        $ret = array();
138        reset($array);
139        foreach ($array as $ii => $va) {
140            $sorter[$ii] = $va[$key];
141        }
142        asort($sorter);
143        foreach ($sorter as $ii => $va) {
144            $ret[$ii] = $array[$ii];
145        }
146        $array = $ret;
147        return $array;
148    }
149
150}
151
152/* End of file privatecontent.php */
153/* Location: ./application/modules/ajax/controllers/privatecontent.php */
Note: See TracBrowser for help on using the repository browser.