我有一个.dat扩展名文件,其中包含我的生物公制机器的信息。现在我想将该文件上传到我的服务器,并希望将其数据导入mysql数据库。
我想使用CodeIgniter的文件上传将数据插入到mysql数据库中。
我已经在核心php中完成了这个,但我不能在CI中这样做,所以请在这里指导我。
这是我的纯PHP代码:
<?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/";
$location =$_FILES['file'];
// 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("c:/wamp/www/codeigniter/uploads/$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($connection,"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
}
}
?>
我的新CI代码:
<?php
class Attendance extends Admin_Controller
{
public function __construct()
{
parent::__construct();
$this->load->library('upload');
$this->load->helper(array('form', 'url'));
}
public function index()
{
$this->data['subview'] = 'admin/attendance/index';
$this->load->view('admin/_layout_main', $this->data);
}
public function upload()
{ 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/";
$location = $_FILES['file'];
$new_size = $file_size / 1024; // new file size in KB
$new_file_name = strtolower($file);
$final_file = str_replace(' ', '-', $new_file_name); // make file name in lower case
if (move_uploaded_file($file_loc, $folder . $final_file)) {
//Prepare upload data
$upload_data = Array(
'file' => $final_file,
'type' => $file_type,
'size' => $new_size
);
//Insert into tbl_uploads
$this->db->insert('daily_data2', $upload_data);
$handle = fopen("c:/wamp/www/codeigniter/uploads/$file", "r");
if ($handle) {
while (($line = fgets($handle)) !== false) {
$lineArr = explode("\t", "$line");
// 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;
$daily_data = Array(
'emp_id' => $emp_id,
'date_data' => $date_data,
'abc' => $abc,
'def' => $def,
'entry' => $entry,
'ghi' => $ghi
);
//Insert data
$this->db->insert('daily_data2', $daily_data);
}
fclose($handle);
}
//Alert success redirect to ?success
$this->alert('successfully uploaded', 'index.php?success');
} else {
//Alert error
$this->alert('error while uploading file', 'index.php?fail');
}
}
}
protected function alert($text, $location) {
return "<script> alert('".$text."'); window.location.href='".$location."'; </script>";
}
}
CI上传表格:
<h2>Upload CSV To Import Users</h2>
<form method="post" action="<?php echo site_url('admin/attendance/upload');?>" enctype="multipart/form-data">
<input type="file" name="userfile" ><br><br>
<input type="submit" name="submit" value="UPLOAD" class="btn btn-primary">
</form>
答案 0 :(得分:2)
这是未经测试但你可以得到这个想法。创建一个新的Controller文件名Import.php并发布此代码:
<?php if ( ! defined('BASEPATH')) exit('No direct script access allowed');
class Attendance extends Admin_Controller
{
public function __construct()
{
parent::__construct();
$this->load->library('upload');
$this->load->helper(array('form', 'url'));
}
public function index()
{
$this->load->view('admin/_layout_main');
}
public function upload()
{
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/";
$location = $_FILES['file'];
$new_size = $file_size / 1024; // new file size in KB
$new_file_name = strtolower($file);
$final_file = str_replace(' ', '-', $new_file_name); // make file name in lower case
if (move_uploaded_file($file_loc, $folder . $final_file)) {
//Prepare upload data
$upload_data = Array(
'file' => $final_file,
'type' => $file_type,
'size' => $new_size
);
//Insert into tbl_uploads
$this->db->insert('daily_data2', $upload_data);
$handle = fopen("c:/wamp/www/codeigniter/uploads/$file", "r");
if ($handle) {
while (($line = fgets($handle)) !== false) {
$lineArr = explode("\t", "$line");
// 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;
$daily_data = Array(
'emp_id' => $emp_id,
'date_data' => $date_data,
'abc' => $abc,
'def' => $def,
'entry' => $entry,
'ghi' => $ghi
);
//Insert data
$this->db->insert('daily_data2', $daily_data);
}
fclose($handle);
}
//Alert success redirect to ?success
$this->alert('successfully uploaded', 'index.php?success');
} else {
//Alert error
$this->alert('error while uploading file', 'index.php?fail');
}
}
}
protected function alert($text, $location) {
return "<script> alert('".$text."'); window.location.href='".$location."'; </script>";
}
}
查看更新...
<h2>Upload CSV To Import Users</h2>
<!-- in the action you need to place /controller/function in our case @Attendance, @upload -->
<form method="post" action="<?php echo site_url('attendance/upload');?>" enctype="multipart/form-data">
<input type="file" name="file" id="file">
<p>
<input type="submit" name="btn-upload" value="btn-upload" id="btn-upload" class="btn btn-primary">
</form>
导航到您的域/导入 看看你得到了什么。祝你好运