• Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 201
  • Last Modified:

reading file on disk and inserting into mysql.

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
jerbell
Asked:
jerbell
1 Solution
 
dkjariwalaCommented:
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
 
vmvCommented:
Also, check out the following link:

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

Featured Post

Free Tool: Path Explorer

An intuitive utility to help find the CSS path to UI elements on a webpage. These paths are used frequently in a variety of front-end development and QA automation tasks.

One of a set of tools we're offering as a way of saying thank you for being a part of the community.

Tackle projects and never again get stuck behind a technical roadblock.
Join Now