[advanced search]
 

Go Back   NamePros.com > Discussion > Web Design & Development > Programming

Programming PHP, Perl, Ruby on Rails, AJAX, HTML, XHTML, CSS, JavaScript, MySQL and any other coding topics.


Closed Thread
 
LinkBack Thread Tools
Old 11-01-2007, 10:42 AM   #1 (permalink)
Senior Member
 
champ_rock's Avatar
 
Join Date: Oct 2006
Location: http://akshayjain.org
Posts: 2,768
8,037.05 NP$ (Donate)

champ_rock has a reputation beyond reputechamp_rock has a reputation beyond reputechamp_rock has a reputation beyond reputechamp_rock has a reputation beyond reputechamp_rock has a reputation beyond reputechamp_rock has a reputation beyond reputechamp_rock has a reputation beyond reputechamp_rock has a reputation beyond reputechamp_rock has a reputation beyond reputechamp_rock has a reputation beyond reputechamp_rock has a reputation beyond repute


PHP help needed

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
__________________
Akshay Jain
......................
champ_rock is offline  
Old 11-01-2007, 12:31 PM   #2 (permalink)
Senior Member
 
Peter's Avatar
 
Join Date: Nov 2003
Location: Scotland
Posts: 4,900
0.60 NP$ (Donate)

Peter has a reputation beyond reputePeter has a reputation beyond reputePeter has a reputation beyond reputePeter has a reputation beyond reputePeter has a reputation beyond reputePeter has a reputation beyond reputePeter has a reputation beyond reputePeter has a reputation beyond reputePeter has a reputation beyond reputePeter has a reputation beyond reputePeter has a reputation beyond repute

Child Abuse Save The Children Save The Children Help The Homeless - Holiday 2009 Help The Homeless - Holiday 2009 Help The Homeless - Holiday 2009 Help The Homeless - Holiday 2009
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.
__________________
Manage your portfolio using my new Domain Portfolio Management script.
Securing Your Domain Name From Theft
Peter is offline  
Old 11-01-2007, 12:34 PM   #3 (permalink)
NamePros Regular
 
Join Date: Oct 2006
Posts: 918
40.00 NP$ (Donate)

neroux has a spectacular aura aboutneroux has a spectacular aura about


Mentioning "download" I assume you dont talk about a local file, right? Do you want to access the beginning of the file?
__________________
Paris loves CityPics

muov.com • qeww.com • sejz.com • viuo.com • vuav.com • wzeo.com • xeib.com • xueo.com

-- Do not let others be treated this way!
neroux is offline  
Old 11-01-2007, 12:35 PM   #4 (permalink)
Senior Member
 
Peter's Avatar
 
Join Date: Nov 2003
Location: Scotland
Posts: 4,900
0.60 NP$ (Donate)

Peter has a reputation beyond reputePeter has a reputation beyond reputePeter has a reputation beyond reputePeter has a reputation beyond reputePeter has a reputation beyond reputePeter has a reputation beyond reputePeter has a reputation beyond reputePeter has a reputation beyond reputePeter has a reputation beyond reputePeter has a reputation beyond reputePeter has a reputation beyond repute

Child Abuse Save The Children Save The Children Help The Homeless - Holiday 2009 Help The Homeless - Holiday 2009 Help The Homeless - Holiday 2009 Help The Homeless - Holiday 2009
Quote:
Originally Posted by neroux
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.
__________________
Manage your portfolio using my new Domain Portfolio Management script.
Securing Your Domain Name From Theft
Peter is offline  
Old 11-01-2007, 12:42 PM   #5 (permalink)
NamePros Regular
 
Join Date: Oct 2006
Posts: 918
40.00 NP$ (Donate)

neroux has a spectacular aura aboutneroux has a spectacular aura about


Quote:
Originally Posted by peter@flexiwebhost
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.
__________________
Paris loves CityPics

muov.com • qeww.com • sejz.com • viuo.com • vuav.com • wzeo.com • xeib.com • xueo.com

-- Do not let others be treated this way!
neroux is offline  
Old 11-01-2007, 12:48 PM   #6 (permalink)
Senior Member
 
