0

I have some problems with my project. In particular, I want to download file text with the name is the Date by code php. Can anyone have a suggestion or any idea?


@Suraj Here is my code. Specifically, I did create a text file that includes data that I store in my database. Then I want one more thing to be able to download this file with the name Date

     <?php
        $host = "localhost";
        $dbuser = "id16877284_hoan";
        $dbpass = "Dauhieubotu9@";
        $db = "id16877284_admin";
        
        date_default_timezone_set('Asia/Ho_Chi_Minh');
        $conn = new mysqli($host,$dbuser,$dbpass, $db);

        if ($conn->connect_error) {
        die("Connection failed: " . $conn->connect_error);}
        else { echo "Connected to mysql database. <br>"; }

        if(!empty($_POST['sensor'])) {
            $sensor = $_POST['sensor'];
            $query = "INSERT INTO esp8266(sensor) values ('$sensor')";
            $run = mysqli_query($conn,$query) or die (mysqli_error($conn));
    
            $file = fopen(date("d-m-Y").".txt","a+");
            fwrite($file,"sensor:");
            fwrite($file,$sensor);
            fwrite($file, date("d-m-Y H:i:s A")."\n");
            echo "data will be saved"."\n";
             
            if($run){
                echo "form successful";
            }
            else {
                echo "form failed";
            }
        }
        
        ?>
  • 1
    You want to read a file with php or make a file downloadable on your page ? – Victorbzh Dec 04 '21 at 12:09
  • Actually, I have created one file text with a name is the date (Eg: 04-12-2021.txt). But I still dont know how to download it to my computer. – hoan nguyen Dec 04 '21 at 12:16
  • If you mean to force download a file then please refer https://stackoverflow.com/questions/11903436/write-a-text-file-and-force-to-download-with-php/11903544 – Suraj Dec 04 '21 at 16:10
  • @Suraj I have code to created file text is `$file = fopen(date("d-m-Y").".txt","a+");` then it will make a file. For example, today is 05/12/2021 then the file name must be 05-12-2021.txt . I tried your code by changing the fixed name like handle.txt to date("d-m-Y").".txt" but it doesn't work. – hoan nguyen Dec 05 '21 at 03:01

0 Answers0