<?php /* Prototype: string file_get_contents( string $filename[, bool $use_include_path[, * resource $context[, int $offset[, int $maxlen]]]] ) * Description: Reads entire file into a string */ /* Prototype: int file_put_contents( string $filename, mixed $data[,int $flags[, resource $context]] ) * Description: Write a string to a file */ /* Testing variation in all argument values */ $file_path = dirname(__FILE__); include $file_path . "/file.inc"; echo "*** Testing with variations in the arguments values ***\n"; $buffer_types = array("text", "numeric", "text_with_new_line", "alphanumeric"); foreach ($buffer_types as $type) { fill_buffer($buffer, $type, 100); file_put_contents($file_path . "/file_put_contents_variation1.tmp", $buffer); var_dump(file_get_contents($file_path . "/file_put_contents_variation1.tmp", 0)); var_dump(file_get_contents($file_path . "/file_put_contents_variation1.tmp", 1)); var_dump(file_get_contents($file_path . "/file_put_contents_variation1.tmp", 0, NULL, 5)); var_dump(file_get_contents($file_path . "/file_put_contents_variation1.tmp", 1, NULL, 5)); var_dump(file_get_contents($file_path . "/file_put_contents_variation1.tmp", 0, NULL, 5, 20)); var_dump(file_get_contents($file_path . "/file_put_contents_variation1.tmp", 1, NULL, 5, 20)); } echo "--- Done ---"; error_reporting(0); //Deleting the temporary file $file_path = dirname(__FILE__); unlink($file_path . "/file_put_contents_variation1.tmp");
<?php /* Prototype: string file_get_contents( string $filename[, bool $use_include_path[, * resource $context[, int $offset[, int $maxlen]]]] ) * Description: Reads entire file into a string */ /* Prototype: int file_put_contents( string $filename, mixed $data[, int $flags[, resource $context]] ) * Description: Write a string to a file */ $file_path = dirname(__FILE__); include $file_path . "/file.inc"; echo "*** Testing the basic functionality of file_put_contents() and file_get_contents() functions ***\n"; echo "-- Testing with simple valid data file --\n"; $file_name = "/file_put_contents.tmp"; fill_buffer($text_buffer, "text", 100); file_put_contents($file_path . $file_name, $text_buffer); var_dump(file_get_contents($file_path . $file_name)); echo "\n-- Testing with empty file --\n"; $file_name = "/file_put_contents1.tmp"; file_put_contents($file_path . $file_name, ""); var_dump(file_get_contents($file_path . $file_name)); echo "\n*** Done ***";
$offset = array(-1, 0, 1, 512, 600); // different offsets $filename = dirname(__FILE__) . "/fseek_ftell_rewind_variation6.tmp"; // this is name of the file created by create_files() /* open the file using $files_modes and perform fseek(),ftell() and rewind() on it */ foreach ($file_content_types as $file_content_type) { echo "\n-- File having data of type " . $file_content_type . " --\n"; foreach ($file_modes as $file_mode) { echo "-- File opened in mode " . $file_mode . " --\n"; $file_handle = fopen($filename, $file_mode); if (!$file_handle) { echo "Error: failed to fopen() file: {$filename}!"; exit; } $data_to_be_written = ""; fill_buffer($data_to_be_written, $file_content_type, 512); //get the data of size 512 $data_to_be_written = $data_to_be_written; fwrite($file_handle, (string) $data_to_be_written); rewind($file_handle); foreach ($offset as $count) { var_dump(fseek($file_handle, $count, SEEK_CUR)); var_dump(ftell($file_handle)); // confirm the file pointer position var_dump(feof($file_handle)); //ensure that file pointer is not at end } //end of offset loop //close the file and check the size fclose($file_handle); var_dump(filesize($filename));
<?php /* Prototype: string file_get_contents( string $filename[, bool $use_include_path[, * resource $context[, int $offset[, int $maxlen]]]] ) * Description: Reads entire file into a string */ /* Prototype: int file_put_contents( string $filename, mixed $data[,int $flags[, resource $context]] ) * Description: Write a string to a file */ /* Testing variation using use_include_path argument */ $file_path = dirname(__FILE__); include $file_path . "/file.inc"; echo "*** Testing with variation in use_include_path argument ***\n"; $dir = "file_get_contents_variation2"; mkdir($file_path . "/" . $dir); $filename = $file_path . "/" . $dir . "/" . "file_get_contents_variation2.tmp"; ini_set('include_path', $file_path . "/" . $dir); $data_array = array(1, " Data1 in an array", 2, " Data2 in an array"); fill_buffer($buffer, "text", 100); file_put_contents($filename, $buffer); fill_buffer($buffer, "numeric", 100); file_put_contents($filename, $buffer, FILE_APPEND, NULL); file_put_contents($filename, $data_array, FILE_APPEND, NULL); var_dump(file_get_contents($filename, 0)); var_dump(file_get_contents($filename, 1)); var_dump(file_get_contents($filename, 0, NULL, 5)); var_dump(file_get_contents($filename, 1, NULL, 5)); var_dump(file_get_contents($filename, 0, NULL, 5, 20)); var_dump(file_get_contents($filename, 1, NULL, 5, 20)); echo "--- Done ---";