note 101263 added to function.file-get-contents

From: Date: Mon, 06 Dec 2010 11:07:44 +0000
Subject: note 101263 added to function.file-get-contents
Groups: php.notes 
Request: Send a blank email to php-notes+get-174643@lists.php.net to get a copy of this message
If working file is bigger than 64kb and you getting deadlock. Your buffer is overflow. Here are two way how to avoid that. 1) use temporary file for descriptor <? $descriptorspec = array( 0 => array("file", "/tmp/ens/a.ens","r"), // stdin is a pipe that the child will read from 1 => array("file", "/tmp/ens/a.html","w"), // stdout is a pipe that the child will write to 2 => array("file", "/tmp/ens/error-output.txt", "a") // stderr is a file to write to ); ?> 2) inline read using stream_set_blocking. PHP doesn't proper handle last part of file. <? $READ_LEN = 64*1024; $MAX_BUF_LEN = 2*$READ_LEN; $url = "http://some.domain.com:5984/".$db."/".$member."/contents"; $src = fopen($url,"r"); $cwd = '/tmp'; $cmd['enscript'] = "/usr/bin/enscript"; $cmd['enscript-options'] = " -q --language=html --color -Ejcl -o -"; $descriptorspec = array( 0 => array("pipe", "r"), // stdin is a pipe that the child will read from 1 => array("pipe", "w") // stdout is a pipe that the child will write to ); $ph=proc_open($cmd['enscript']." ".$cmd['enscript-options'],$descriptorspec,$pipes,$cwd); stream_set_blocking($src,0); stream_set_blocking($pipes[0],0); stream_set_blocking($pipes[1],0); $CMD_OUT_OPEN = TRUE; $k = 0; while (!feof($pipes[1]) || !feof($src) || $k > 0) { if (!feof($src) && $k+$READ_LEN <= $MAX_BUF_LEN) { $input .= fread($src,$READ_LEN); $k = strlen($input); } if ($k > 0) { $l = fwrite($pipes[0],$input); $k -= $l; $input = substr($input,$l); } if ($CMD_OUT_OPEN && $k == 0 && feof($src)) { fclose($pipes[0]); $CMD_OUT_OPEN = FALSE; } $output = fread($pipes[1],$READ_LEN); $outputn = str_replace("<H1>(stdin)</H1>","",$output); echo $outputn; } fclose($pipes[1]); $return_value = proc_close($ph); ?> ---- Server IP: 208.69.120.55 Probable Submitter: 130.119.248.52 ---- Manual Page -- http://www.php.net/manual/en/function.file-get-contents.php Edit -- https://master.php.net/note/edit/101263 Del: integrated -- https://master.php.net/note/delete/101263/integrated Del: useless -- https://master.php.net/note/delete/101263/useless Del: bad code -- https://master.php.net/note/delete/101263/bad+code Del: spam -- https://master.php.net/note/delete/101263/spam Del: non-english -- https://master.php.net/note/delete/101263/non-english Del: in docs -- https://master.php.net/note/delete/101263/in+docs Del: other reasons-- https://master.php.net/note/delete/101263 Reject -- https://master.php.net/note/reject/101263 Search -- https://master.php.net/manage/user-notes.php

« previous php.notes (#174643) next »