将每个文本文件的内容保存到数据库表mysql中

时间:2016-12-04 03:23:41

标签: php mysql sql file-get-contents tokenize

我有100个带有.txt扩展名的文本文件。我想将每个文件中的文本文件的内容保存到MySQL数据库表中,在该字段中,fieldnya名称我给出了名称" token"。所以我的表的结构是:id,filename,token,size。我已尝试使用以下php代码:

it is my code :
 <?php
  include_once 'connection.php';
  $array_file = glob('savetoken/*.txt');
  foreach ($array_file as $token_words) {
  $string_Text = file_get_contents($token_words);
  global $string_Teks;
  } 
if(isset($_POST['btnUpload']))
    {    
$FileInput = $_FILES['data_upload']['name'];
$file_loc = $_FILES['data_upload']['tmp_name'];
$file_size = $_FILES['data_upload']['size'];
$file_type = $_FILES['data_upload']['type'];

$folder="savestopword/";
$UploadDirectory = 'c:/xampp/htdocs/try/savestopword/';
$new_size = $file_size/1024;  
$new_file_name = strtolower($FileInput);
$final_file = str_replace('','_',$new_file_name);
global $final_file;
$File_Name          = strtolower($_FILES['data_upload']['name']);
$File_Ext           = substr($File_Name, strrpos($File_Name, '.')); 
$NewFileName        = $File_Name;
if(move_uploaded_file($_FILES['data_upload']['tmp_name'], $UploadDirectory.$NewFileName ))
   {
    $sql = "INSERT INTO token_keyword(filename,token,size) VALUES('$final_file','$string_Text','$new_size')";
    mysql_query($sql);
   echo "<script>";
    ?>
    alert('successfully uploaded');
    window.location.href='upload_modifikasi.php?success';
    <?php
    echo "</script>";
    die('Success! File Uploaded.');
    include "upload_modification.php";
}else{
    die('error uploading File!');
}   
if(move_uploaded_file($file_loc,$folder.$final_file))
{

}
else
{
    ?>
    <script>
    alert('error while uploading file');
    window.location.href='upload_modification.php?fail';
    </script>
    <?php
    }
   }
  ?>

为了测试我使用三个文本文件。当我使用代码时,当我上传发生的文本文件时: 文件成功上传到数据库表后,文件已成功保存到存储目录,并且没有错误。但是在字段令牌上发生的是一个文本文件,将三个文件的所有内容都放入字段.txt令牌中,当我想要的是上传一个文件时,字段令牌只存储文件的内容,而不是存储所有文件的内容。不管怎么解决?我试过谷歌搜索,但没有找到办法。

1 个答案:

答案 0 :(得分:0)

解决方案:

<?php
include_once 'connection.php';
if(isset($_POST['max_filesize']))
{
    $FileInput = $_FILES['FileInput']['name'];
    $file_loc = $_FILES['FileInput']['tmp_name'];
    $file_size = $_FILES['FileInput']['size'];
    $file_type = $_FILES['FileInput']['type'];
    $folder="yourdirectoryforsavefile/";
    $UploadDirectory = 'c:/xampp/htdocs/yourfolder/yourfolder/';
    $new_size = $file_size/1024;  
    $new_file_name = strtolower($FileInput);
    $final_file = str_replace('','_',$new_file_name);
    $File_Name = strtolower($_FILES['FileInput']['name']);
    $File_Ext = substr($File_Name, strrpos($File_Name, '.')); 
    $NewFileName = $File_Name;
    if(move_uploaded_file($_FILES['FileInput']['tmp_name'], $UploadDirectory.$NewFileName ))
    {
        $sql = "INSERT INTO yourtablename (name,size,type)   VALUES('$final_file','$new_size','$file_type')";
        mysql_query($sql);
        echo "<script>";
        ?>
            alert('successfully uploaded');
            window.location.href='upload.php?success';
        <?php
        echo "</script>";
        die('Success! File Uploaded.');
        include "upload.php";
    }else{
        die('error uploading File!');
    }   
    if(move_uploaded_file($file_loc,$folder.$final_file))
    {

    }
    else
    {
        ?>
        <script>
            alert('error while uploading file');
            window.location.href='upload.php?fail';
        </script>
        <?php
    }
}
?>