1--TEST-- 2Test popen() and pclose function: basic functionality 3--SKIPIF-- 4<?php 5if(substr(PHP_OS, 0, 3) == 'WIN' ) 6 die("skip Not Valid for Windows"); 7?> 8 9--FILE-- 10<?php 11/* 12 * Prototype: resource popen ( string command, string mode ) 13 * Description: Opens process file pointer. 14 * 15 * Prototype: int pclose ( resource handle ); 16 * Description: Closes process file pointer. 17 */ 18 19$file_path = dirname(__FILE__); 20require($file_path."/file.inc"); 21 22echo "*** Testing popen() and pclose() with different processes ***\n"; 23 24echo "-- Testing popen(): reading from the pipe --\n"; 25$dirpath = $file_path."/popen_basic"; 26mkdir($dirpath); 27touch($dirpath."/popen_basic.tmp"); 28define('CMD', "ls $dirpath"); 29$file_handle = popen(CMD, 'r'); 30fpassthru($file_handle); 31pclose($file_handle); 32 33echo "-- Testing popen(): reading from a file using 'cat' command --\n"; 34create_files($dirpath, 1, "text_with_new_line", 0755, 100, "w", "popen_basic", 1, "bytes"); 35$filename = $dirpath."/popen_basic1.tmp"; 36$command = "cat $filename"; 37$file_handle = popen($command, "r"); 38$return_value = fpassthru($file_handle); 39echo "\n"; 40var_dump($return_value); 41pclose($file_handle); 42delete_files($dirpath, 1); 43 44echo "*** Testing popen(): writing to the pipe ***\n"; 45$arr = array("ggg", "ddd", "aaa", "sss"); 46$file_handle = popen("sort", "w"); 47$counter = 0; 48$newline = "\n"; 49foreach($arr as $str) { 50 fwrite($file_handle, (binary)$str); 51 fwrite($file_handle, (binary)$newline); 52} 53pclose($file_handle); 54 55 56echo "*** Testing for return type of popen() and pclose() functions ***\n"; 57$string = "Test String"; 58$return_value_popen = popen("echo $string", "r"); 59var_dump( is_resource($return_value_popen) ); 60fpassthru($return_value_popen); 61$return_value_pclose = pclose($return_value_popen); 62var_dump( is_int($return_value_pclose) ); 63 64echo "\n--- Done ---"; 65?> 66--CLEAN-- 67<?php 68$file_path = dirname(__FILE__); 69$dirpath = $file_path."/popen_basic"; 70unlink($dirpath."/popen_basic.tmp"); 71unlink($dirpath."/popen_basic1.tmp"); 72rmdir($dirpath); 73?> 74 75--EXPECTF-- 76*** Testing popen() and pclose() with different processes *** 77-- Testing popen(): reading from the pipe -- 78popen_basic.tmp 79-- Testing popen(): reading from a file using 'cat' command -- 80line 81line of text 82line 83line of text 84line 85line of text 86line 87line of text 88line 89line of text 90line 91line 92int(100) 93*** Testing popen(): writing to the pipe *** 94aaa 95ddd 96ggg 97sss 98*** Testing for return type of popen() and pclose() functions *** 99bool(true) 100Test String 101bool(true) 102 103--- Done --- 104