File - adampatterson/Dingo-Framework GitHub Wiki

###Overview The file library allows you to open, read, and write files in an object-oriented fashion. To load the file library manually you may do this:

load::library('file');

###Read Reading the contents of an entire file can be done like so:

$file = new file('myfile.txt','r');
$data = $file->read();

###Write Writing data to a file is done like this:

$file = new file('myfile.txt','w');
$file->write('Hello, World!');

###Read + Write You can read and write to a file from the same file object:

$file = new file('myfile.txt','r+');
$data = $file->read();
$file->write('End of file!');

###Seek You can move the position of the file pointer in the file with the seek() method. In this example assume 'myfile.txt' contains 'Hello World!':

$file = new file('myfile.txt','r');
$file->seek(6);
echo $file->read(); // Outputs: 'World!'

###Custom File Locking By default the files class sets a LOCK_EX file lock to all files it opens. You can however change the file lock whatever other file lock mode you want to by changing the optional third argument:

$file = new file('myfile.txt','r+',LOCK_SH);
// Do something...

###Scan You can scan through a file easily with the scan() method. Scan is a wrapper for the fscanf() PHP function, so any syntax that works with that will also work with this:

$file = new file('myfile.txt','r');

while($data = $fh->scan("%s\n"))
{
    list($line) = $data;
    echo "$line<br />";
}

###Temporary Files By omitting all arguments you can open up and manipulate a new temporary file. For more info on temporary files see here.

$file = new file();
// Do something...

###Close By default the file will close itself when the file object destructs. You can however close the file connection at any time via the close() method if you choose to do so:

$file = new file('myfile.txt','r+');
// Do something...
$file->close();

###Mime Returns the MIME-Type of a file.

echo file::mime('resume.doc');

###Download Sends a file to the browser to be downloaded. The first argument is the location of the file to be downloaded. The second argument is the file name the user will see.

file::download('localfile123.pdf','readme.pdf');
⚠️ **GitHub.com Fallback** ⚠️