Import CSV File To Server And Upload Data As Stream In Mysql Using Php

 
To allow users to upload files from a form can be very useful.This script will allow you to upload files from your browser to your hosting, using PHP. The first thing we need to do is create an HTML form that allows people to choose the file they want to upload.This form sends data to the file "upload.php", which is what we will be creating next to actually upload the file.

  • The enctype attribute of the <form> tag specifies which content-type to use when submitting the form. "multipart/form-data" is used when a form requires binary data, like the contents of a file, to be uploaded
 
  • The type="file" attribute of the <input> tag specifies that the input should be processed as a file. For example, when viewed in a browser, there will be a browse-button next to the input field

 

  • Here we are attaching CSV File which can contain data like bellow

Name

Description

Price($)

shipping($)

quantity

Pen Drive

Which made by …

25

1.23

25

paper

Which is made by…

2.5

0

2000

Similarly we can have many number of rowed data.Therefore we should create the Mysql Data base which should contain the table column names as Above.










Index.php
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<title>IndexFile</title>
</head>
<body>
<form enctype="multipart/form-data" action="uploader.php" method="POST">
<input type="hidden" name="MAX_FILE_SIZE" value="1000000" />
Choose a file to upload: <input name="uploadedfile" type="file" />
<input type="submit" value="Upload File" />
</form>
<form enctype="multipart/form-data" action="process.php" method="POST">
<input type="hidden" name="MAX_FILE_SIZE" value="1000000" />
Select File : <input name="uploadedfile" type="file" />
<input type="submit" value="Process Data" />
</form>
</body>
</html>


Upload.php


<?php
// Where the file is going to be placed 
$target_path = "C:/wamp/www/Release/uploads/";
echo $target_path . basename( $_FILES['uploadedfile']['name']); 
/* Add the original filename to our target path. 
Result is "uploads/filename.extension" */
$target_path = $target_path . basename( $_FILES['uploadedfile']['name']); 
if(move_uploaded_file($_FILES['uploadedfile']['tmp_name'], $target_path)) {
echo "The file ". basename( $_FILES['uploadedfile']['name']). 
" has been uploaded"; 
} else{
echo "There was an error uploading the file, please try again!";
}
header('Location: http://localhost/Release/');
?>


By using the global PHP $_FILES array you can upload files from a client computer to the remote server.


The first parameter is the form's input name and the second index can be either "name", "type", "size", "tmp_name" or "error".


 
process.php

<?php
//connect to localhost
$con = mysql_connect("localhost","root","");
if (!$con)
{
die('Could not connect: ' . mysql_error());
}
//connect to Database
mysql_select_db("cart", $con);
//File path to CSV
$filePath = "C:/wamp/www/Release/uploads/";
$filePath = $filePath . basename( $_FILES['uploadedfile']['name']); 
echo $filePath;
//read the CSV file to Stream
$row = 1;
if (($handle = fopen("$filePath", "r")) !== FALSE) {
    while (($data = fgetcsv($handle, 1000, ",")) !== FALSE) {
    $num = count($data);
    $row++;
    $name=$data[0];
    $des=$data[1];
    $pri=$data[2];
    $ship=$data[3];
    $quan=$data[4];
//input the red data from CSV in to Database
mysql_query("INSERT INTO products (name,description,price,shipping,quantity)
VALUES ('$data[0]','$data[1]','$data[2]','$data[3]','$data[4]')");
} 
fclose($handle);
}
//To redirect to index page
header('Location: http://localhost/Release/');
mysql_close($con);
?>



Have Fun With PHP «

11 comments:

  1. can you pls update the same snippet with nodejs-html/view page?

    Thank you

    ReplyDelete
  2. I would really like to read some personal experiences like the way, you've explained through the above article. I'm glad for your achievements and would probably like to see much more in the near future. Thanks for share.
    Selenium training in Chennai

    Selenium training in Bangalore

    ReplyDelete
  3. I really appreciate this post. I’ve been looking all over for this! Thank goodness I found it on Bing. You’ve made my day! Thx again!
    python Training institute in Pune
    python Training institute in Chennai
    python Training institute in Bangalore

    ReplyDelete
  4. Woah this blog is wonderful i like studying your posts. Keep up the great work! You understand, lots of persons are hunting around for this info, you could help them greatly.
    Data Science Training in Chennai
    Data Science course in anna nagar
    Data Science course in chennai
    Data science course in Bangalore
    Data Science course in marathahalli
    Data science course in bangalore

    ReplyDelete
  5. Thank you so much for the great and very beneficial stuff that you have shared with the world.

    Upgrade your career Learn Data Warehousing Training in Bangalore from industry experts get Complete hands-on Training, Interview preparation, and Job Assistance at Softgen Infotech.

    ReplyDelete
  6. I read this blog,Thanks for sharing this information.

    Devops training in pune

    ReplyDelete

  7. I recently came across your article and have been reading along. I want to express my admiration of your writing skill and ability to make readers read from the beginning to the end.
    DevOps Training in Pune

    ReplyDelete
  8. Employees in a DevOps culture understand that the relevance of their job extends beyond merely meeting their personal goals. Instead of one team committed to development and another to operations and optimization, a DevOps-enabled organisation would have mixed groups dedicated to maximising the value of end products and assisting their colleagues in every way they can. Essentially, responsibility for the project is divided so that everyone participating may give their utmost. for more info visit: Devops Classes in Pune

    ReplyDelete
  9. Nice information.Thanks for sharing such a valuable post.
    SQL Classes in Pune

    ReplyDelete