PHP file upload Questions with Answers

10 practice questions on PHP file upload (PHP) with answers and step-by-step solutions. Try 10 samples below, then practise all 10 free in the AptiNest app.

Sample questions

Question 1Medium

If you want to temporarily store uploaded files in the /tmp/phpuploads/ directory, which one of the following statement will you use?

  1. upload_tmp_dir '/tmp/phpuploads/ directory'
  2. upload_dir '/tmp/phpuploads/ directory'
  3. upload_temp_dir '/tmp/phpuploads/ directory'
  4. upload_temp_director '/tmp/phpuploads/ directory'

Answer: A. upload_tmp_dir '/tmp/phpuploads/ directory'

Explanation

Because an uploaded file must be successfully transferred to the server before subsequent processing on that file can begin, a staging area of sorts must be designated for such files as the location where they can be temporarily placed until they are moved to their final location.
For example, suppose you want to temporarily store uploaded files in the /tmp/phpuploads/ directory. You would use the following:
upload_tmp_dir = "/tmp/phpuploads/"

Step-by-step solution

  1. The directive that sets the folder for temporary uploaded files is upload_tmp_dir.
  2. Its value is set in quotes, for example upload_tmp_dir = "/tmp/phpuploads/".
  3. Option A uses the right directive name, so it is the correct answer.

Why the other options are wrong

  • B. Upload_dir is not a PHP directive name.
  • C. Upload_temp_dir is not the directive name; it is upload_tmp_dir.
  • D. Upload_temp_director is misspelled and is not a directive.

Tip: Upload settings start with upload_, and the temporary folder directive is upload_tmp_dir.

Question 2Medium

What is default size of a file set in upload_max_filesize?

  1. 1 MB
  2. 2 MB
  3. 2.5 MB
  4. 3 MB

Answer: B. 2 MB

Explanation

The default values for PHP will restrict you to a maximum 2 MB upload file size.

Step-by-step solution

  1. The question asks for the default value of upload_max_filesize.
  2. The explanation says the PHP default restricts uploads to a maximum of 2 MB.
  3. So option B, 2 MB, is the answer.

Why the other options are wrong

  • A. 1 MB is lower than the default of 2 MB.
  • C. 2.5 MB is larger than the default of 2 MB.
  • D. 3 MB is larger than the default of 2 MB.

Tip: Large uploads often need a higher upload_max_filesize in php.ini.

Question 3Medium

How many items are available in the $_FILES array?

  1. 2
  2. 3
  3. 4
  4. 5

Answer: D. 5

Explanation

$_FILEs['userfile']['error'], $_FILEs['userfile']['name'], $_FILEs['userfile']['size'], $_FILEs['userfile']['tmp_name'], $_FILEs['userfile']['type'] are the five items in the array.

Step-by-step solution

  1. The question asks how many items the FILES array holds for one uploaded file.
  2. The listed keys are name, type, tmp_name, error and size.
  3. That is five items, so the correct answer is option D, 5.

Why the other options are wrong

  • A. Two is too few; the entry lists five keys.
  • B. Three is too few; the entry lists five keys.
  • C. Four misses one key, because size is also listed.

Tip: Count the named keys when a question asks how many items an array holds.

Question 4Medium

Which directive sets a maximum allowable amount of memory in megabytes that a script can allow?

  1. max_size
  2. post_max_size
  3. max_memory_limit
  4. memory_limit

Answer: D. memory_limit

Explanation

This sets the maximum amount of memory in bytes that a script is allowed to allocate. This helps prevent poorly written scripts for eating up all available memory on a server. Note that to have no memory limit, set this directive to-1.Its default value is 16M.

Step-by-step solution

  1. The question asks for the directive that sets the maximum memory a script may allocate.
  2. Post_max_size limits the size of POST data, not the script's memory.
  3. Max_size and max_memory_limit are not the PHP memory directive names.
  4. The directive that sets this cap is memory_limit, which is the correct answer (option D).

Why the other options are wrong

  • A. Max_size is not a PHP directive for script memory.
  • B. Post_max_size limits POST data size, not the memory a script can allocate.
  • C. Max_memory_limit is not a PHP directive name.

Tip: For PHP limits, memory_limit controls script memory and post_max_size controls request data.

Question 5Medium

Since which version of PHP was the directive max_file_uploads available.

  1. PHP 5.2.1
  2. PHP 5.2.2
  3. PHP 5.2.12
  4. PHP 5.2.21

Answer: C. PHP 5.2.12

Explanation

The max_file_uploads directive sets an upper limit on the number of files which can be simultaneously uploaded.

Step-by-step solution

  1. The question asks which PHP version added this upload limit directive.
  2. The explanation says the directive sets an upper limit on the number of files uploaded at the same time.
  3. That directive was added in PHP 5.2.12, so option C is the answer.

Why the other options are wrong

  • A. PHP 5.2.1 is earlier than the version that added this directive.
  • B. PHP 5.2.2 is earlier than the version that added this directive.
  • D. PHP 5.2.21 is not the version that added this directive.

