Save multiple text content from multiple .txt files with file_get_contents in my database in php

Viewed 42

I'm working on a script that allows me to take .txt files content from a folder on my computer and read them with file_get_contents() then save them to my database, each time I manage to save only the first file, and I would like to save the contents of each file from the values ​​retrieved by the file_get_contents() or file() function (with for loop).

Thank you for your help !

Here's the code:

    <?php

$pdo = require 'db_settings.php';

if(isset($_POST['submit'])){

    $line1 = $_FILES['filetxt']["tmp_name"];
    
    echo '<pre>';

    $count = count($line1);

        for ($i=0; $i < $count; $i++) { 
            $insert1 = file_get_contents($line1[$i]);
        
            // Begin
                try {
                        $sql = "INSERT INTO fill_wp (content) VALUES (:insert1)"; 
                        $stmt = $conn->prepare($sql);
                        $stmt->execute(array(":insert1" => $insert1));

                    }
                
                        catch(PDOException $exception){
                        die('ERROR: ' . $exception->getMessage());
                
                }
        }

        //End
        echo '</pre>';
    
}

        
        <form method='post' action='get_temp.php'>
              <input type="text" name="urldirectory" id="textfileurl">
              <input type='submit' name='submit' value='Upload'>
        </form>
0 Answers
Related