PHP Set 6 Flashcards

1
Q

How can you read a file in PHP and what is the syntax?

A

readfile() function allows you to read a file and write it to the output buffer
Syntax:
readfile(string $filename, bool $use_include_path=false, resource $context): int | false

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

What does the readile function return?

A

Returns the number of bytes read from the file on success or false on failure

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

How do you open a file and what is the syntax?

A

Using the fopen() function
Syntax:
fopen(string $filename, string $mode, bool $use_include_path=false, resource $context): resource | false

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

What does the fopen function return?

A

Returns a file pointer resource on success or false on failure

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

What are the possible modes you can select when opening a file?

A
  1. ‘r’ is to open for reading only
  2. ‘r+’ is to open for reading and writing
  3. ‘w’ is to open for writing only
  4. ‘w+’ is to open for reading and writing
  5. ‘a’ is to open for writing only
  6. ‘a+’ is to open for reading and writing
  7. ‘x’ is to create and open for writing only
  8. ‘x+’ is to create and open for reading and writing
  9. ‘c’ is to open the file for writing only
  10. ‘c+’ is to open the file for reading and writing
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
6
Q

What is the difference between ‘w’, ‘a’, ‘x’, and ‘c’ file open modes since they are all used for writing only?

A

w places the file pointer at the beginning of the file and truncates the file to zero length before writing. If the file does not exist it will try to create it

a places the file pointer at the end of the file. fseek function has not effect as writes are always appended. If the file does not exist it will try to create it

x places the file pointer at the beginning of the file. If the file already exists the open call will fail and an E_WARNING error will be generated

c positions the file pointer at the beginning of the file. It will not truncate it or throw an error if the file already exists.

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

What function is used to read from an open file and what is the syntax?

A

Use the fread() function to read up to a specified number of bytes from the stream created by fopen()
Syntax:
fread(resource $stream, int $length): string | false

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

What does the fread function return?

A

Returns the read data as a string on success or false on failure

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

How can you get the size of a file in bytes?

A

Using the filesize(string $filename) function

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

How do you close a file?

A

Using the fclose(resource $stream) function

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

What does the fgets function do and what is its syntax?

A

It is used to read a single line from an opened file
Syntax:
fgetsIresource $handle, int $length=?): string | false

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

What does the fgets function return?

A

Returns a string of up to length-1 bytes read from the file on success or false on failure

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

What is the difference between fread and fgets?

A

fread is a binary-safe file read and reads up to the number of bytes that must be specified.
fgets gets a line from a file pointer, it reads a line and stops when it encounters a newline if a length is not provided

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

What does the fclose function return?

A

Returns true on success or false on failure

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

What does the feof function do and what is its syntax?

A

Checks if the end of file has been reached
Syntax:
feof(resource $stream): bool

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

What does the feof function return?

A

Returns true if EOF has been reached or an error occurs. Returns false otherwise

17
Q

What function is used to write to a file and what is its syntax?

A

Use the fwrite() function to write to a file open for writing
Syntax:
fwrite(resource $stream, string $string, int $length): int | false

18
Q

What does the fwrite function return?

A

Returns the number of bytes written on success or false on error

19
Q

What do you need to do in order to enable uploading files to a web server?

A

Configure the php.ini file and specify file_uploads = on
Create an HTML form with a file upload input
On the server side, use the move_uploaded_file function to move the uploaded file to a space on the server’s hard disk

20
Q

Why should you use filters in PHP?

A

Many web application receive external input and you should always validate external data before using it

21
Q

What are the 2 types of filters?

A
  1. Validating data: Determines if data is in the proper form and meets certain qualifications
  2. Sanitizing data: Removes illegal characters from the data so it may alter the data
22
Q

What function is used to filter input and what is the syntax?

A

filter_var() filters a variable with a specified filter
Syntax:
filter_var($var, filtername, options)
var is the variable to filter
filtername is the ID or name of the filter to use (FILTER_DEFAULT by default which is not filtering)
options specifies one or more flags to use

23
Q

What does the filter_var function return?

A

Returns the filtered data on success or false on failure

24
Q

What are the possible validate filters available?

A
  1. FILTER_VALIDATE_BOOL
  2. FILTER_VALIDATE_BOOLEAN
  3. FILTER_VALIDATE_DOMAIN
  4. FILTER_VALIDATE_EMAIL
  5. FILTER_VALIDATE_FLOAT
  6. FILTER_VALIDATE_INT
  7. FILTER_VALIDATE_IP
  8. FILTER_VALIDATE_MAC
  9. FILTER_VALIDATE_URL
  10. FILTER_VALIDATE_REGEXP
25
Q

What are the possible sanitize filters?

A
  1. FILTER_SANITIZE_EMAIL
  2. FILTER_SANITIZE_ENCODED
  3. FILTER_SANITIZE_MAGIC_QUOTES
  4. FILTER_SANITIZE_ADD_SLASHES
  5. FILTER_SANITIZE_NUMBER_FLOAT
  6. FILTER_SANITIZE_NUMBER_INT
  7. FILTER_SANITIZE_SPECIAL_CHARS
  8. FILTER_SANITIZE_FULL_SPECIAL_CHARS
  9. FILTER_SANITIZE_STRING
  10. FILTER_SANITIZE_STRIPPED
  11. FILTER_SANITIZE_URL
  12. FILTER_UNSAFE_RAW
26
Q

What are the options available for FILTER_VALIDATE_URL?

A
  1. FILTER_FLAG_SCHEME_REQUIRED: Ensures URL is RFC compliant
  2. FILTER_FLAG_HOST_REQUIRED: Ensures URL includes host name
  3. FLITER_FLAG_PATH_REQUIRED: Ensures the URL has a path after the domain name
  4. FILTER_FLAG_QUERY_REQUIRED: Ensures the URL has a query string attched