echo "-- Iteration {$count} with file containing {$type} Data--\n"; foreach ($file_modes as $mode) { echo "-- File opened in {$mode} mode --\n"; // creating the file except for x mode if (substr($mode, 0, 1) != "x") { $file_handle = fopen($file_name, "w"); if ($file_handle == false) { exit("Error:failed to open file {$file_name}"); } // filling the file some data if mode is append mode if (substr($mode, 0, 1) == "a") { fill_file($file_handle, $type, 10); } fclose($file_handle); } // opening the file in different modes $file_handle = fopen($file_name, $mode); if ($file_handle == false) { exit("Error:failed to open file {$file_name}"); } // writing data to the file var_dump(fill_file($file_handle, $type, 50)); var_dump(fflush($file_handle)); fclose($file_handle); // reading the contents of the file after flushing var_dump(readfile($file_name)); unlink($file_name); } $count++; } echo "\n*** Done ***";
Prototype: string fgets ( resource $handle [, int $length] ); Description: Gets a line from file pointer */ // include the file.inc for common test funcitons include "file.inc"; $file_modes = array("w+", "w+b", "w+t", "a+", "a+b", "a+t", "x+", "x+b", "x+t"); $file_content_types = array("numeric", "text", "text_with_new_line", "alphanumeric"); echo "*** Testing fgets() : usage variations ***\n"; $filename = dirname(__FILE__) . "/fgets_variation3.tmp"; foreach ($file_modes as $file_mode) { echo "\n-- Testing fgets() with file opened using mode {$file_mode} --\n"; foreach ($file_content_types as $file_content_type) { echo "-- File content type : {$file_content_type} --\n"; /* create files with $file_content_type */ $file_handle = fopen($filename, $file_mode); $data = fill_file($file_handle, $file_content_type, 50); if (!$file_handle) { echo "Error: failed to open file {$filename}!"; exit; } echo "-- fgets() with default length, file pointer at 0 --\n"; // get the file pointer to beginning of the file rewind($file_handle); var_dump(ftell($file_handle)); var_dump(fgets($file_handle)); // with default length var_dump(ftell($file_handle)); // ensure the file pointer position var_dump(feof($file_handle)); // enusre if eof set echo "-- fgets() with length = 23, Expected: 22 chars, file pointer at 0 --\n";
/* Prototype: int readfile ( string $filename [, bool $use_include_path [, resource $context]] ); Description: Outputs a file */ /* test readfile() by providing an include path, second argument */ // include file.inc require "file.inc"; $file_path = dirname(__FILE__); $dirname = "{$file_path}/readfile_variation3"; echo "*** Testing readfile(): checking second argument, include path ***\n"; // temp dir created mkdir($dirname); // temp file name used here $filename = "{$dirname}/readfile_variation3.tmp"; // create file $fp = fopen($filename, "w"); fill_file($fp, "text_with_new_line", 50); fclose($fp); // including $dirname in 'include_path' ini_set('include_path', $dirname); // 'include_path' set to true $count = readfile("readfile_variation3.tmp", true); echo "\n"; var_dump($count); // use the context argument with include path echo "*** Testing readfile(): checking second argument, include path with context specified ***\n"; $context = stream_context_create(); $count = readfile("readfile_variation3.tmp", true, $context); echo "\n"; var_dump($count); echo "Done\n"; error_reporting(0);
// include common file related test functions include "file.inc"; echo "*** Testing ftruncate() : usage variations ***\n"; /* test ftruncate with file opened in different modes */ $file_modes = array("r", "rb", "rt", "r+", "r+b", "r+t", "w", "wb", "wt", "w+", "w+b", "w+t", "x", "xb", "xt", "x+", "x+b", "x+t", "a", "ab", "at", "a+", "a+b", "a+t"); $file_content_types = array("numeric", "text_with_new_line"); foreach ($file_content_types as $file_content_type) { echo "\n-- Testing ftruncate() with file having data of type " . $file_content_type . " --\n"; for ($mode_counter = 0; $mode_counter < count($file_modes); $mode_counter++) { echo "-- Testing ftruncate() with file opening using {$file_modes[$mode_counter]} mode --\n"; // create 1 file with some contents $filename = dirname(__FILE__) . "/ftruncate_variation1.tmp"; if (strstr($file_modes[$mode_counter], "x") || strstr($file_modes[$mode_counter], "w")) { // fopen the file using the $file_modes $file_handle = fopen($filename, $file_modes[$mode_counter]); fill_file($file_handle, $file_content_type, 1024); } else { create_files(dirname(__FILE__), 1, $file_content_type, 0755, 1, "w", "ftruncate_variation"); // fopen the file using the $file_modes $file_handle = fopen($filename, $file_modes[$mode_counter]); } if (!$file_handle) { echo "Error: failed to open file {$filename}!\n"; exit; } rewind($file_handle); // file pointer to 0 /* truncate it to size 0 */ echo "-- Testing ftruncate(): truncate file to size = 0 --\n"; $new_size = 0; var_dump(filesize($filename));
// common file used require dirname(__FILE__) . '/file.inc'; echo "*** Testing readfile() : basic functionality ***\n"; $file_path = dirname(__FILE__); $file_prefix = "readfile_basic"; // temp files created with this prefix // the content that is filled into the temp files as created $filetypes = array("numeric", "text", "empty", "alphanumeric", "text_with_new_line"); // different file modes $filemodes = array("w", "wt", "wb", "w+", "w+b", "w+t", "a", "at", "ab", "a+", "a+b", "a+t", "x", "xb", "xt", "x+", "x+b", "x+t"); // create file, read the file content, delete file foreach ($filetypes as $type) { echo "\n-- File filled with content type: {$type} --\n"; foreach ($filemodes as $mode) { echo "-- File opened with mode: {$mode} --\n"; if (strstr($mode, "x")) { $fp = fopen($file_path . "/" . $file_prefix . "1.tmp", $mode); fill_file($fp, $type, 100); fclose($fp); } else { // creating file in write mode create_files($file_path, 1, $type, 0755, 100, $mode, $file_prefix, 1, "byte"); } $count = readfile($file_path . "/" . $file_prefix . "1.tmp"); echo "\n"; var_dump($count); // delete files created delete_files($file_path, 1, $file_prefix, 1); } } echo "Done\n";