By IO we mean working with files…
There are multiple ways to read a file:
are most important ones.
The file
-function takes a filename as argument and returns an array where each array item corresponds to a line in the file.
This function takes a filename as argument and returns a string containing the complete file contents (hence the name).
touch
can be used to update the modification date of a file. If the file doesn’t exist yet, it will be created.
touch('new-file.txt'); // create new-file.txt
touch('new-file.txt'); // update modifaction timestamp of new-file.txt
fwrite
can be used to write contents to a file handle.
A handle can be created via fopen
.
$handle = fopen('file.txt', 'w'); // 'w' indicatets open file for writing
fwrite($handle, 'Hello world'); // Write hello world to opened file
fclose($handle); // close file
This sequence of opening, writing and closing a file is quite cumbersome, therefor a shorthand is also available: file_put_contents
.
A file can be deleted via unlink
.
A file can be renamed via rename
.
Similar functions exist for directories:
mkdir
: Create directory
mkdir('/path/to/dir');
rmdir
: Delete directory
rmdir('/path/to/dir');
rename
: Rename directory
rename('/path/to/dir', '/path/-to-new-dirname');
readdir
: Iterate over the files in a directory-handle
$handle = opendir('/path/to/dir');
while (false !== ($file = readdir($handle))) {
echo "$file\n";
}
glob
: List all files in a directory (as array) matching a certain pattern.
$files = glob('/path/to/dir/*.php');
File can be uploaded via a form-submission. The tag used to specify files is:
Info: In order to use the input[type=file]
, the form must specify the enctype
-attribute:
When a form containing files is submitted the $_FILES
special PHP-variable is populated with the file(s) currently uploading:
$_FILES = Array
(
[uploaded-file] => Array
(
[name] => MyFile.txt // name of the file
[type] => text/plain // filetype
[tmp_name] => /tmp/php/php1h4j1o // php stores the file in a randomized tmp location
[error] => 0 // 0 = UPLOAD_ERR_OK --> no errors
[size] => 123 // the size in bytes
)
[file2] => Array
(
[name] => MyFile.jpg
[type] => image/jpeg
[tmp_name] => /tmp/php/php6hst32
[error] => UPLOAD_ERR_OK
[size] => 98174
)
)
PHP stores the uploaded file in a temporary location. We must use the files contents or move the file to a different location. At the next request this tmp_name
will be something else. So keep this in mind.
If we opt not to read the file and process it, but move the file instead, the move_uploaded_file
should be used to move a file to a new destination.
This function has some additional checks builtin and is thus the recommended way to move uploaded files:
Create a web-page which allows to upload a file, and print the contents of the file.
Create a web-page which allows to upload a file, and:
Create a web-page which allows to upload or paste multi-fasta data and:
Create a web-page which allows to upload or paste multi-fasta data and:
Create a web-page which allows to upload or paste multi-fasta data and:
Create a web-page which allows to upload a file or paste some text, and specify a list of terms.
the|red a|green an
In the previous exercises we created a single page application capable of receiving DNA sequences and displaying the nucleotides sequences colored on screen with an additional frequency graph.
Let’s now extend this single page application into a multi page web-app.
This app is composed out of three pages:
All pages also share a common navigation bar and styles.
Create a paste-bin web-app. The app allows the user to create a new snippet and save it (eq. write to file) via a webpage. The user can browse all snippets and view them by clicking a link…
This app is composed out of three pages: