Check out Scrivo

Do you want to try out Scrivo? Then here's a demo for you that does not just get your feet wet but lets you plunge right in.

Contact us

For more information, please contact us. We're happy to help you out!

Next Oct 20 Previous

Creating nothing

Which function(s) can be used to create an empty file that persists after the script ends, choose all that apply:

A: file_put_contents()
B: fopen()
C: touch()
D: tmpfile()

Answer

You can use all functions to create a new empty file. See the following code fragment for an example:

<?php
file_put_contents("file1.txt", "");
fopen("file2.txt", "c");
touch("file3.txt");
tmpfile();
?>

Obviously file_put_contents() on line 2 does just wat it suggests it does. Also note that opening a file in create "c" or write "w" mode is enough to create a file. It is not necessary to close the file with fclose() because this will be done by PHP when the script ends (but it is very much appriciated, thank you).

touch() might be a rather uncommon function to many. But this it wat touch() does: update the file time if the file exists or create a file if it doen't.

tempfile() however creates a temporary file that you can use in your script as some sort of local facility. For instance to store an intermediate result when parsing some data. But the file will be deleted by PHP when the script ends. Therefore answers A, B and C are correct and D is false.