Solved

reading file on disk and inserting into mysql.

Posted on 2002-05-28
2
189 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

Announcing the Most Valuable Experts of 2016

MVEs are more concerned with the satisfaction of those they help than with the considerable points they can earn. They are the types of people you feel privileged to call colleagues. Join us in honoring this amazing group of Experts.

Question has a verified solution.

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

Generating table dynamically is the most common issue faced by php developers.... So it seems there is a need of an article that explains the basic concept of generating tables dynamically. It just requires a basic knowledge of html and little maths…
I imagine that there are some, like me, who require a way of getting currency exchange rates for implementation in web project from time to time, so I thought I would share a solution that I have developed for this purpose. It turns out that Yaho…
Learn how to match and substitute tagged data using PHP regular expressions. Demonstrated on Windows 7, but also applies to other operating systems. Demonstrated technique applies to PHP (all versions) and Firefox, but very similar techniques will w…
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…

813 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

Need Help in Real-Time?

Connect with top rated Experts

8 Experts available now in Live!

Get 1:1 Help Now