1. Advertising
    y u no do it?

    Advertising (learn more)

    Advertise virtually anything here, with CPM banner ads, CPM email ads and CPC contextual links. You can target relevant areas of the site and show ads based on geographical location of the user if you wish.

    Starts at just $1 per CPM or $0.10 per CPC.

Reading lines from text file with delimiter and adding into database

Discussion in 'PHP' started by ariffin246, Sep 20, 2011.

  1. #1
    Hi everyone,

    I have a text file with similar content as below:
    
    admin,123
    user,123
    user2,123
    
    Code (markup):

    What i need to perform is to upload the text file and read each lines. Then split the content of each lines by delimiter and add it to database. In my database there are two fields; user and password.


    Any PHP Guru, Please guide me on this issue.

    Thank in advance.

    This is the code that i have used to split the uploaded data by lines.



    
     <?
     if (isset($_POST['Submit'])){
     
    	$fd = fopen ($form_data, "r"); 
    	$contents = fread ($fd,filesize ($form_data));
    
    	fclose ($fd); 
    	$delimiter = ",";
    	$splitcontents = explode($delimiter, $contents);
    	$counter = "";
     
     	 
    	$lines = file($form_data);
    	foreach($lines as $line_num => $line)
    	{
    	echo $line;
    	echo "<br>";
    
    	
    	}
     }
     ?>
    
     <form method="post" action="" enctype="multipart/form-data"> 
    
     <input type="hidden" name="MAX_FILE_SIZE" value="1000000"> 
     <br>File to upload:<br> 
     <input type="file" name="form_data" size="40"> 
     <p><input type="submit" name="Submit" value="Submit"> 
     </form>
    
    PHP:
     
    ariffin246, Sep 20, 2011 IP
  2. mastermunj

    mastermunj Well-Known Member

    Messages:
    687
    Likes Received:
    13
    Best Answers:
    0
    Trophy Points:
    110
    #2
    mastermunj, Sep 21, 2011 IP
    jevchance likes this.
  3. ariffin246

    ariffin246 Well-Known Member

    Messages:
    247
    Likes Received:
    1
    Best Answers:
    0
    Trophy Points:
    105
    #3
    Hi,

    Thanks for the reply. But i am not using files with .csv format but a .txt file.
    Would it work on it ?
     
    ariffin246, Sep 21, 2011 IP
  4. mastermunj

    mastermunj Well-Known Member

    Messages:
    687
    Likes Received:
    13
    Best Answers:
    0
    Trophy Points:
    110
    #4
    Yes, that will work.
     
    mastermunj, Sep 21, 2011 IP
  5. ariffin246

    ariffin246 Well-Known Member

    Messages:
    247
    Likes Received:
    1
    Best Answers:
    0
    Trophy Points:
    105
    #5
    I tried to use the following code after referring to the php manual file
    Its working fine on LAN when i run in XAMPP but when i run in online server. It keeps looping and inserting empty records like few hundreds rows.
    What would be the problem ?
     
    ariffin246, Sep 29, 2011 IP
  6. mastermunj

    mastermunj Well-Known Member

    Messages:
    687
    Likes Received:
    13
    Best Answers:
    0
    Trophy Points:
    110
    #6
    Below line is completely wrong.

    
    $handle = fopen("$form_data", "r"); 
    
    PHP:
    You need to refer the file either from $_FILES array directly, or first move the file using move_uploaded_file to another location and use from there.
     
    mastermunj, Sep 29, 2011 IP