Create content type and download with specified path in php?

518 views Asked by At

I have created the text file and downloading when I execute the code. But I need to store this downloaded file in a specified folder inside the server. I have tried in many ways, but I didn't get any solution. I have mentioned my code below, Can anyone guide me?

$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; 

Here is the path need to move the downloading file, /var/www/html/art/assets/uploads/edi/sample.txt

1

There are 1 answers

0
Jagan Akash On

Thank you for your support. Finally I got solution for this which is shown below:

$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; 

Now the file stored in the specified folder as well download also.