return a single line from a text doc
hi, using fread how do you get it to return a single line from a text doc. like with this kind of code
PHP Code:
<?php
$line = $_GET['line'];
$uid = $_GET['uid'];
$filename = "D:/inetpub/xproot/users/" . $uid . "fav.txt";
$handle = fopen($filename, "r");
$contents = //make $contents to be what is on line defined by $line;
fclose($handle);
echo $contents;
?>
i have googled this and all it had was some other things that had nothing to do with my question. this is something to do with a program i am making which uses msinet to get what is returned into a string or text box.
thanks, dandono
Re: return a single line from a text doc
Have a look at the file() function - it should help you :)
Cheers,
RyanJ
Re: return a single line from a text doc
ok i get what that function does but i dont get how to make it display only one line and that line being $line. how do i do this?
Re: return a single line from a text doc
Quote:
Originally Posted by dandono
ok i get what that function does but i dont get how to make it display only one line and that line being $line. how do i do this?
Because the function returns the array, just pass the $line as the index to get the required line :)
Cheers,
RyanJ
Re: return a single line from a text doc
i think that this is not the best way to do this but it is the only way i can think. now somebody is going to come up with a code a houndred times better than mine in like five minuets.
this is my code. i will set it to open a local text file in a bit.
PHP Code:
<?php
// Get a file into an array. In this example we'll go through HTTP to get
// the HTML source of a URL.
$lines = file('http://www.php.net/');
$no = $_GET['line'];
// Loop through our array, show HTML source as HTML source; and line numbers too.
foreach ($lines as $line_num => $line) {
if ($line_num == $no)
echo "Line #<b>{$line_num}</b> : " . htmlspecialchars($line) . "<br />\n";
}
// Another example, let's get a web page into a string. See also file_get_contents().
$html = implode('', file('http://www.example.com/'));
?>
thanks for the help
Re: return a single line from a text doc
how do you get that code to view a text doc on the server mechine like the path D:/blah.txt? is it just with fread?
Re: return a single line from a text doc
To get just the first line of a file use the fgets() function.
Re: return a single line from a text doc
sorry i fixed it a different way like this:
PHP Code:
<?php
$uid = $_GET['uid'];
$file = "D:/inetpub/xproot/xorite/browser/" . $uid . "fav.txt";
// Get a file into an array. In this example we'll go through HTTP to get
// the HTML source of a URL.
$lines = file($file);
$no = $_GET['line'];
// Loop through our array, show HTML source as HTML source; and line numbers too.
foreach ($lines as $line_num => $line) {
if ($line_num == $no)
echo htmlspecialchars($line) . "<br />\n";
}
// Another example, let's get a web page into a string. See also file_get_contents().
$html = implode('', file($file));
?>