2015-06-29 46 views
0

我創建了文本文件並在執行代碼時下載。但我需要將此下載的文件存儲在服務器內的指定文件夾中。我嘗試過很多方法,但我沒有得到任何解決方案。我在下面提到了我的代碼,任何人都可以引導我嗎?在php中創建內容類型並下載指定的路徑?

$saving_name = $i.''.$machineid.'.'.$count_pad; 

       foreach($data['SalesDetails'] as $d) 
       { 

        $val = $d->inv_total - $d->inv_discount; 

        $explodedval = explode(".",$val); 

        $totalval = str_pad($explodedval['0'], 8, "0", STR_PAD_LEFT); 

        $totalvals = str_pad($explodedval['1'], 2, "0", STR_PAD_RIGHT); 

        $result = $totalval.'.'.$totalvals;  

        $dates = str_replace("-","",$d->date); 

        $out .= $i.''.$machineid.''.$dates.''.$result.' '."\r\n"; 

       } 
        header('Content-Description: File Transfer'); 
        header('Content-Type: application/octet-stream'); 
        header('Content-Disposition: attachment; filename='.$saving_name.'.txt'); 
        header('Content-Transfer-Encoding: binary'); 
        header('Expires: 0'); 
        header('Cache-Control: must-revalidate, post-check=0, pre-check=0'); 
        header('Pragma: public'); 
        echo "\xEF\xBB\xBF"; // UTF-8 BOM 
        echo $out; 
        exit; 

這裏的路徑需要移動的下載文件,/var/www/html/art/assets/uploads/edi/sample.txt

+0

我在本地系統下載的代碼。但我需要在服務器文件夾內下載。如何移動或如何將路徑設置到指定的文件夾中。 –

回答

1

謝謝您的支持。最後我得到了這個解決方案如下所示:

$saving_name = $i.''.$machineid.'.'.$count_pad; 

       foreach($data['SalesDetails'] as $d) 
       { 

        $val = $d->inv_total - $d->inv_discount; 

        $explodedval = explode(".",$val); 

        $totalval = str_pad($explodedval['0'], 8, "0", STR_PAD_LEFT); 

        $totalvals = str_pad($explodedval['1'], 2, "0", STR_PAD_RIGHT); 

        $result = $totalval.'.'.$totalvals;  

        $dates = str_replace("-","",$d->date); 

        $out .= $i.''.$machineid.''.$dates.''.$result.' '."\r\n"; 

       } 
        header('Content-Description: File Transfer'); 
        header('Content-Type: application/octet-stream'); 
        header('Content-Disposition: attachment; filename='.$saving_name.'.txt'); 
        header('Content-Transfer-Encoding: binary'); 
        header('Expires: 0'); 
        header('Cache-Control: must-revalidate, post-check=0, pre-check=0'); 
        header('Pragma: public'); 
        echo "\xEF\xBB\xBF"; // UTF-8 BOM 
        echo $out; 

        $backup_file = '/var/www/html/artbak/assets/uploads/edi/'.$saving_name.'.txt'; 

        //save file 
        $handle = fopen($backup_file,'w+'); 
        fwrite($handle,$out); 
        fclose($handle); 

        exit; 

現在文件存儲在指定的文件夾以及下載也。