Solved

reading file on disk and inserting into mysql.

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

Highfive Gives IT Their Time Back

Highfive is so simple that setting up every meeting room takes just minutes and every employee will be able to start or join a call from any room with ease. Never be called into a meeting just to get it started again. This is how video conferencing should work!

Join & Write a Comment

Suggested Solutions

Developers of all skill levels should learn to use current best practices when developing websites. However many developers, new and old, fall into the trap of using deprecated features because this is what so many tutorials and books tell them to u…
Nothing in an HTTP request can be trusted, including HTTP headers and form data.  A form token is a tool that can be used to guard against request forgeries (CSRF).  This article shows an improved approach to form tokens, making it more difficult to…
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…
The viewer will learn how to count occurrences of each item in an array.

744 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

13 Experts available now in Live!

Get 1:1 Help Now