Mindaugas Posted January 14, 2010 Share Posted January 14, 2010 Hello, sorry if this kind of thread already exists, i couldn`t find it. For example there is file with lines: message1 message2 message3 message4 and i want to echo only 3rd line, or only 1,2,3 lines, how to do this? Link to comment https://forums.phpfreaks.com/topic/188449-single-or-several-lines-from-txt-file/ Share on other sites More sharing options...
JAY6390 Posted January 14, 2010 Share Posted January 14, 2010 use fopen and fgets Link to comment https://forums.phpfreaks.com/topic/188449-single-or-several-lines-from-txt-file/#findComment-994864 Share on other sites More sharing options...
ram4nd Posted January 14, 2010 Share Posted January 14, 2010 "read line from file php" to google Link to comment https://forums.phpfreaks.com/topic/188449-single-or-several-lines-from-txt-file/#findComment-994865 Share on other sites More sharing options...
PFMaBiSmAd Posted January 14, 2010 Share Posted January 14, 2010 The answer depends on if the maximum size of the file can grow so that it will not entirely fit in available memory at one time? Link to comment https://forums.phpfreaks.com/topic/188449-single-or-several-lines-from-txt-file/#findComment-994867 Share on other sites More sharing options...
PFMaBiSmAd Posted January 14, 2010 Share Posted January 14, 2010 And assuming the file will always fit into memory, try this simple solution - <?php $the_file = 'text.dat'; // what file to read $lines = file($the_file); // read the file into an array // example 1 $find_these = array(1); // the line(s) you want foreach($find_these as $key){ $index = $key -1; // adjust to match actual index values (start at zero) echo "Line: $key, $lines[$index]<br />"; } // example 2 $find_these = array(1,2,3); // the line(s) you want foreach($find_these as $key){ $index = $key -1; // adjust to match actual index values (start at zero) echo "Line: $key, $lines[$index]<br />"; } ?> Link to comment https://forums.phpfreaks.com/topic/188449-single-or-several-lines-from-txt-file/#findComment-994872 Share on other sites More sharing options...
Mindaugas Posted January 14, 2010 Author Share Posted January 14, 2010 And assuming the file will always fit into memory, try this simple solution - <?php $the_file = 'text.dat'; // what file to read $lines = file($the_file); // read the file into an array // example 1 $find_these = array(1); // the line(s) you want foreach($find_these as $key){ $index = $key -1; // adjust to match actual index values (start at zero) echo "Line: $key, $lines[$index]<br />"; } // example 2 $find_these = array(1,2,3); // the line(s) you want foreach($find_these as $key){ $index = $key -1; // adjust to match actual index values (start at zero) echo "Line: $key, $lines[$index]<br />"; } ?> Thx it`s working perfectly why txt file is .dat not .txt ? Link to comment https://forums.phpfreaks.com/topic/188449-single-or-several-lines-from-txt-file/#findComment-994876 Share on other sites More sharing options...
PFMaBiSmAd Posted January 14, 2010 Share Posted January 14, 2010 why txt file is .dat not .txt ? Because it is just an example of how to do something and it is expected that it may need to be adapted it to fit any specific use you may have. Link to comment https://forums.phpfreaks.com/topic/188449-single-or-several-lines-from-txt-file/#findComment-994889 Share on other sites More sharing options...
Mindaugas Posted January 14, 2010 Author Share Posted January 14, 2010 Now i`m wondering what file extensions is best to include or use for reading ? .dat .txt .inc .others Link to comment https://forums.phpfreaks.com/topic/188449-single-or-several-lines-from-txt-file/#findComment-994892 Share on other sites More sharing options...
JAY6390 Posted January 14, 2010 Share Posted January 14, 2010 For your script, it doesn't actually make a difference. It's only if you're going to allow people to view it directly that it will make a difference Link to comment https://forums.phpfreaks.com/topic/188449-single-or-several-lines-from-txt-file/#findComment-994896 Share on other sites More sharing options...
Mindaugas Posted January 14, 2010 Author Share Posted January 14, 2010 For example , simple chat and every message saves in file.txt, and i dont want that ppl could see it directly www.name.com/file.txt , just want when ppl come to chat they see only first 5 lines, it will be 5 newest messages. Link to comment https://forums.phpfreaks.com/topic/188449-single-or-several-lines-from-txt-file/#findComment-994904 Share on other sites More sharing options...
JAY6390 Posted January 14, 2010 Share Posted January 14, 2010 For this you are much better off using a database to be honest Link to comment https://forums.phpfreaks.com/topic/188449-single-or-several-lines-from-txt-file/#findComment-994905 Share on other sites More sharing options...
Mindaugas Posted January 14, 2010 Author Share Posted January 14, 2010 I know, i think it`s easier to use MySQL than work with txt database, but it`s my first project, simple game, and i want to finish it and then learn mysql Link to comment https://forums.phpfreaks.com/topic/188449-single-or-several-lines-from-txt-file/#findComment-994907 Share on other sites More sharing options...
JAY6390 Posted January 14, 2010 Share Posted January 14, 2010 I see. Well personally I keep any files that I don't want people to have access to outside of the document root folder, so that they aren't accessible via any uri other than internally on the server. I suppose you could use a .htaccess file to block certain file extensions from being access too Link to comment https://forums.phpfreaks.com/topic/188449-single-or-several-lines-from-txt-file/#findComment-994910 Share on other sites More sharing options...
Mindaugas Posted January 14, 2010 Author Share Posted January 14, 2010 Well thx all of you for help really Have a good day Link to comment https://forums.phpfreaks.com/topic/188449-single-or-several-lines-from-txt-file/#findComment-994914 Share on other sites More sharing options...
Mindaugas Posted January 14, 2010 Author Share Posted January 14, 2010 I added .htaccess file in my server, added : <Files ~ "\.(txt)$"> order allow,deny deny from all </Files> And now noone can view this file p.s. sorry for two posts, maybe this info will be usefull for someone. Link to comment https://forums.phpfreaks.com/topic/188449-single-or-several-lines-from-txt-file/#findComment-994922 Share on other sites More sharing options...
JAY6390 Posted January 14, 2010 Share Posted January 14, 2010 Excellent Link to comment https://forums.phpfreaks.com/topic/188449-single-or-several-lines-from-txt-file/#findComment-994943 Share on other sites More sharing options...
Recommended Posts
Archived
This topic is now archived and is closed to further replies.