Working With Files Flashcards

1
Q

How to check if a file named “example.txt” exists in PHP

A

if (file_exists(‘example.txt’)) {
echo “file exists”;
} else {
echo “file does not exist”;
}

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
2
Q

How do you open a file named “example.txt” for reading in PHP

A

$handle = fopen(‘example.txt’, ‘r’);
if ($handle) {
// File is open and ready for reading.
} else {
echo “Failed to open the file.”;
}

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
3
Q

How do you close:
$handle = fopen(‘example.txt’, ‘r’);

A

fclose($handle);

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
4
Q

How do you get the size of a file named “example.txt”. Save it to a variable named $size

A

$size = filesize(‘example.txt’);

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
5
Q

Create, open, write to, and then close a file named “example.txt”. The file should contain “Hello World” inside it.

Do it in one operation.

A

file_put_contents(‘example.txt’, “Hello World”);

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
6
Q

How to read the entire content of a file named “example.txt” into a string. If there is no file found then it should echo “Failed to read the file.”

Save it to a variable called $content

A

$content = file_get_contents(‘example.txt’);
if ($content !== false) {
echo $content;
} else {
echo “Failed to read the file.”;
}

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
7
Q

$content = file_get_contents(‘example.txt’);

A

Reads entire content of example.txt into $content.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
8
Q

file_exists(‘example.txt’);

A

Returns true if example.txt exists; false otherwise.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
9
Q

file_put_contents(‘example.txt’, “Hello, World!”);

A

Writes “Hello, World!” To example.txt, overwriting existing content.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
10
Q

What is this line of code likely doing?

define(‘APP_PATH’, dirname(__FILE__) . ‘/../‘;

A

Defines a constant ‘APP_PATH’ that holds the path to the parent directory of the current script file.

‘dirname(__FILE__)’ gets the directory of the current file, and appending ‘/../‘ navigates one directory up.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly