gpt4 book ai didi

php - 如何传递文件以动态导入?

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

您好,我正在尝试将文件导入到我的 SQL 数据库中。我有一个上传文件的工作脚本。我还有一个脚本,允许我从静态文件导入数据[我的意思是我已经静态定义了该文件的路径]。

现在我可以将文件导入到我上传的 sql 中..

我的导入脚本是:

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


$f = fopen("./uploads/imp.txt", "r");
while(!feof($f)) {
$data = explode(" ", fgets($f));

$emp_id = $data[0];
$date_data = $data[1];
$abc = $data[2];
$def = $data[3];
$entry = $data[4];
$ghi = $data[5];

$result= mysqli_query($con,"INSERT INTO `daily_data2` (emp_id, date_data, abc, def, entry, ghi)
VALUES ('$emp_id', '$date_data', '$abc', '$def', '$entry', '$ghi')") or die(mysql_error());


print_r($data);

if($result)

{
//echo "success";
}
}

fclose($f);

?>

上传脚本是:

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

如何导入我已上传的同一文件?

查看文件脚本:

<?php
include_once 'dbconfig.php';
?>
<!DOCTYPE html>
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<title>Upload the Data</title>
<link rel="stylesheet" href="style.css" type="text/css" />
</head>
<body>
<div id="header">
<label>File Uploading With PHP and MySql</label>
</div>
<div id="body">
<table width="80%" border="1">
<tr>
<th colspan="4">your uploads...<label><a href="index.php">upload new files...</a></label></th>
</tr>
<tr>
<td>File Name</td>
<td>File Type</td>
<td>File Size(KB)</td>
<td>Import File</td>
</tr>
<?php

$sql="SELECT * FROM tbl_uploads";

$result_set= mysqli_query($connection,$sql);

while($row=mysqli_fetch_array($result_set))
{
?>
<tr>
<td><?php echo $row['file'] ?></td>
<td><?php echo $row['type'] ?></td>
<td><?php echo $row['size'] ?></td>
<td><a href="uploads/<?php echo $row['file'] ?>" target="_blank">view file</a></td>
</tr>
<?php
}
?>
</table>

</div>
</body>
</html>

最佳答案

使用 LOAD DATA INFILE 在 MySql 中导入文件询问。

    $file_path = "<<Full name with path>>";

$sql = "LOAD DATA INFILE '".$file_path."' INTO TABLE daily_data2
FIELDS TERMINATED BY ' ' ENCLOSED BY '\"'
LINES TERMINATED BY '\\r\\n'
(emp_id, date_data, abc, def, entry, ghi)";

mysqli_query($con,$sql);

关于php - 如何传递文件以动态导入?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/34219769/

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