SitePoint Sponsor

User Tag List

Results 1 to 3 of 3
  1. #1
    SitePoint Evangelist jazztie's Avatar
    Join Date
    Mar 2001
    Location
    the Netherlands
    Posts
    519
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)

    How to use a uploaded csv file??

    I have a little problem. For a student-organization I want them to be able to "upload" a db-dump in csv-format to the database of the website. Direct connections between the two databases aren't possible due to security settings on campus and because the Board aren't techies (at all).

    I have figured out how to put the info from the csv-file into an array and to insert into the database:
    PHP Code:
    $csv_filename  "LabyrintMeuk.csv"
    $conn=mysql_connect($mysql["server"], $mysql["username"], $mysql["password"]);
    mysql_select_db($mysql["database"], $conn);
    print 
    "<table border=0 bgcolor='#000000' cellspacing=1 cellpadding=1>
     <tr bgcolor='#6699CC'><td>.</td><td>naam</td><td>email</td><td>geboortedatum</td><td align='center'>beginjaar</td></tr>"
    ;
    $fp=fopen($csv_filename'r');
    $idnum '1';
    while(!
    feof($fp)) {
        
        
    $row=fgets($fp4096);
        
    $data=explode(";"$row);
     
        
    $voornaam   str_replace("\"","",$data[0]); 
        
    $tussenvoegsel  str_replace("\"","",$data[1]);
        
    $achternaam  str_replace("\"","",$data[2]);
        
    $email  str_replace("\"","",$data[3]);
        
    $geboortedatum  str_replace("\"","",$data[4]);
        
    $beginjaar   str_replace("\"","",$data[5]);
        
        print 
    "<tr bgcolor='#FFFFFF'>
          <td>
    $idnum </td>
          <td>
    $voornaam $tussenvoegsel $achternaam</td>
          <td>
    $email</td>
          <td>
    $geboortedatum</td>
          <td align='center'>
    $beginjaar </td></tr>";
     
    /*  $sql="insert into ipcountry ".
         "(`".$mysql["table"]["id"]."`, "
         ."`".$mysql["table"]["ip_from"]."`, "
         ."`".$mysql["table"]["ip_to"]."`, "
         ."`".$mysql["table"]["code"]."`, "
         ."`".$mysql["table"]["code3"]."`, "
         ."`".$mysql["table"]["country"]."`) values ".
         "('".$idnum."', '".$ip_start."', '".$ip_end."', '".$code."', '".$code3."', '".$country."')";
        if(!mysql_query($sql, $conn)) {
     echo mysql_error()."<br>\n";
     die("Something wicked happened, check your file!!<br>\n");
        }
    */ // Haven't update the above statement yet, but that's not the problem    
        
    $num++;
        
    $idnum++;
    }
    fclose($fp); 
    Now, what I want to do is somehow 'upload' the file (where?) and use it inside this script. How do I upload a file? Can I dump this into a temp-file? What should the settings be of the (external) php-server?

    Any help is appreciated!
    Jazz

  2. #2
    SitePoint Enthusiast
    Join Date
    Feb 2003
    Location
    Argentina
    Posts
    64
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    hello,
    file uploads with PHP are covered in detail
    here
    Have fun!

  3. #3
    SitePoint Member
    Join Date
    Aug 2002
    Posts
    4
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    There is also a php function that specifically parses CSV files: fgetcsv()


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
  •