A table contains a listed of data and upload form, user the be able to upload the file, and also we create a php function for the file upload handling, validation and etc.
Step 1: Create a Database
First create a database named ‘import_csv’ you can change if you want, and create a table named ‘csv_data’ and fields, and insert some data.
1 | CREATE TABLE IF NOT EXISTS `csv_data` ( |
2 | `id` int(11) NOT NULL AUTO_INCREMENT, |
3 | `product_title` varchar(255) NOT NULL, |
4 | `product_price` varchar(255) NOT NULL, |
Step 2: Create Database Connection
Create file name ‘database.php’ to connect from the database we have assigning the database credentials, MySQL host, user, password and database name the ‘import_csv’.
2 | $db = mysql_connect( 'localhost' , 'root' , '' ); |
3 | if (! $db ) { echo mysql_error(); } |
4 | $select_db = mysql_select_db( 'import_csv' ); |
5 | if (! $select_db ) { echo mysql_error(); } |
Step 3: Creating the Page Template
Create a file name ‘index.php’ and copy the code below. In the index page, first require the database connection and query the added sample product in the table, and upload form set the attribute enctype multipart for upload handling.
index.php
1 | <?php require_once ( "database.php" ); ?> |
6 | <title>Importing Data with CSV | istockphp.com</title> |
7 | <link href= "style/style.css" rel= "stylesheet" type= "text/css" media= "all" /> |
11 | <div id= "wrap" align= "center" > <!--wrap start--> |
12 | <h1>Importing Data with CSV</h1> |
13 | <table id= "table_data" > |
19 | <?php $query = mysql_query( "SELECT `id`, `product_title`, `product_price` FROM `csv_data`" ); |
20 | while ( $row = mysql_fetch_array( $query )) { |
23 | <td><?php echo $row [ 'id' ]; ?></td> |
24 | <td><?php echo $row [ 'product_title' ]; ?></td> |
25 | <td><?php echo $row [ 'product_price' ]; ?></td> |
27 | <?php } unset( $row ); ?> |
30 | <form action= "import.php" method= "post" enctype= "multipart/form-data" > |
31 | <input name= "cvs" type= "file" /> |
32 | <input name= "upload" type= "submit" value= "Upload CSV" /> |
35 | if (isset( $_GET [ 'success' ])) { |
36 | echo '<span class="msg">' . $_GET [ 'count' ] . ' Data Successfully Imported.' . '</span>' ; |
39 | </div> <!--wrap end --> |
A simple style for the page.
style.css
2 | font-family : Arial , Helvetica , sans-serif ; |
8 | font-family : "Helvetica Neue" , Helvetica , Arial , sans-serif ; |
18 | table#table_data tr:first-child { |
20 | text-transform : uppercase |
22 | table#table_data tr td { |
23 | border-bottom : 1px solid #CCCCCC ; |
26 | table#table_data tr:first-child { |
31 | background : url (correct.png) no-repeat right ; |
Step 4: The Import PHP Script
import.php
2 | require_once ( "database.php" ); |
6 | $file_name = $_FILES [ 'cvs' ][ 'name' ]; |
7 | $file_type = $_FILES [ 'cvs' ][ 'type' ]; |
8 | $file_temp_loc = $_FILES [ 'cvs' ][ 'tmp_name' ]; |
9 | $file_error_msg = $_FILES [ 'cvs' ][ 'error' ]; |
10 | $file_size = $_FILES [ 'cvs' ][ 'size' ]; |
14 | echo "Error: please browse for a file before clicking the upload button." ; |
17 | if (!preg_match( "/\.(csv)$/i" , $file_name )) { |
18 | echo 'Error: your file is not CSV.' ; |
19 | @unlink( $file_temp_loc ); |
22 | if ( $file_size > 5242880) { |
23 | echo "Error: you file was larger than 5 Megabytes in size." ; |
26 | if ( $file_error_msg == 1) { |
27 | echo "Error: an error occured while processing the file, try agian." ; |
31 | $move_file = move_uploaded_file( $file_temp_loc , "upload/{$file_name}" ); |
32 | if ( $move_file != true) { |
33 | echo 'Error: File not uploaded, try again.' ; |
34 | @unlink( $file_temp_loc ); |
38 | $csvFile = 'upload/' . $file_name ; |
39 | $csvFileLength = filesize ( $csvFile ); |
41 | $handle = fopen ( $csvFile , 'r' ); |
44 | while ( $data = fgetcsv ( $handle , $csvFileLength , $csvSeparator )) { |
45 | $count += count ( $data [0]); |
46 | mysql_query( "INSERT INTO `csv_data` (`product_title`, `product_price`) VALUES ( '$data[0]', '$data[1]' )" ); |
51 | header( 'Location: index.php?success=1&count=' . $count ); |
In the php import script we require the database connection first. If the user upload the file, the $_FILES assigns the variables, and we put in the upload validation. After the validation we put in the move uploaded file function to transferred the csv to the server, we will use the fopen function for open the file and get contents and then perform a while loop with mysql query, after performing the query we put in the unlink to delete the cvs file in server for saving space.
Step 5: Complete
Were done, we created a php web application can be able to import data to the database using csv. Let’s have a look at what we’ve achieved:
- We’ve set up a database.
- We’ve create page to fetch the data.
- We’ve create a file upload handling with validation.
- We’ve able to count the uploaded data.
- We’ve able to delete the cvs after imported.
No comments:
Post a Comment