Import from csv file to MySQL

Collapse
This topic is closed.
X
X
 
  • Time
  • Show
Clear All
new posts
  • Smitro

    Import from csv file to MySQL

    Hi,

    I'm in the middle of writing a script to import a csv file into mySQL
    (as apart of a bigger project)

    I need to know what are the best steps to go from $_FILES to file().

    Once the file is read into an array I should be right. I don't want to
    keep a copy of the file uploaded either, as once it is imported I will
    have no use for it.

    I'm not sure, do I copy it store it then use it or is there a faster
    way? Will the temp file be deleted or do I have to delete it?

  • chrislive

    #2
    Re: Import from csv file to MySQL

    I am at work so I cant give you the code but what you need to do is the
    following:

    1. upload to a temporary directory (say /temp - this folder will need
    to have appropriate access rights)

    2. use the file_read funcions (look for tutorials on flat files for all
    this)

    3. import your data into the DB

    4. If successful use unlink($file); to delete OR print error message

    Thanks
    Chris

    Smitro wrote:[color=blue]
    > Hi,
    >
    > I'm in the middle of writing a script to import a csv file into mySQL
    > (as apart of a bigger project)
    >
    > I need to know what are the best steps to go from $_FILES to file().
    >
    > Once the file is read into an array I should be right. I don't want to
    > keep a copy of the file uploaded either, as once it is imported I will
    > have no use for it.
    >
    > I'm not sure, do I copy it store it then use it or is there a faster
    > way? Will the temp file be deleted or do I have to delete it?[/color]

    Comment

    • Smitro

      #3
      Re: Import from csv file to MySQL

      I think I've worked it out, I'm in the middle of writing the script.

      I upload the file into $_FILES

      grab the temp directory and temp file name etc from $_FILES

      then run the mysql_query () something along the lines of "LOAD DATA
      INFILE '$file_location ' INTO TABLE tale_name etc...

      I guess it's one of these things, if you look hard enough you'll find
      an answer.

      Thanks anyway.

      Comment

      Working...