Dynadot โ€” .com Transfer

PHP help needed

Spacemail by SpaceshipSpacemail by Spaceship
Watch
Impact
91
hi

i am trying to fopen a file which is 20mb in size.

i want fopen to only get the first 100kb or so from the file and ignore the rest. i do not want to even download the rest of the file. (saving bandwidth)

is it possible? please tell me what commands to use for that

thanks
 
0
•••
The views expressed on this page by users and staff are their own, not those of NamePros.
GoDaddyGoDaddy
I am not 100% sure but I would suspect using fgets could do it:-

http://uk.php.net/fgets

you would need to specify the length you wish to read in the 2nd parameter.
 
0
•••
Mentioning "download" I assume you dont talk about a local file, right? Do you want to access the beginning of the file?
 
0
•••
neroux said:
Mentioning "download" I assume you dont talk about a local file, right? Do you want to access the beginning of the file?

Obviously he is talking download otherwise he would not be mentioning trying to save bandwidth.
 
0
•••
peter@flexiwebhost said:
Obviously he is talking download otherwise he would not be mentioning trying to save bandwidth.
Thats what I assumed, but wanted a confirmation.

fgets might not work by the way, as it only works with textfiles and probably does not support partial downloads.
 
0
•••
read the manual for fgets. Part of its functionality is to read the first x amount of bytes from a file. And what makes you say fgets only works with txt files? Any file that contains ASCII can be read by fgets.

In fact if you read the documentation for fsockopen you will see they have used it to read the contents of a webpage (whilst only reading 128 bytes at a time).

http://uk.php.net/manual/en/function.fsockopen.php
 
0
•••
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, 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:
And what makes you say fgets only works with txt files? Any file that contains ASCII can be read by fgets.
Yes and it returns on the first newline, not exactly useful on binary data.
 
0
•••
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.

A pseudo piece of code (working from the fsockopen example) could be:-

PHP:
<?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);
}
?>

This will not get the exact length required (possibly slightly over) but with a little more coding that can be achieved with ease.

If he needs to know for sure that it works then that is what testing is for.
 
0
•••
I suppose you could use fgetc() or fread(), to avoid the newline problem

PHP:
<?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:
<?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); 
} 
?>
 
0
•••
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.
Nobody said that.

peter@flexiwebhost said:
A pseudo piece of code (working from the fsockopen example) could be:-
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.
 
0
•••
Dynadot โ€” .com TransferDynadot โ€” .com Transfer
Domain Recover
DomainEasy โ€” Payment Flexibility
  • The sidebar remains visible by scrolling at a speed relative to the pageโ€™s height.
Back