LinuxQuestions.org
Go Job Hunting at the LQ Job Marketplace
Go Back   LinuxQuestions.org > Forums > Non-*NIX Forums > Programming
User Name
Password
Programming This forum is for all programming questions.
The question does not have to be directly related to Linux and any language is fair game.

Notices



Reply
 
Search this Thread
Old 07-16-2008, 04:25 PM   #1
adrianno2
Member
 
Registered: Oct 2005
Location: South Romania
Distribution: Lubuntu 13.04
Posts: 39

Rep: Reputation: 15
php script that reads content from text file


hi.
as subject says, is it possible to build a php script that reads a text file and add it to a mysql database?

example:
i have a file named contacts.txt, in this file, i have some names and phone numbers like this:
name1, phone#1
name2, phone#2
name3, phone#3
..............
..............
..............
and list continues, i have about 200 lines in this file.

i have basic knowledge about php, i know how to work with a database from php but i'm confused about reading text from files in php.
can you tell me what functions i need to use and explain me how can i build this if is possible?

thanks
 
Old 07-16-2008, 04:39 PM   #2
TB0ne
Guru
 
Registered: Jul 2003
Location: Birmingham, Alabama
Distribution: SuSE, RedHat, Slack,CentOS
Posts: 15,082

Rep: Reputation: 2714Reputation: 2714Reputation: 2714Reputation: 2714Reputation: 2714Reputation: 2714Reputation: 2714Reputation: 2714Reputation: 2714Reputation: 2714Reputation: 2714
Quote:
Originally Posted by adrianno2 View Post
hi.
as subject says, is it possible to build a php script that reads a text file and add it to a mysql database?

example:
i have a file named contacts.txt, in this file, i have some names and phone numbers like this:
name1, phone#1
name2, phone#2
name3, phone#3
..............
..............
..............
and list continues, i have about 200 lines in this file.

i have basic knowledge about php, i know how to work with a database from php but i'm confused about reading text from files in php.
can you tell me what functions i need to use and explain me how can i build this if is possible?

thanks
Very possible, and fairly easy. Check out http://www.php.net, that should get you a list of all the functions you're looking for. In particular, search for mysql_connect, and check out this example:

http://us.php.net/manual/en/mysql.examples.php

and

http://us.php.net/manual/en/function.fread.php

on how to read a file. This page

http://www.modwest.com/help/kb.phtml?qid=253&cat=6

Might give you a better idea.
 
Old 07-16-2008, 04:45 PM   #3
avatardeviva
Member
 
Registered: Jan 2004
Location: Almost Canada :-p
Posts: 34

Rep: Reputation: 15
I'll throw my two cents in too:

Code:
<?php

$file='test.txt';
$content=file_get_contents($file); //Donno how much different this is
                                   //than using fread, but it works!
$pieces = array();
$pieces = explode("\n",$content);

foreach($content as $line){
    //Do something, like split it again!
    $linedata = array();
    $linedata = explode(",",$line);
    print_r($linedata); //Lets see whats in there
}

?>
Now granted, I just wrote that offhand, but thats basically the idea. You can use the $linedata array to put things in where they are supposed to go, for example:
Code:
mysql_query("insert into somewhere (`col1`,`col2`,`col3`) values ('{$linedata[0]}','{$linedata[1]}','{$linedata[2]}')",$db);
 
Old 07-21-2008, 02:14 AM   #4
adrianno2
Member
 
Registered: Oct 2005
Location: South Romania
Distribution: Lubuntu 13.04
Posts: 39

Original Poster
Rep: Reputation: 15
thanks TB0ne and avatardeviva, now i'm trying that

sorry for my late reply, i had a harddisk failure



LE:

@avatardeviva: your script throws me an error:
Warning: Invalid argument supplied for foreach() in /home/ra/www/index.php on line 9

line 9 is this:
foreach($content as $line){

whats the problem?

Last edited by adrianno2; 07-21-2008 at 04:17 AM.
 
  


Reply


Thread Tools Search this Thread
Search this Thread:

Advanced Search

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
simple php script to add line/file to text file dnoy Programming 1 05-21-2008 06:08 PM
Replace text of unknown content with other text in file brian0918 Programming 15 07-14-2005 10:22 PM
Replace text of unknown content with other text in file brian0918 Linux - Software 1 07-14-2005 04:22 PM
php script can not write text file lemotion Linux - Newbie 5 04-20-2004 11:14 PM
set content-type to 'text/html' in sendmail, using perl script brokenfeet Programming 3 08-05-2003 03:12 PM


All times are GMT -5. The time now is 11:56 PM.

Main Menu
Advertisement
My LQ
Write for LQ
LinuxQuestions.org is looking for people interested in writing Editorials, Articles, Reviews, and more. If you'd like to contribute content, let us know.
Main Menu
Syndicate
RSS1  Latest Threads
RSS1  LQ News
Twitter: @linuxquestions
identi.ca: @linuxquestions
Facebook: linuxquestions Google+: linuxquestions
Open Source Consulting | Domain Registration