Solved

reading file on disk and inserting into mysql.

Posted on 2002-05-28
2
193 Views
Last Modified: 2008-03-17
Hello everybody.
I want to have an application on the internet
that would read files on disk from users and insert into mysql database.

The file on disk from users would be many row and many field.
for example
3 field seperated by "|" (if that is a problem I can change it to what is needed)
and each row is seperated by carriage return

example:
surname|given1|given2
jeremy|ron|star
pamela|anderson|mary
locklear|heather|babealishious

so I want a user to insert disk and press
a button that would load these values in my table
called person.
100 points...
0
Comment
Question by:jerbell
2 Comments
 
LVL 5

Accepted Solution

by:
dkjariwala earned 100 total points
ID: 7039187
Check the comments.

<?php

/// assuming name of your field field is userfile

if (is_uploaded_file($userfile)) {
    copy($userfile, "/place/to/put/uploaded/file");
} else {
    echo "Possible file upload attack: filename '$userfile'.";
}

move_uploaded_file($userfile, "/place/to/put/uploaded/file");

//now read that uploaded file.
$lines = file($userfile);

//connect to mysql.
$conn = mysql_connect('localhost','root','');

foreach($lines as $line)
{
     //read lines.
     $fields = explode('|',$line);
     $surname = trim($fields[0]);
     $given1 = trim($fields[1]);
     $given2 = trim($fields[2]);

     if(! get_magic_quotes_gpc())
     {
          //addslashes if magic quotes is NOT enabled.
          $surname = addslashes($surname);
          $given1 = addslashes($given1);
          $given2 = addslashes($given2);
     }
     //make query.
     $query = "insert into PERSON (surname,given1,given2) values ('$surname','$given1','$given2')";
     print $query;
     //execute it.
     $result = mysql_query($query) or die('can not execute query. reason : '. mysql_error());
}



?>

JD
0
 
LVL 2

Expert Comment

by:vmv
ID: 7039266
Also, check out the following link:

http://www.mysql.com/doc/L/O/LOAD_DATA.html
0

Featured Post

Free Tool: SSL Checker

Scans your site and returns information about your SSL implementation and certificate. Helpful for debugging and validating your SSL configuration.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Suggested Solutions

Title # Comments Views Activity
PHP strings vs array errors 13 32
Dynamic Dropdowns 15 32
simple html dom php accessing table/cell  values 4 19
Reference key in foreach loop 4 18
This article will explain how to display the first page of your Microsoft Word documents (e.g. .doc, .docx, etc...) as images in a web page programatically. I have scoured the web on a way to do this unsuccessfully. The goal is to produce something …
Foreword (July, 2015) Since I first wrote this article, years ago, a great many more people have begun using the internet.  They are coming online from every part of the globe, learning, reading, shopping and spending money at an ever-increasing ra…
Explain concepts important to validation of email addresses with regular expressions. Applies to most languages/tools that uses regular expressions. Consider email address RFCs: Look at HTML5 form input element (with type=email) regex pattern: T…
The viewer will learn how to create a basic form using some HTML5 and PHP for later processing. Set up your basic HTML file. Open your form tag and set the method and action attributes.: (CODE) Set up your first few inputs one for the name and …

856 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question