forked from wdaweb/113php-2-11304-PHP_FILE_AND_GRAPHIC
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathtext-import.php
85 lines (72 loc) · 1.76 KB
/
text-import.php
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
<style>
table{
border-collapse:collapse;
border-spacing:0;
border:1px solid #ccc;
margin:10px 0;
}
table th,table td{
border:1px solid #ccc;
padding:5px;
}
.header{
text-align:center;
}
</style>
<?php
/****
* 1.建立資料庫及資料表
* 2.建立上傳檔案機制
* 3.取得檔案資源
* 4.取得檔案內容
* 5.建立SQL語法
* 6.寫入資料庫
* 7.結束檔案
*/
if(!empty($_FILES['file'])){
move_uploaded_file($_FILES['file']['tmp_name'], "./files/{$_FILES['file']['name']}");
echo $_FILES['file']['name']."上傳成功";
getfile("./files/{$_FILES['file']['name']}");
}
function getfile($path){
$file=fopen($path,'r');
$line=fgets($file);
$cols=explode(",",trim($line));
echo "<table>";
echo "<tr class='header'>";
foreach($cols as $col){
echo "<th>{$col}</th>";
}
echo "</tr>";
while($line=fgets($file)){
$cols=explode(",",trim($line));
echo "<tr>";
foreach($cols as $col){
echo "<td>{$col}</td>";
}
echo "</tr>";
}
echo "</table>";
}
?>
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<meta http-equiv="X-UA-Compatible" content="ie=edge">
<title>文字檔案匯入</title>
<link rel="stylesheet" href="style.css">
</head>
<body>
<h1 class="header">文字檔案匯入練習</h1>
<!---建立檔案上傳機制--->
<form action="?" method="post" enctype="multipart/form-data">
<label for="file">文字檔:</label><input type="file" name="file" id="file">
<input type="submit" value="上傳">
</form>
<!----讀出匯入完成的資料----->
<?php
?>
</body>
</html>