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:
This is a CSV format. Use fgetcsv to make it simple. For using uploaded file, you need to refer to $_FILES and move_uploaded_file.
Hi, Thanks for the reply. But i am not using files with .csv format but a .txt file. Would it work on it ?
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 ?
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.