note 74274 added to function.fread

From: Date: Tue, 03 Apr 2007 10:38:09 +0000
Subject: note 74274 added to function.fread
Groups: php.notes 
Request: Send a blank email to php-notes+get-125028@lists.php.net to get a copy of this message
To save the file from remote host to local host, for example, <?php file_put_contents ("somefile.rar",file_get_contents( "http://remotehost/somefile.rar",false)); ?> But the example above have some drawbacks, because It not only uses a lot system memory, but also consumes 99% of your CPU resources during execution time. For memory matter, we can use fread to optimize system memory usage. We use fsockopen to make a connection to the remote host, then we use f read to read data from stream. I don't know whether there is a bug that cause fread consume 99% of my CPU resource, but at least, I have solved my computer memory problem. Example, <? ............. ............. //open socket connection $fp = fsockopen ($hostip, '80'); if ($fp) { //sent header to remote host $header_str = "GET ".$path." HTTP/1.1\r\n"; $header_str .= "Host: ".$host."\r\n"; $header_str .= "Accept: */*\r\n"; $header_str .= "Referer: ".$referer."\r\n"; $header_str .= "Cookie: BX=c3skagl1v1ncl&b=3&s=4a\r\n"; $header_str .= "User-Agent: Mozilla/4.0 " $header_str .="(compatible; MSIE 5.00; Windows 98)\r\n"; $header_str .= "Pragma: no-cache\r\n"; $header_str .= "Cache-Control: no-cache\r\n"; $header_str .= "Connection: close\r\n"; fwrite ($fp, $header_str."\r\n"); while (!feof($fp)) { //read stream $buffer.=fread($fp, 16000); ................................... /*find the first encounter "\r\n\r\n" string, the data follow is the content you want. Repeat: read stream, save content until fread encounter eof. */ } fclose($fp) /*Because of notes restriction, I cannot show you the full example.*/ ?> ---- Server IP: 202.157.183.147 Probable Submitter: 219.95.95.56 ---- Manual Page -- http://www.php.net/manual/en/function.fread.php Edit -- https://master.php.net/note/edit/74274 Del: integrated -- https://master.php.net/note/delete/74274/integrated Del: useless -- https://master.php.net/note/delete/74274/useless Del: bad code -- https://master.php.net/note/delete/74274/bad+code Del: spam -- https://master.php.net/note/delete/74274/spam Del: non-english -- https://master.php.net/note/delete/74274/non-english Del: in docs -- https://master.php.net/note/delete/74274/in+docs Del: other reasons-- https://master.php.net/note/delete/74274 Reject -- https://master.php.net/note/reject/74274 Search -- https://master.php.net/manage/user-notes.php

« previous php.notes (#125028) next »