SitePoint Sponsor

User Tag List

Results 1 to 6 of 6

Thread: upload csv file

  1. #1
    SitePoint Member meow's Avatar
    Join Date
    Oct 2001
    Posts
    21
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)

    upload csv file

    I have a CSV file, comma delimited which automatically uploaded to a database. Its all running smoothly.

    How can i avoid/filter if someone accidently upload rubbish file(non csv formatted file) or it will ruin MySQL.

    Please help.

  2. #2
    epsilon transition cupid's Avatar
    Join Date
    Aug 2001
    Location
    Kent, Ohio
    Posts
    367
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Do you have some sort of script that automatically calls mysqlimport or some other means of importing the delimited file into the database automatically? If so, can you paste some code? If not, can you share with us your procedure or just change the password or permission for that user?

  3. #3
    SitePoint Member meow's Avatar
    Join Date
    Oct 2001
    Posts
    21
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)

    my code

    Here is my code for upload csv file.

    $filename = ($userfile_name);
    copy($userfile, "adduser.txt");
    include "./common_dblist.inc";
    $link_id = mysql_connect($dbhost, $dbusername, $dbuserpassword);
    mysql_select_db("$default_dbname", $link_id);

    $fp = fopen ($userfile,"r");

    while ($data = fgetcsv ($fp, 2500, ",")) {
    $num = count ($data);
    for ($c=0; $c<$num; $c++) {
    $userid = $data[0];
    $fullname = ereg_replace('\'','',$data[1]);
    $password = $data[2];

    $sql = "INSERT INTO user (userid,fullname,password) ".
    "VALUES ('$userid','$fullname',password ('$password'))";
    $result = mysql_query($sql);
    $query = "INSERT INTO $usergroup VALUES('$userid')";
    $result = mysql_query($query);
    }

    Thanks.

  4. #4
    epsilon transition cupid's Avatar
    Join Date
    Aug 2001
    Location
    Kent, Ohio
    Posts
    367
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    fgetcsv() is not sophisticated enough to do error checking. By looking at your code, you might get away with checking to see if $data[0], $data[1], $data[2] are existent and throwing away the file if one of them isn't. However, this will not work if there are null values for ANY of these.

    If it were me, I'd go the easy route and put the file inside a password protected directory where only "trusted" users can access.

  5. #5
    SitePoint Member meow's Avatar
    Join Date
    Oct 2001
    Posts
    21
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    In my case the file itself inside the protected directory(admin only).
    Are you saying that there is no other way to filter it
    or there is other safe way to upload csv file?
    Thanks for the info.

  6. #6
    epsilon transition cupid's Avatar
    Join Date
    Aug 2001
    Location
    Kent, Ohio
    Posts
    367
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    In the case that we're working with a live database and a corrupted file was uploaded... that'd be tragic. I wouldn't have approached it with an automated system. But rather, I'd make a 'dormant' sister of the database, manually import it in there to make sure everything works before I update the live db. It's a bit more time consuming, but it's a small price to pay.

    Oh yeah, I guess it wouldn't hurt to keep a daily dump backup of everything either


Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •