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? Quote Link to comment Share on other sites More sharing options...
JAY6390 Posted January 14, 2010 Share Posted January 14, 2010 use fopen and fgets Quote Link to comment Share on other sites More sharing options...
ram4nd Posted January 14, 2010 Share Posted January 14, 2010 "read line from file php" to google Quote Link to comment 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? Quote Link to comment 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 />"; } ?> Quote Link to comment 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 ? Quote Link to comment 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. Quote Link to comment 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 Quote Link to comment 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 Quote Link to comment 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. Quote Link to comment 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 Quote Link to comment 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 Quote Link to comment 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 Quote Link to comment 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 Quote Link to comment 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. Quote Link to comment Share on other sites More sharing options...
JAY6390 Posted January 14, 2010 Share Posted January 14, 2010 Excellent Quote Link to comment Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.