Ho do I confirm all bytes have been read from stream

PHP programming forum. Ask questions or help people concerning PHP code. Don't understand a function? Need help implementing a class? Don't understand a class? Here is where to ask. Remember to do your homework!

Moderator: General Moderators

Post Reply
Ravisankar
Forum Newbie
Posts: 9
Joined: Tue Mar 04, 2008 5:12 am

Ho do I confirm all bytes have been read from stream

Post by Ravisankar »

<?php

$handle = fopen("php://input");
$how_much=100;
$str = read_data($handle,$how_much);

Function read_data($handle,$how_much)
{

$string='';
while(1)
{
$string .= fread($handle,$how_much);//Up to 8kb data can be read from inputstream using fread()
$read_len = strlen($string);//it will take some CPU
if($read_len == $how_much)
{
break;
}
else if($read_len < $how_much)
{
$how_much = $how_much-$read_len;
}
}
return $string;
}
?>


In this above code, I have read 100 bytes from stream. after read I need to confirm
that whether I have read all 100 bytes or not. For that one thing can be done, using strlen()
we can confirm the no. of bytes. I thing strlen() is inefficient way for big amount of data.
suppose we are reading 8kb bytes. when we use strlen() function to compare at that time
function's loop would run 1b to 8kb times to find length of string.

In java has function in inputstream ,
public int read(byte[] b,int offset,int len) this function will return no. of bytes which is read.

Will PHP has this kind of function?
Any Idea about confirming read bytes.?
please give me suggestion.
thanks in advance
User avatar
requinix
Spammer :|
Posts: 6617
Joined: Wed Oct 15, 2008 2:35 am
Location: WA, USA

Re: Ho do I confirm all bytes have been read from stream

Post by requinix »

PHP keeps track of the length of a string so strlen() only tells you that value. It doesn't have to compute it every time like in other languages.
Ravisankar
Forum Newbie
Posts: 9
Joined: Tue Mar 04, 2008 5:12 am

Re: Ho do I confirm all bytes have been read from stream

Post by Ravisankar »

/****PHP keeps track of the length of a string so strlen() only tells you that value. It doesn't have to compute it every time like in other languages.***/

Thank you for your reply

I did not understand, while reading the stream at that time itself will strlen() started to count
?
User avatar
requinix
Spammer :|
Posts: 6617
Joined: Wed Oct 15, 2008 2:35 am
Location: WA, USA

Re: Ho do I confirm all bytes have been read from stream

Post by requinix »

Code: Select all

$string .= fread($handle,$how_much);
As soon as you put the string into the variable the length was recorded.
Ravisankar
Forum Newbie
Posts: 9
Joined: Tue Mar 04, 2008 5:12 am

Re: Ho do I confirm all bytes have been read from stream

Post by Ravisankar »

/***As soon as you put the string into the variable the length was recorded.**/

Oh! my doubt is got over. Thank you friend.
Post Reply