Peter's Avatar
 
Join Date: Nov 2003
Location: Scotland
Posts: 4,900
0.60 NP$ (Donate)

Peter has a reputation beyond reputePeter has a reputation beyond reputePeter has a reputation beyond reputePeter has a reputation beyond reputePeter has a reputation beyond reputePeter has a reputation beyond reputePeter has a reputation beyond reputePeter has a reputation beyond reputePeter has a reputation beyond reputePeter has a reputation beyond reputePeter has a reputation beyond repute

Child Abuse Save The Children Save The Children Help The Homeless - Holiday 2009 Help The Homeless - Holiday 2009 Help The Homeless - Holiday 2009 Help The Homeless - Holiday 2009
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
__________________
Manage your portfolio using my new Domain Portfolio Management script.
Securing Your Domain Name From Theft
Peter is offline  
Old 11-01-2007, 12:53 PM   #7 (permalink)
NamePros Regular
 
Join Date: Oct 2006
Posts: 918
40.00 NP$ (Donate)

neroux has a spectacular aura aboutneroux has a spectacular aura about


Quote:
Originally Posted by peter@flexiwebhost
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.

Quote:
Originally Posted by peter@flexiwebhost
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.
__________________
Paris loves CityPics

muov.com • qeww.com • sejz.com • viuo.com • vuav.com • wzeo.com • xeib.com • xueo.com

-- Do not let others be treated this way!
neroux is offline  
Old 11-01-2007, 01:02 PM   #8 (permalink)
Senior Member
 
Peter's Avatar
 
Join Date: Nov 2003
Location: Scotland
Posts: 4,900
0.60 NP$ (Donate)

Peter has a reputation beyond reputePeter has a reputation beyond reputePeter has a reputation beyond reputePeter has a reputation beyond reputePeter has a reputation beyond reputePeter has a reputation beyond reputePeter has a reputation beyond reputePeter has a reputation beyond reputePeter has a reputation beyond reputePeter has a reputation beyond reputePeter has a reputation beyond repute

Child Abuse Save The Children Save The Children Help The Homeless - Holiday 2009 Help The Homeless - Holiday 2009 Help The Homeless - Holiday 2009 Help The Homeless - Holiday 2009
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 Code:
<?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.
__________________
Manage your portfolio using my new Domain Portfolio Management script.
Securing Your Domain Name From Theft
Peter is offline  
Old 11-01-2007, 01:10 PM   #9 (permalink)
Barru.
 
Barrucadu's Avatar
 
Join Date: Aug 2005
Location: East Yorkshire, England
Posts: 2,731
78.50 NP$ (Donate)

Barrucadu is a splendid one to beholdBarrucadu is a splendid one to beholdBarrucadu is a splendid one to beholdBarrucadu is a splendid one to beholdBarrucadu is a splendid one to beholdBarrucadu is a splendid one to beholdBarrucadu is a splendid one to behold


I suppose you could use fgetc() or fread(), to avoid the newline problem

PHP Code:
<?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 Code:
<?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);
}
?>
Barrucadu is offline  
Old 11-01-2007, 01:19 PM   #10 (permalink)
NamePros Regular
 
Join Date: Oct 2006
Posts: 918
40.00 NP$ (Donate)

neroux has a spectacular aura aboutneroux has a spectacular aura about


Quote:
Originally Posted by peter@flexiwebhost
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.

Quote:
Originally Posted by peter@flexiwebhost
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.
__________________
Paris loves CityPics

muov.com • qeww.com • sejz.com • viuo.com • vuav.com • wzeo.com • xeib.com • xueo.com

-- Do not let others be treated this way!
neroux is offline  
Closed Thread


Currently Active Users Viewing This Thread: 1 (0 members and 1 guests)
 
Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are On


Site Sponsors
Advertise your business at NamePros

All times are GMT -7. The time now is 06:47 AM.


Powered by: vBulletin® Copyright ©2000 - 2009, Jelsoft Enterprises Ltd.
Search Engine Friendly URLs by vBSEO 3.3.0
Template-Modifications by TMS
vBCredits v1.4 Copyright ©2007 - 2008, PixelFX Studios

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85