Navigation Menu

Skip to content

Instantly share code, notes, and snippets.

@benmarks
Created December 18, 2013 00:09
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save benmarks/8015156 to your computer and use it in GitHub Desktop.
Save benmarks/8015156 to your computer and use it in GitHub Desktop.
Simple file operations via David Walsh
<?php
/**
* ref http://davidwalsh.name/basic-php-file-handling-create-open-read-write-append-close-delete
*/
header('Content-Type: text/plain');
//##Create a File
//$my_file = 'file.txt';
//$handle = fopen($my_file, 'w') or die('Cannot open file: '.$my_file); //implicitly creates file
##Open a File
//$my_file = 'file.txt';
//$handle = fopen($my_file, 'w') or die('Cannot open file: '.$my_file); //open file for writing ('w','r','a')...
##Write to a File
//$my_file = 'file.txt';
//$handle = fopen($my_file, 'w') or die('Cannot open file: '.$my_file);
//$data = 'This is the data';
//fwrite($handle, $data);
//##Read a File
//$my_file = 'file.txt';
//$handle = fopen($my_file, 'r');
//$data = fread($handle,filesize($my_file));
//echo $data;
##Append to a File
//$my_file = 'file.txt';
//$handle = fopen($my_file, 'a') or die('Cannot open file: '.$my_file);
//$data = "\n".'New data line 1';
//fwrite($handle, $data);
//$new_data = "\n".'New data line 2';
//fwrite($handle, $new_data);
//$read = file_get_contents($my_file);
//echo $read;
//##Close a File
//$my_file = 'file.txt';
//$handle = fopen($my_file, 'w') or die('Cannot open file: '.$my_file);
////write some data here
//fclose($handle);
//##Delete a File
//$my_file = 'file.txt';
//unlink($my_file);
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment