NameSilo

PHP help needed

SpaceshipSpaceship
SpaceshipSpaceship
SpaceshipSpaceship
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
Close
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
Close
Mentioning "download" I assume you dont talk about a local file, right? Do you want to access the beginning of the file?
 
0
Close
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
Close
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
Close
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
Close
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
Close
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
Close
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
Close
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
Close
CatchedCatched

We're social

Escrow.com
Spaceship
Domain Recover
CryptoExchange.com

NamePros updates

Switch between accounts
You can now stay logged in to more than one NamePros account in the same browser. Click your avatar, then "Accounts in this browser", to...
AIVikings
Catchy
  • The sidebar remains visible by scrolling at a speed relative to the page’s height.
Back