PHP:上传文本文件并将内容保存在数据库中

时间:2016-03-15 08:04:21

标签: php mysql

我是php新手,希望你们能帮我解决这个问题..

我有一个报告文件,我必须上传,然后将内容保存到数据库..我已成功上传但未能将其保存在具有单独字段的数据库中..我只能将其上传到1个字段< / p>

文本文件的内容是这样的(使用FIXED FORMAT):

的text.txt

2010000130QUEEN CONSOLIDATED            45119700005794493080171527OLIVER                              800000.0029/01/1622:27:069501
2010000130QUEEN CONSOLIDATED            45119700008408176500068670QUEEN                               500000.0029/01/1622:07:30373L

根据以上所述,前3列为no,后7为代码,30为companyname,16为userid,10为usernumber,35为name,10为trans,8为日期,8为时间和最后是位置..

我尝试了很多代码,但我认为这更接近我的需要。代码如下所示

upload.php的

&#13;
&#13;
<?php include "header.php";

?>
    
  <form action="do_upload.php" method="post" enctype="multipart/form-data">
        	<table width="409" height="199" border="0" align="center">
<tr>
                	<td width="161">Upload File (.txt only)</td>
        <td width="14">:</td>
        <td width="218"><input type="file" name="upload"></td>
              </tr>
                <tr>
                	<td colspan="3"><button type="submit">Upload</button></td>
                </tr>
                <tr>
                	<td colspan="3">
						<?php if(!empty($_GET['msg'])){
							echo $_GET['msg'];}
						?>
                    </td>
                </tr>
            </table>
      </form>
        
<?php include "footer.php";?>
&#13;
&#13;
&#13;

这是我的PHP代码,它的工作原理如下。

do_upload.php

&#13;
&#13;
<?php include "../connection.php";

	$location_file=$_FILES['upload']['tmp_name'];
$name_file=$_FILES['upload']['name'];
$type_file=$_FILES['upload']['type'];
$size_file=$_FILES['upload']['size'];
$content = file_get_contents($_FILES['upload']['tmp_name']);
$handle = fopen($_FILES['upload']['tmp_name'],"r");
$lines = explode("\n", $content);

if($location_file==""){
	header("location:upload.php?msg=Please Choose the File");
}else if ($type_file == '.txt'){
	header("location:upload.php?msg=Upload file with .txt extension only");
}else if($size_file > 104857600){
	header("location:upload.php?msg=Max File Size is 100MB");
}
else{
	
	$data = array();
	while (false !== ($line = fgets($handle)))
	{
		$data[] = array(
			'no' => trim(substr($line, 0, 3)),
			'code' => trim(substr($line,3,7)),  
			'companyname' => trim(substr($line, 10, 30)),
			'userid' => trim(substr($line, 40, 16)),
			'usernumber' => trim(substr($line, 56, 10)),
			'name' => trim(substr($line, 66, 35)),
			'trans' => trim(substr($line, 101, 10)),
			'date' => trim(substr($line, 111, 8)),
			'time' => trim(substr($line,119,8)),
			'location' => trim(substr($line,127))
		);
	}
	fclose($handle);
	
	foreach($data as $row){
		$sql="INSERT INTO report (no, code, companyname, userid, usernumber, name, trans, date, time, location) VALUES ('{$row[0]}','{$row[1]}','{$row[2]}','{$row[3]}','{$row[4]}','{$row[5]}','{$row[6]}','{$row[7]}','{$row[8]}','{$row[9]}')";
		if (!mysqli_query($connect,$sql))
		{
		  die('Error: ' . mysqli_error($connect));
		}
	}
	mysqli_close($connect);
	header("location:upload.php?msg=Upload Success! Please View it at 'VIEW REPORT' Menu");
        }
?>
&#13;
&#13;
&#13;

由于我是php的新手,我想我应该使用substr()函数,因为会有一个空格我会使用trim(),因为这是一个固定的格式。

问题是在我上传文本后,它会在数据库中插入一个空白值。我真的不知道该怎么做

请指导我如何去做。我还是php的新手,所以任何帮助都会受到高度赞赏..谢谢!

UPDATE ..数组正常工作,但未在数据库中添加值

&#13;
&#13;
$data[] = array();
		$i = 0;
		while (false !== ($lines = fgets($handle)))
		{
			$data[$i] = array(
		     'no' => trim(substr($line, 0, 3)),
		     'code' => trim(substr($line,3,7)),  
		     'companyname' => trim(substr($line, 10, 30)),
		     'userid' => trim(substr($line, 40, 16)),
		     'usernumber' => trim(substr($line, 56, 10)),
		     'name' => trim(substr($line, 66, 35)),
		     'trans' => trim(substr($line, 101, 10)),
		     'date' => trim(substr($line, 111, 8)),
		     'time' => trim(substr($line,119,8)),
		     'location' => trim(substr($line,127))
	       );
			$i++;
		}
		fclose($handle);
		
        // THIS IS THE PROBLEM,if this not commented then the loop will not work
		/*foreach($data as $data){
			$sql="INSERT INTO report (no, code, companyname, userid, usernumber, name, trans, date, time, location) VALUES ('{$data[0]}','{$data[1]}','{$data[2]}','{$data[3]}','{$data[4]}','{$data[5]}','{$data[6]}','{$data[7]}','{$data[8]}','{$data[9]}')";
			if (!mysqli_query($connect,$sql))
			{
			  die('Error: ' . mysqli_error($connect));
			}
		}*/
		
		//echo "<pre>";
		//print_r($data);    Checking the values
		//echo "</pre>";
                mysqli_close($connect);
                header("location:upload.php?msg=Upload Success!");
&#13;
&#13;
&#13;

现在我的&#39; substr()&#39;函数有效,唯一的问题是当我将值插入数据库时​​我无法循环,我显然不知道..当我可以插入值时,它会像这样插入

Image inside of phpmyadmin Database

2 个答案:

答案 0 :(得分:0)

您是否检查了$ data数组中的元素值。 你可以使用print_r($ data)来查看数组元素&amp;值。

谢谢,

答案 1 :(得分:-1)

$sql = "INSERT INTO report (no, code, companyname, userid, usernumber, name, trans, date, time, location) VALUES ('{$row['no']}','{$row['code']}','{$row['companyname']}','{$row['userid']}','{$row['usernumber']}','{$row['name']}','{$row['trans']}','{$row['date']}','{$row['time']}','{$row['location']}')";