<?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
Ho do I confirm all bytes have been read from stream
Moderator: General Moderators
-
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
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
/****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
?
Thank you for your reply
I did not understand, while reading the stream at that time itself will strlen() started to count
?
Re: Ho do I confirm all bytes have been read from stream
Code: Select all
$string .= fread($handle,$how_much);-
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
/***As soon as you put the string into the variable the length was recorded.**/
Oh! my doubt is got over. Thank you friend.
Oh! my doubt is got over. Thank you friend.