Solved

reading file on disk and inserting into mysql.

Posted on 2002-05-28
2
197 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
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

Instantly Create Instructional Tutorials

Contextual Guidance at the moment of need helps your employees adopt to new software or processes instantly. Boost knowledge retention and employee engagement step-by-step with one easy solution.

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…
This article discusses how to create an extensible mechanism for linked drop downs.
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 create and use a small PHP class to apply a watermark to an image. This video shows the viewer the setup for the PHP watermark as well as important coding language. Continue to Part 2 to learn the core code used in creat…

724 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