您好我正在尝试通过上传文本文件将数据导入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);
}
?>
答案 0 :(得分:1)
显然开放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
}
}
?>