将每个文本文件的内容保存到数据库中 table mysql
save each text file's contents into a database table mysql
我得到了 100 个扩展名为 .txt 的文本文件。我想将每个文件的文本文件的内容保存到 MySQL 数据库 table 中,在字段中,我给字段命名的地方是 "token"。所以我的 table 的结构是:id、文件名、标记、大小。我尝试使用以下 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
}
}
?>
为了测试,我使用了三个文本文件。当我使用代码时,当我上传发生的文本文件时:
file successfully uploaded to the database table,文件成功保存到存储目录,没有报错。但是在field token上发生的是将三个文件的所有内容的文本文件放到.txt token字段中,当我想要的是上传一个文件时,field 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
}
}
?>
我得到了 100 个扩展名为 .txt 的文本文件。我想将每个文件的文本文件的内容保存到 MySQL 数据库 table 中,在字段中,我给字段命名的地方是 "token"。所以我的 table 的结构是:id、文件名、标记、大小。我尝试使用以下 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
}
}
?>
为了测试,我使用了三个文本文件。当我使用代码时,当我上传发生的文本文件时: file successfully uploaded to the database table,文件成功保存到存储目录,没有报错。但是在field token上发生的是将三个文件的所有内容的文本文件放到.txt token字段中,当我想要的是上传一个文件时,field 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
}
}
?>