note 101263 modified in function.file-get-contents by danbrown

From: Date: Mon, 06 Dec 2010 17:57:48 +0000
Subject: note 101263 modified in function.file-get-contents by danbrown
References: 1  Groups: php.notes 
Request: Send a blank email to php-notes+get-174663@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 <?php $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. <?php $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); ?> --was-- 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); ?> http://php.net/manual/en/function.file-get-contents.php

« previous php.notes (#174663) next »