gpt4 book ai didi

php - 如何将已上传的同一个文件导入到mysql中?

转载 作者:行者123 更新时间:2023-11-28 23:41:40 24 4
gpt4 key购买 nike

您好,我正在尝试通过上传文本文件将数据导入 mysql。我已经在不同的文件中编写了文件上传和 mysql 导入代码,现在我正在尝试合并我的代码,我想上传一个文件,同时希望我的代码将其数据导入 mysql 数据库。我尝试在上传后粘贴导入代码并将 fopen('static-file') 替换为 fopen('$FILES_["file"],"r"')。但这没有用。

请告诉我如何同时上传和导入。

下面是我的上传代码:

<?php
include_once 'dbconfig.php';
if(isset($_POST['btn-upload']))
{

$file = rand(1000,100000)."-".$_FILES['file']['name'];
$file_loc = $_FILES['file']['tmp_name'];
$file_size = $_FILES['file']['size'];
$file_type = $_FILES['file']['type'];
$folder="uploads/";

// new file size in KB
$new_size = $file_size/1024;
// new file size in KB

// make file name in lower case
$new_file_name = strtolower($file);
// make file name in lower case

$final_file=str_replace(' ','-',$new_file_name);

if(move_uploaded_file($file_loc,$folder.$final_file))
{
$sql="INSERT INTO tbl_uploads(file,type,size) VALUES('$final_file','$file_type','$new_size')";
mysqli_query($connection,$sql);


?>
<script>
alert('successfully uploaded');
// window.location.href='index.php?success';
</script>
<?php


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

}

?>

这是我的 MYSQL 导入代码:

    <?php
$con=mysqli_connect("localhost","root","","hiren");
// Check connection
if (mysqli_connect_errno())
{
echo "Failed to connect to MySQL: " . mysqli_connect_error();
}


$handle = fopen("./uploads/imp.dat", "r");
if ($handle) {
while (($line = fgets($handle)) !== false) {

$lineArr = explode("\t", "$line");
var_dump($lineArr); // to make sure array is ok

// instead assigning one by onb use php list -> http://php.net/manual/en/function.list.php
list($emp_id, $date_data, $abc, $def, $entry, $ghi) = $lineArr;

// and then insert data
mysqli_query($con,"INSERT INTO `daily_data2` (emp_id, date_data, abc, def, entry, ghi)
VALUES ('$emp_id', '$date_data', '$abc', '$def', '$entry', '$ghi')");
}

fclose($handle);
}

?>

最佳答案

显然打开 fopen('$FILES_["file"],"r"') 是行不通的,因为那是一个数组(假设你的意思是 $_FILES[ "file"] 当然。)即使你尝试打开 $_FILES["file"]["tmp_name"] 它也不会工作——你认为 move_uploaded_file() 是什么在做什么?

<?php
include_once 'dbconfig.php';
if(isset($_POST['btn-upload'])) {
$file = rand(1000,100000)."-".$_FILES['file']['name'];
$file_loc = $_FILES['file']['tmp_name'];
$file_size = $_FILES['file']['size'];
$file_type = $_FILES['file']['type'];
$folder="uploads/";

// new file size in KB
$new_size = $file_size/1024;
// new file size in KB

// make file name in lower case
$new_file_name = strtolower($file);
// make file name in lower case

$final_file=str_replace(' ','-',$new_file_name);

if(move_uploaded_file($file_loc,$folder.$final_file)) {
$sql="INSERT INTO tbl_uploads(file,type,size) VALUES('$final_file','$file_type','$new_size')";
mysqli_query($connection,$sql);

$handle = fopen($folder.$final_file, "r");
if ($handle) {
while (($line = fgets($handle)) !== false) {
$lineArr = explode("\t", "$line");
var_dump($lineArr); // to make sure array is ok
// instead assigning one by onb use php list -> http://php.net/manual/en/function.list.php
list($emp_id, $date_data, $abc, $def, $entry, $ghi) = $lineArr;

// and then insert data
mysqli_query($con,"INSERT INTO `daily_data2` (emp_id, date_data, abc, def, entry, ghi)
VALUES ('$emp_id', '$date_data', '$abc', '$def', '$entry', '$ghi')");
}
fclose($handle);
}
?>
<script>
alert('successfully uploaded');
// window.location.href='index.php?success';
</script>
<?php
}
else {
?>
<script>
alert('error while uploading file');
window.location.href='index.php?fail';
</script>
<?php
}
}
?>

关于php - 如何将已上传的同一个文件导入到mysql中?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/34261389/

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