- Impact
- 91


neroux said:Mentioning "download" I assume you dont talk about a local file, right? Do you want to access the beginning of the file?
Thats what I assumed, but wanted a confirmation.peter@flexiwebhost said:Obviously he is talking download otherwise he would not be mentioning trying to save bandwidth.
Yes, but its not mentioned whether it also works over network connections. Also it would only work on the first call, all subsequent calls would move the offset.peter@flexiwebhost said:read the manual for fgets. Part of its functionality is to read the first x amount of bytes from a file.
Yes and it returns on the first newline, not exactly useful on binary data.peter@flexiwebhost said:And what makes you say fgets only works with txt files? Any file that contains ASCII can be read by fgets.
<?php
$fp = fsockopen("www.example.com", 80, $errno, $errstr, 30);
if (!$fp) {
echo "$errstr ($errno)<br />\n";
} else {
$out = "GET / HTTP/1.1\r\n";
$out .= "Host: www.example.com\r\n";
$out .= "Connection: Close\r\n\r\n";
fwrite($fp, $out);
$maximumbuffer = '1024';
$buffer = '';
while (!feof($fp) && strlen($buffer)<=$maximumbuffer) {
$buffer .= fgets($fp, 128);
}
fclose($fp);
}
?>
<?php
$fp = fsockopen("www.example.com", 80, $errno, $errstr, 30);
if (!$fp) {
echo "$errstr ($errno)<br />\n";
} else {
$out = "GET / HTTP/1.1\r\n";
$out .= "Host: www.example.com\r\n";
$out .= "Connection: Close\r\n\r\n";
fwrite($fp, $out);
$maximumbuffer = '1024';
$buffer = '';
while (!feof($fp) && strlen($buffer)<=$maximumbuffer) {
$buffer = fread($fp, 128);
}
fclose($fp);
}
?>
<?php
$fp = fsockopen("www.example.com", 80, $errno, $errstr, 30);
if (!$fp) {
echo "$errstr ($errno)<br />\n";
} else {
$out = "GET / HTTP/1.1\r\n";
$out .= "Host: www.example.com\r\n";
$out .= "Connection: Close\r\n\r\n";
fwrite($fp, $out);
$maximumbuffer = '1024';
$buffer = '';
while (!feof($fp) && strlen($buffer)<=$maximumbuffer) {
$buffer = fgetc($fp);
}
fclose($fp);
}
?>
Nobody said that.peter@flexiwebhost said:PHP is not about having handed to you on a plate you do actually have to do stuff (and think of things) for yourself as well.
The given example will make a full request (20 MB in this case), the premature fclose will probably abort the data transfer however. As it only reads line by line until the buffer is at least 1 KB, fread() might be the better solution, although it could lead to a truncated line, but then we can only guess without more information about requirements and data structure.peter@flexiwebhost said:A pseudo piece of code (working from the fsockopen example) could be:-


