Changeset 864 for pro-violet-viettel


Ignore:
Timestamp:
Mar 31, 2015 2:54:28 PM (10 years ago)
Author:
quyenla
Message:

dowanload

File:
1 edited

Legend:

Unmodified
Added
Removed
  • pro-violet-viettel/www/deploy/20150304/application/modules/ajax/controllers/download.php

    r863 r864  
    1515
    1616    public function getFile() {
     17
    1718        $user = $this->session->userdata('userInfo');
    1819        $data = $this->input->post('data');
    1920        $treeArray = json_decode($data, TRUE);
    2021
     22        foreach ($treeArray as $idx=>$tree) {
     23                $treeArray[$idx]['fileurl'] = $this->encodePath($treeArray[$idx]['fileurl']);
     24                $treeArray[$idx]['name']= $this->convert_vi_to_en($treeArray[$idx]['name']);
     25                if ($tree['type'] == 'file') {
     26                    $path = $folder . "/" . $tree['name'];
     27                    $filecontent = file_get_contents($tree['fileurl']);
     28                    file_put_contents($path, $filecontent);
     29                }
     30                if ($tree['type'] == 'directory') { 
     31                    $childDirs = $this->aasort($tree['childDirs'], "id");
     32                    foreach ($childDirs as $index => $dir) {
     33                        $treeArray[$idx]['childDirs'][$index]['fileurl'] = $this->encodePath($treeArray[$idx]['childDirs'][$index]['fileurl']);
     34                        $treeArray[$idx]['childDirs'][$index]['name']= $this->convert_vi_to_en($treeArray[$idx]['childDirs'][$index]['name']);
     35                }
     36                $childDirs = $this->aasort($tree['childFiles'], "id");
     37                    foreach ($childDirs as $index => $dir) {
     38                        echo $dir['name']."<br />";
     39                        $treeArray[$idx]['childDirs'][$index]['fileurl'] = $this->encodePath($treeArray[$idx]['childDirs'][$index]['fileurl']);
     40                        $treeArray[$idx]['childDirs'][$index]['name']= $this->convert_vi_to_en($treeArray[$idx]['childDirs'][$index]['name']);
     41                }
     42               
     43                    }
     44               
     45        }
     46die();
    2147        if ((count($treeArray) == 1) && ($treeArray[0]['type'] == 'file')) {
     48           
    2249            $file = $treeArray[0];
    2350            $local_file = "downloads/" . $file['name'];
     
    2653            file_put_contents($local_file, $filecontent);
    2754        } else {
    28             $download_file=$user['username']."_".date("d-m-Y");
     55            $download_file = $user['username'] . "_" . date("d-m-Y");
    2956            $folder = "downloads/" . $download_file;
    3057            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']);
     58            foreach ($treeArray as $idx=>$tree) {
     59               
     60                if ($tree['type'] == 'file') {
     61                    $path = $folder . "/" . $tree['name'];
     62                    $filecontent = file_get_contents($tree['fileurl']);
    3763                    file_put_contents($path, $filecontent);
    3864                }
    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;
     65                if ($tree['type'] == 'directory') {
     66                    mkdir($folder . "/" . $tree['name']);
     67                    $childDirs = $this->aasort($tree['childDirs'], "id");
     68                    foreach ($childDirs as $index => $dir) {
     69                   
     70                        if ($dir['parentID'] == $tree['id']) {
     71                            $path = $folder . "/" . $tree['name'] . "/" . $dir['name'];
     72                            mkdir($path, 0777);
     73                            $childDirs[$index]['path'] = $path;
     74                        } else {
     75                            foreach ($childDirs as $index2 => $dir2) {
     76                                if ($dir2['id'] == $dir['parentID']) {
     77                                    $path = $childDirs[$index2]['path'] . "/" . $dir['name'];
     78                                    mkdir($path, 0777);
     79                                    $childDirs[$index]['path'] = $path;
    6180                                }
    6281                            }
    63                        
    6482                        }
    6583                    }
    6684                    $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                            
     85                    foreach ($childFiles as $index => $file) {
     86                        if ($file['parentID'] == $tree['id']) {
     87                            $path = $folder . "/" . $tree['name'] . "/" . $file['name'];
     88                            $file['fileurl'] = $this->encodePath($file['fileurl']);
    7389                            $filecontent = file_get_contents($file['fileurl']);
    7490                            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);
     91                        } else {
     92                            foreach ($childDirs as $index2 => $dir2) {
     93                                if ($dir2['id'] == $file['parentID']) {
     94                                    $path = $childDirs[$index2]['path'] . "/" . $file['name'];
     95                                    $filecontent = file_get_contents($file['fileurl']);
     96                                    file_put_contents($path, $filecontent);
    8597                                }
    8698                            }
     
    89101                }
    90102            }
    91            
    92             $command="tar -C downloads -cvf ".$folder.".zip"." $download_file";
    93            
     103
     104            $command = "tar -C downloads -cvf " . $folder . ".zip" . " $download_file";
     105
    94106            exec($command);
    95             $local_file = $folder.".zip";
    96             $download_file = $download_file.".zip";
    97            
    98            
     107            $local_file = $folder . ".zip";
     108            $download_file = $download_file . ".zip";
    99109        }
    100 
     110        $mime = "application/octet-stream";
     111        if (get_mime_by_extension($local_file) != "") {
     112            $mime = get_mime_by_extension($local_file);
     113        }
    101114        header('Cache-control: private');
    102         header('Content-Type: application/zip');
     115        header('Content-Type: ' . $mime);
    103116        header('Content-Length: ' . filesize($local_file));
    104         header('Content-Disposition: filename=' . $download_file);
    105         $download_rate = 512;
    106 
    107         // flush content
     117        header('Content-Disposition: attachment; filename=' . $download_file);
     118        $download_rate = 2048;
    108119        flush();
    109 
    110         // open file stream
    111120        $file = fopen($local_file, "r");
    112121
    113122        while (!feof($file)) {
    114 
    115             // send the current file part to the browser
    116123            print fread($file, round($download_rate * 1024));
    117 
    118             // flush the content to the browser
    119124            flush();
    120 
    121             // sleep one second
    122125            sleep(1);
    123126        }
    124 
    125         // close file stream
    126127        fclose($file);
    127             //unlink($local_file);
    128         if (isset($folder))
    129         {
    130             //exec("rm -fr $folder");
     128        unlink($local_file);
     129        if (isset($folder)) {
     130            exec("rm -fr $folder");
    131131        }
    132132        exit;
    133133    }
    134134
    135     public function aasort($array, $key) {
     135    private function aasort($array, $key) {
    136136        $sorter = array();
    137137        $ret = array();
     
    148148    }
    149149
     150    private function encodePath($path) {
     151        $fileName = $path;
     152        $fileNames = explode("/", $fileName);
     153        $name = $fileNames[(count($fileNames) - 1)];
     154        $fileName = str_replace($name, "", $fileName) . str_replace("+", "%20", urlencode($name));
     155        return $fileName;
     156    }
     157
     158    private function convert_vi_to_en($str) {
     159        $str = preg_replace("/(à|á|ạ|ả|ã|â|ầ|ấ|ậ|ẩ|ẫ|ă|ằ|ắ|ặ|ẳ|ẵ)/", "a", $str);
     160        $str = preg_replace("/(Ú|é|ẹ|ẻ|ẜ|ê|ề|ế|ệ|ể|á»
     161)/", "e", $str);
     162        $str = preg_replace("/(ì|í|ị|ỉ|Ä©)/", "i", $str);
     163        $str = preg_replace("/(ò|ó|ọ|ỏ|õ|ÃŽ|ồ|ố|ộ|ổ|ỗ|Æ¡|ờ|ớ|ợ|ở|ỡ)/", "o", $str);
     164        $str = preg_replace("/(ù|ú|ụ|á»§|Å©|ư|ừ|ứ|á»±|á»­|ữ)/", "u", $str);
     165        $str = preg_replace("/(ỳ|Ü|ỵ|á»·|ỹ)/", "y", $str);
     166        $str = preg_replace("/(đ)/", "d", $str);
     167        $str = preg_replace("/(À|Á|Ạ|Ả|Ã|Â|Ẋ|Ẁ|Ậ|ẚ|Ẫ|Ă|Ằ|Ắ|Ặ|Ẳ|Ẏ)/", "A", $str);
     168        $str = preg_replace("/(È|É|ẞ|Ẻ|Ẍ|Ê|Ề|ẟ|Ệ|Ể|Ễ)/", "E", $str);
     169        $str = preg_replace("/(Ì|Í|Ị|Ỉ|Äš)/", "I", $str);
     170        $str = preg_replace("/(Ò|Ó|Ọ|Ỏ|Õ|Ô|Ồ|Ố|Ộ|Ổ|Ỗ|Æ |Ờ|Ớ|Ợ|Ở|á» )/", "O", $str);
     171        $str = preg_replace("/(Ù|Ú|Ề|Ị|Åš|Ư|Ừ|Ớ|á»°|Ử|á»®)/", "U", $str);
     172        $str = preg_replace("/(Ỳ|Ý|Ỏ|á»¶|Ở)/", "Y", $str);
     173        $str = preg_replace("/(Đ)/", "D", $str);
     174//$str = str_replace(" ", "-", str_replace("&*#39;","",$str));
     175        $str=  str_replace(" ", "_", $str);
     176        return $str;
     177    }
     178
    150179}
    151180
Note: See TracChangeset for help on using the changeset viewer.