Skip to content

Instantly share code, notes, and snippets.

@locvfx
Last active November 14, 2017 04:24
Show Gist options
  • Save locvfx/f01b7bdc813b7e777935ac40e0daee75 to your computer and use it in GitHub Desktop.
Save locvfx/f01b7bdc813b7e777935ac40e0daee75 to your computer and use it in GitHub Desktop.
//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')...
//Read a File
$my_file = 'file.txt';
$handle = fopen($my_file, 'r');
$data = fread($handle,filesize($my_file));
//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);
//Append to a File
$my_file = 'file.txt';
$handle = fopen($my_file, 'a') or die('Cannot open file: '.$my_file);
$data = 'New data line 1';
fwrite($handle, $data);
$new_data = "\n".'New data line 2';
fwrite($handle, $new_data);
//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);
//Create a folder if it is not exist
if (!file_exists('path/to/directory')) {
mkdir('path/to/directory', 0777, true);
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment