note 74274 deleted from function.fread by bjori

From: Date: Tue, 03 Apr 2007 11:59:42 +0000
Subject: note 74274 deleted from function.fread by bjori
References: 1  Groups: php.notes 
Request: Send a blank email to php-notes+get-125033@lists.php.net to get a copy of this message
Note Submitter: donynam at hotmail dot com Reason: bad code ---- 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.*/ ?>

« previous php.notes (#125033) next »