0

Hello i am trying to insert data from multiple files within a folder to a mysql database throigh a php script

they are around 750 files and located within a folder called notes on my server i used the following code

each file has the following structure

<starttitle>Title</div>
<start>
Contrary to popular belief, Lorem Ipsum is not simply random text. It has roots in a piece of classical Latin literature from 45 BC, making it over 2000 years old. Richard McClintock, a Latin professor at Hampden-Sydney College in Virginia, looked up one of the more obscure Latin words, consectetur, from a Lorem Ipsum passage, and going through the cites of the word in classical literature, discovered the undoubtable source. Lorem Ipsum comes from sections 1.10.32 and 1.10.33 of "de Finibus Bonorum et Malorum" (The Extremes of Good and Evil) by Cicero, written in 45 BC. This book is a treatise on the theory of ethics, very popular during the Renaissance. The first line of Lorem Ipsum, "Lorem ipsum dolor sit amet..", comes from a line in section 1.10.32.

The standard chunk of Lorem Ipsum used since the 1500s is reproduced below for those interested. Sections 1.10.32 and 1.10.33 from "de Finibus Bonorum et Malorum" by Cicero are also reproduced in their exact original form, accompanied by English versions from the 1914 translation by H. Rackham.
<end>

and i used the following php code to loop through files and insert in database

<?php

$con = mysqli_connect("localhost","root","","qbank");

// Check connection
if (mysqli_connect_errno())
{
    echo "Failed to connect to MySQL: " . mysqli_connect_error();
}



function get_string_between($string, $start, $end){
    $string = ' ' . $string;
    $ini = strpos($string, $start);
    if ($ini == 0) return '';
    $ini += strlen($start);
    $len = strpos($string, $end, $ini) - $ini;
    return substr($string, $ini, $len);
}

            foreach (glob("notes/*.php") as $file) {
                $file_handle = fopen($file, "r");
                while (!feof($file_handle)) {
                    $line = fgets($file_handle);
                    $title = get_string_between($line, '<starttitle>', '</div>');
                    $content = get_string_between($line, '<start>', '<end>');
                    $date = date('Y-m-d H:i:s');
                    $by = "Marco";


                    if(!empty($title) || !empty($content)){

                        $stmt = $con->prepare("INSERT INTO textbook (textbook_address, text, created_at, created_by) VALUES (?, ?, ?, ?)");
                        $stmt->bind_param("ssss", $title, $content, $date, $by);
                        $stmt->execute();
                        $stmt->close();

                    }

                }
                fclose($file_handle);

}

?>

the problem is that this code insert only title in one row and put the text on the next row but i need to put each title with it's own text in the same row

any help will be appreciated

1
  • @zebnat thanks it works please right it and i will chose as an answer Commented Sep 25, 2018 at 18:44

1 Answer 1

2

Instead of reading line by line just use file_get_contents (if its a dirty script). then strip your data from the result, and then execute your query. All this foreach file.

Something like this:

foreach (glob("notes/*.php") as $file) {
  $data = file_get_contents($file);
  $title = get_string_between($data, '<starttitle>', '</div>');
  $content = get_string_between($data, '<start>', '<end>');
  $date = date('Y-m-d H:i:s');
  $by = "Marco";
  if(!empty($title) || !empty($content)){
    $stmt = $con->prepare("INSERT INTO textbook (textbook_address, text, created_at, created_by) VALUES (?, ?, ?, ?)");
    $stmt->bind_param("ssss", $title, $content, $date, $by);
    $stmt->execute();
    $stmt->close();
   }
}
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.