Tip: For version questions, check the exact release number, since patch versions can differ.

Question 6Medium

Which function is used to determine whether a file was uploaded?

  1. is_file_uploaded()
  2. is_uploaded_file()
  3. file_uploaded('filename')
  4. uploaded_file('filename')

Answer: B. is_uploaded_file()

Explanation

is_uploaded_file() returns TRUE when the named file was uploaded through HTTP POST. The other function names are not PHP functions.

Step-by-step solution

  1. is_uploaded_file() checks if a file was uploaded via HTTP POST.
  2. It takes the filename as input and returns TRUE if the file is uploaded.
  3. The function is part of PHP's file handling functions for security checks.
  4. Other options either do not exist or are not used for this purpose.
  5. Correct answer is option B: is_uploaded_file()
  6. Final Answer: is_uploaded_file()

Why the other options are wrong

  • A. is_file_uploaded() is not a PHP function; the check is done with is_uploaded_file().
  • C. file_uploaded() is not a PHP function; is_uploaded_file() checks the upload.
  • D. uploaded_file() is not a PHP function; is_uploaded_file() checks the upload.

Tip: Use is_uploaded_file() to safely check for uploaded files in PHP.

Question 7Medium

What is the default value of max_execution_time directive?

  1. 30 seconds
  2. 60 seconds
  3. 120 seconds
  4. 1 second

Answer: A. 30 seconds

Explanation

The default value of max_input_time directive is 30 sec.

Step-by-step solution

  1. The question asks for the built-in default of max_execution_time.
  2. PHP sets max_execution_time to 30 seconds by default.
  3. 30 seconds is option A, the correct answer.

Why the other options are wrong

  • B. 60 seconds is not the default; the default is 30 seconds.
  • C. 120 seconds is not the default value.
  • D. 1 second is not the default and would stop most scripts at once.

Tip: Learn the common php.ini defaults, such as 30 seconds for max_execution_time, and check the php.ini file when a limit matters.

Question 8Medium

What is the default value of the directive max_file_uploads?

  1. 10 files
  2. 15 files
  3. 20 files
  4. 25 files

Answer: C. 20 files

Explanation

PHP 5.2.12 had added a new INI directive called max_file_uploads, designed to prevent DOS attacks. The supposed attack would work by uploading a huge number of files to a server, filling up the available space in its temp folder. The default setting for max_file_uploads is 20 files.

Step-by-step solution

  1. The upload limit directive defaults to 20 files.
  2. So option C is correct, which is 20 files.

Why the other options are wrong

  • A. 10 is not the default.
  • B. 15 is not the default.
  • D. 25 is not the default.

Tip: PHP max_file_uploads defaults to 20.

Question 9Medium

Which directive determines whether PHP scripts on the server can accept file uploads?

  1. file_uploads
  2. file_upload
  3. file_input
  4. file_intake

Answer: A. file_uploads

Explanation

This directive determines that PHP scripts on the server can allow the file uploads or not.
file_uploads= On / Off
Scope: PHP_INI_SYSTEM; Default value: 1

Step-by-step solution

  1. PHP has a directive called file_uploads that controls file uploads.
  2. This directive is set to On or Off to allow or disallow uploads.
  3. The correct option matches the exact directive name used in PHP.
  4. Other options like file_upload or file_input are not valid directives.
  5. Therefore, the answer is file_uploads.

Why the other options are wrong

  • B. file_upload is not the directive name; the real directive is file_uploads, with an s.
  • C. file_input is not a PHP directive that controls file uploads.
  • D. file_intake is not a PHP directive that controls file uploads.

Tip: Remember: 'file_uploads' is the only valid directive for enabling/disabling file uploads in PHP.

Question 10Medium

Which superglobal stores a variety of information pertinent to a file uploaded to the server via a PHP script?

  1. $_FILE Array
  2. $_FILES Array
  3. $_FILES_UPLOADED Array
  4. $_FILE_UPLOADED Array

Answer: B. $_FILES Array

Explanation

By using the global PHP $_FILES array you can upload files from a client computer to the remote server.The first parameter is the form's input name and the second index can be either "name", "type", "size", "tmp_name" or "error".

Step-by-step solution

  1. The superglobal that holds details of uploaded files is $_FILES.
  2. Option B is the intended answer; its spelling $_FILEs is a slightly wrong form of $_FILES.
  3. $_FILES holds the name, type, size, tmp_name and error for each uploaded file.

Why the other options are wrong

  • A. $_FILE is missing the final S; the real superglobal is $_FILES.
  • C. $_FILES_UPLOADED is not a PHP superglobal.
  • D. $_FILE_UPLOADED is not a PHP superglobal.

Tip: Uploaded file details are in $_FILES, indexed by the form field name.

Practise all 10 questions free

The full question bank with timed practice, progress tracking and Vidya, your AI tutor. Free, no ads.

Open this topic in the app →

Related topics

See all PHP topics · All topics