Skip to content

Instantly share code, notes, and snippets.

@bbatsche
Created May 21, 2014 16:21
Show Gist options
  • Save bbatsche/4dff97d69d9e5da37c3f to your computer and use it in GitHub Desktop.
Save bbatsche/4dff97d69d9e5da37c3f to your computer and use it in GitHub Desktop.
PHP File Handling Notes
<?php
/************************************
* NOTE!!!!
* This file is meant as notes / examples.
* The code, as it's current written, will not work
* and will probably output some errors/notices.
* Use this file as examples, not as code that's meant to be run.
************************************
*/
// Different paths to the same file
$filename = 'princesses.txt';
$filename = '../notes/princesses.txt';
$filename = '/vagrant/notes/princesses.txt';
// Magic constants so we can see where our file/director are
echo __FILE__ . PHP_EOL;
echo __DIR__ . PHP_EOL;
// Boolean, checks if the file exists and can be read by our script
is_readable($filename);
// Get the fize of the file, useful for reading entire file at once
$fileSize = filesize($filename);
// File pointer. Describes:
// 1) Which file
// 2) What we can/can't do with the file (read, write, etc)
// 3) Where we are in the file
$handle = fopen($filename, 'r');
// Check if the file pointer has reached the end of the file
feof($handle);
// Pull back the entire contents of the file as a single string
$princessString = fread($handle, $fileSize);
$princessString = fread($handle, filesize($filename));
// Acoustics Princess\n
// Bee Princess\n
// Bounce House Princess\n
// Breakfast Princess\n
// etc..
// Always close your files when you're done with them!
fclose($handle);
function readBytesATime($filename, $bytes = 100) {
$contents = '';
if (is_readable($filename)) {
$handle = fopen($filename, 'r');
while(!feof($handle)) {
$contents .= fread($handle, $bytes);
sleep(1);
}
fclose($handle);
}
return $contents;
}
fwrite(STDOUT, readBytesATime($filename));
// Remove extra \n at the end of the file
$princessString = trim($princessString);
// Break our string apart into an array
$princesses = explode(PHP_EOL, $princessString);
// Sort our princesses alphabetically
sort($princesses);
// Other modes for opening files
// Open for write, truncate the file
$handle = fopen('sorted_princesses.txt', 'w');
// Open for write, put pointer at END
$handle = fopen('sorted_princesses.txt', 'a');
// Open for write, put pointer at BEGINNING
$handle = fopen('sorted_princesses.txt', 'c');
// Loop through princesses writing them out with a new line at the end
foreach ($princesses as $princess) {
fwrite($handle, $princess . PHP_EOL);
}
// Glue the $princesses array together, add a final newline character at the end
fwrite($handle, implode("\n", $princesses) . "\n");
// Pick a random index
$hots = array_rand($princesses);
// Open for appending so we can keep track of our history
$handle = fopen('hots.log', 'a');
// Write to our file, append new data
fwrite($handle, "The Ice King totally has the hots for {$princesses[$hots]}!\n");
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment