gpt4 book ai didi

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

转载 作者:行者123 更新时间:2023-11-29 19:46:17 25 4
gpt4 key购买 nike

我有 100 个扩展名为 .txt 的文本文件。我想将每个文件中的文本文件的内容保存到 MySQL 数据库表中,在字段中,我将其中的 fieldnya 名称命名为“token”。所以我的表的结构是:id、文件名、 token 、大小。我尝试过以下 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
}
}
?>

为了进行测试,我使用了三个文本文件。当我使用代码时,当我上传文本文件时,发生的情况是:文件成功上传到数据库表,文件成功保存到存储目录,并且没有错误。但是字段 token 上发生的情况是将三个文档的所有内容的文本文件放入字段.txt token 中,当我想要的是上传一个文件时,字段 token 只存储文件的内容,而不是存储所有文件的内容。不,如何解决?我尝试过谷歌搜索,但没有找到方法。

最佳答案

解决方案:

<?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
}
}
?>

关于php - 将每个文本文件的内容保存到数据库表 mysql 中,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/40955034/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com