MCQs Of Day 12 File Uploads
MCQs Of Day 12
File Uploads
Basic Level Questions
1. Which HTML attribute is required to enable file uploads in a form?
o (a) method="file"
o (b) enctype="multipart/form-data" ✅
o (c) type="file"
o (d) accept="*"
Explanation: The enctype="multipart/form-data" is necessary to submit binary data (like files) through an HTML form.
2. What Laravel method is used to handle uploaded files?
o (a) $request->input()
o (b) $request->file() ✅
o (c) $request->data()
o (d) $request->upload()
Explanation: The $request->file() method retrieves the uploaded file.
3. Which method saves an uploaded file to a specified directory?
o (a) move()
o (b) store() ✅
o (c) save()
o (d) upload()
Explanation: The store() method saves the uploaded file to a predefined storage location.
4. What is the default storage directory in Laravel for uploaded files?
o (a) /uploads
o (b) /storage/app ✅
o (c) /public/uploads
o (d) /resources/files
Explanation: Files are stored in the /storage/app directory unless configured otherwise.
5. Which method is used to specify a custom filename during upload?
o (a) store()
o (b) storeAs() ✅
o (c) saveAs()
o (d) uploadAs()
Explanation: The storeAs() method allows specifying a custom file name.
6. What does the getClientOriginalName() method return?
o (a) File size
o (b) File MIME type
o (c) Original filename ✅
o (d) Storage path
Explanation: The getClientOriginalName() method retrieves the original name of the uploaded file.
7. Which method retrieves the MIME type of the uploaded file?
o (a) getMimeType() ✅
o (b) getClientType()
o (c) fileMimeType()
o (d) mimeType()
Explanation: The getMimeType() method returns the MIME type of the file.
8. What validation rule is used to ensure only image files are uploaded?
o (a) file
o (b) mimes:jpg,png,gif ✅
o (c) size:images
o (d) type:image
Explanation: The mimes rule specifies allowed file extensions or MIME types.
9. Which Laravel method generates a URL for accessing uploaded files?
o (a) url()
o (b) asset() ✅
o (c) storage()
o (d) route()
Explanation: The asset() method generates a URL for accessing files stored in the public directory.
10. Which folder needs to be linked to the public directory for file access?
o (a) /storage/app
o (b) /storage/public ✅
o (c) /uploads
o (d) /resources/files
Explanation: The storage:link command links the /storage/public folder to /public.
11. What does the max:2048 validation rule specify?
o (a) Maximum file name length
o (b) Maximum file size in KB ✅
o (c) Maximum number of files
o (d) Maximum file dimensions
Explanation: The max rule specifies the maximum allowable file size in kilobytes.
12. Which method is used to check if a file exists?
o (a) exists() ✅
o (b) hasFile()
o (c) check()
o (d) fileExists()
Explanation: The exists() method checks if a file exists at the specified path.
Intermediate Level Questions
13. What command creates a symbolic link between storage and public/storage?
o (a) php artisan link:storage
o (b) php artisan storage:link ✅
o (c) php artisan public:link
o (d) php artisan create:link
Explanation: The storage:link command is used to create symbolic links for file accessibility.
14. Which Laravel configuration file manages file systems?
o (a) app.php
o (b) filesystem.php ✅
o (c) config.php
o (d) storage.php
Explanation: File system configurations are managed in config/filesystem.php.
15. Which method deletes a file from the storage?
o (a) unlink()
o (b) delete() ✅
o (c) remove()
o (d) destroy()
Explanation: The delete() method removes the file from storage.
Advanced Level Questions
16. What is the purpose of the disk() method in Laravel?
o (a) To retrieve file size
o (b) To specify the storage disk ✅
o (c) To retrieve file permissions
o (d) To set file visibility
Explanation: The disk() method specifies the storage disk (e.g., local, public, or s3).
17. How can you restrict a user from uploading more than 5 files?
o (a) Use the count validation rule
o (b) Check the count of files already uploaded ✅
o (c) Use limit:5 in validation
o (d) Set file:max=5
Explanation: Checking the number of previously uploaded files is a common method to limit uploads.
18. What is the correct command to generate a new model for tracking uploaded files?
o (a) php artisan make:controller UploadFile
o (b) php artisan make:model UploadedFile ✅
o (c) php artisan generate:model Upload
o (d) php artisan create:model File
Explanation: make:model is used to create a model in Laravel.
19. How do you retrieve the storage path of a file?
o (a) filePath()
o (b) path() ✅
o (c) fileLocation()
o (d) getPath()
Explanation: The path() method returns the storage path of the file.
20. What is the purpose of visibility in Laravel file storage?
o (a) To encrypt files
o (b) To set file access level (public or private) ✅
o (c) To compress files
o (d) To categorize files
Explanation: The visibility option sets whether a file is publicly accessible or private.
Next Steps
This is a snippet of the MCQs (20 out of 100). If you'd like me to continue and expand to 100 MCQs, let me know!
4o
You said:
yes
ChatGPT said:
ChatGPT
Here’s the continuation of the 100 MCQs on File Uploads in Laravel, with detailed explanations:
Intermediate and Advanced Level Questions (Continued)
21. What Laravel function is used to retrieve a file's extension?
o (a) getFileExtension()
o (b) extension() ✅
o (c) getExtension()
o (d) fileExtension()
Explanation: The extension() method provides the extension of the file being uploaded.
22. What does the mimetypes validation rule validate?
o (a) File extensions
o (b) MIME types ✅
o (c) File names
o (d) File paths
Explanation: The mimetypes rule validates the file's MIME type, e.g., image/jpeg.
23. Which storage disk is used for Amazon S3 uploads in Laravel?
o (a) cloud
o (b) s3 ✅
o (c) remote
o (d) public
Explanation: The s3 disk is configured in Laravel to interact with Amazon S3.
24. What is the purpose of the storePublicly() method?
o (a) Save files without encryption
o (b) Save files with public visibility ✅
o (c) Save files to external disks
o (d) Save files without overwriting
Explanation: storePublicly() ensures the uploaded file is accessible publicly.
25. What is required to handle file uploads in Laravel controllers?
o (a) $request object ✅
o (b) $file helper
o (c) $storage object
o (d) $disk object
Explanation: The $request object manages uploaded files in Laravel controllers.
26. How can you prevent overwriting files with the same name in storage?
o (a) Use unique()
o (b) Use a timestamp in the filename ✅
o (c) Use overwrite: false
o (d) Use distinct()
Explanation: Appending a timestamp to the filename ensures it is unique.
27. Which method allows uploading multiple files simultaneously?
o (a) $request->allFiles() ✅
o (b) $request->multiFile()
o (c) $request->getFiles()
o (d) $request->multiple()
Explanation: The allFiles() method retrieves all uploaded files.
28. What is the correct syntax to validate file uploads in Laravel?
o (a) $request->validate(['file' => 'required']); ✅
o (b) $request->validate('file', 'required');
o (c) $request->validate(['required' => 'file']);
o (d) $request->file(['required']);
Explanation: Validation rules are passed as an associative array to the validate() method.
29. What is the default visibility for files uploaded to the local disk?
o (a) Private ✅
o (b) Public
o (c) Shared
o (d) Cloud
Explanation: Files saved on the local disk are private by default.
30. What command would you use to clear file storage caches in Laravel?
o (a) php artisan cache:clear ✅
o (b) php artisan storage:flush
o (c) php artisan config:clear
o (d) php artisan file:clear
Explanation: The cache:clear command clears all cached data, including file storage-related caches.
31. Which of the following methods checks if a file was successfully uploaded?
o (a) isUploaded()
o (b) isValid() ✅
o (c) isComplete()
o (d) isFinished()
Explanation: The isValid() method checks whether the uploaded file is valid.
32. What happens if you try to upload a file larger than the allowed size in Laravel?
o (a) The file is truncated
o (b) An exception is thrown ✅
o (c) The file is uploaded partially
o (d) The file is automatically resized
Explanation: Laravel throws a validation exception if the uploaded file exceeds the specified size.
33. What is the max rule in Laravel's validation used for in file uploads?
o (a) Maximum filename length
o (b) Maximum file size in kilobytes ✅
o (c) Maximum upload duration
o (d) Maximum folder size
Explanation: The max validation rule checks the file size in KB.
34. What is the role of the hasFile() method?
o (a) Retrieve uploaded files
o (b) Check if a file was uploaded ✅
o (c) Retrieve file details
o (d) Save a file
Explanation: The hasFile() method determines whether a file was uploaded in the request.
35. What is the correct way to upload a file to a subdirectory in the storage folder?
o (a) store('uploads/subdir') ✅
o (b) storeAs('subdir/uploads')
o (c) saveTo('uploads/subdir')
o (d) storeFile('subdir/uploads')
Explanation: You can specify a subdirectory in the store() method.
36. How can you set a file's visibility to private?
o (a) visibility('private')
o (b) storeAs($file, ['visibility' => 'private']) ✅
o (c) setVisibility('private')
o (d) setPrivate()
Explanation: The visibility option is used when storing a file to set its access level.
37. What function allows you to move an uploaded file to a specific directory?
o (a) move() ✅
o (b) storeAs()
o (c) save()
o (d) relocate()
Explanation: The move() method is used to move the uploaded file to a new location.
38. Which Laravel method provides metadata for uploaded files?
o (a) getInfo()
o (b) metadata()
o (c) fileInfo()
o (d) get() ✅
Explanation: The get() method provides details like size, extension, and type.
39. What is the maximum upload size for files set in Laravel by default?
o (a) 2 MB
o (b) 5 MB
o (c) Configured in PHP settings ✅
o (d) Unlimited
Explanation: Laravel uses PHP's upload_max_filesize and post_max_size configurations.
40. How do you access files uploaded to a cloud storage service in Laravel?
o (a) Storage::cloud() ✅
o (b) Storage::s3()
o (c) Storage::disk('s3')
o (d) Storage::external()
Explanation: The cloud() method is used to access files in cloud storage.
Want the remaining questions?
Let me know if you'd like me to continue up to 100 questions! Each subsequent question will explore further nuances, advanced scenarios, and practical use cases related to Laravel file uploads.
4o
You said:
yes
ChatGPT said:
ChatGPT
Advanced Level Questions (41–60)
41. Which of the following Laravel commands publishes the storage link to the public directory?
o (a) php artisan storage:link ✅
o (b) php artisan make:storage
o (c) php artisan link:storage
o (d) php artisan publish:link
Explanation: The storage:link command creates a symbolic link from storage/app/public to public/storage.
42. Which helper function retrieves the contents of an uploaded file?
o (a) file_get_contents()
o (b) Storage::get() ✅
o (c) readfile()
o (d) File::get()
Explanation: The Storage::get() method retrieves the file contents from Laravel's storage system.
43. What is the default path for uploaded files in Laravel when using the local disk?
o (a) storage/app ✅
o (b) storage/public
o (c) public/uploads
o (d) app/uploads
Explanation: By default, the local disk saves files in the storage/app directory.
44. Which of the following is NOT a valid Laravel file validation rule?
o (a) image
o (b) max
o (c) exists ✅
o (d) mimes
Explanation: The exists rule is used to validate database records, not files.
45. What function can generate a random filename for uploaded files?
o (a) md5()
o (b) uniqid() ✅
o (c) random()
o (d) generateName()
Explanation: The uniqid() function generates a unique ID based on the current time.
46. What is the purpose of the storeAs() method in Laravel?
o (a) Store a file with a specific name ✅
o (b) Retrieve a file from storage
o (c) Delete a file from storage
o (d) Move a file to a public folder
Explanation: The storeAs() method allows the developer to set a custom filename when storing a file.
47. What will happen if you use the store() method with no parameters?
o (a) The file will not be saved
o (b) The file will be saved in the default disk's root ✅
o (c) The method will throw an error
o (d) The file will be stored in /uploads
Explanation: Without a specified directory, the file is stored in the root directory of the default disk.
48. How can you delete an uploaded file using Laravel?
o (a) unlink()
o (b) Storage::delete() ✅
o (c) File::remove()
o (d) Storage::unlink()
Explanation: The delete() method of the Storage facade is used to delete files.
49. What method allows you to copy a file to another directory in Laravel?
o (a) copy() ✅
o (b) replicate()
o (c) move()
o (d) duplicate()
Explanation: The copy() method duplicates a file and stores it in the specified location.
50. What validation rule ensures that an uploaded file is an image?
o (a) mimes:image
o (b) image ✅
o (c) file:image
o (d) type:image
Explanation: The image validation rule ensures the uploaded file is a valid image format.
51. Which disk configuration would you use to store files on Google Drive?
o (a) cloud
o (b) gdrive
o (c) custom ✅
o (d) google
Explanation: Laravel allows creating custom disk configurations for external services like Google Drive.
52. How can you set a file's permissions in Laravel?
o (a) Storage::permissions()
o (b) chmod()
o (c) Use the visibility option ✅
o (d) filePermissions()
Explanation: The visibility option sets access permissions like private or public.
53. Which of these methods retrieves the URL of a file stored on the public disk?
o (a) Storage::url() ✅
o (b) Storage::link()
o (c) Storage::getURL()
o (d) Storage::retrieve()
Explanation: The url() method generates a public URL for files stored in the public disk.
54. What does the store() method return upon successfully uploading a file?
o (a) true
o (b) The file's new path ✅
o (c) The file's URL
o (d) null
Explanation: The store() method returns the relative path of the uploaded file.
55. Which Laravel configuration file contains the default disk settings?
o (a) app.php
o (b) filesystems.php ✅
o (c) storage.php
o (d) upload.php
Explanation: Disk configurations are defined in the config/filesystems.php file.
56. What does the files() method in the Storage facade do?
o (a) Returns the files in a directory ✅
o (b) Uploads files to a directory
o (c) Validates uploaded files
o (d) Deletes all files in a directory
Explanation: The files() method retrieves all files in a specified directory.
57. How can you specify the disk to use for uploading files?
o (a) store('path', 'disk') ✅
o (b) storeAs('disk', 'path')
o (c) Storage::setDisk()
o (d) Storage::useDisk()
Explanation: The store() method allows specifying the disk as the second parameter.
58. What validation rule ensures that an uploaded file is within a specific size range?
o (a) max_size
o (b) between ✅
o (c) size_range
o (d) file_size
Explanation: The between validation rule specifies a range of acceptable file sizes.
59. What does the disk() method of the Storage facade do?
o (a) Deletes a disk
o (b) Selects a storage disk ✅
o (c) Configures a new disk
o (d) Checks disk space
Explanation: The disk() method specifies which storage disk to use for file operations.
60. Which HTTP method is typically used for file uploads?
o (a) GET
o (b) POST ✅
o (c) PUT
o (d) DELETE
Explanation: File uploads are performed using the POST method in HTML forms.
Would you like me to continue with the remaining 40 questions? Let me know!
4o
You said:
yes
ChatGPT said:
ChatGPT
Advanced Level Questions (61–100)
61. Which method creates a directory in Laravel's storage?
o (a) Storage::create()
o (b) Storage::makeDirectory() ✅
o (c) Storage::addFolder()
o (d) Storage::newDirectory()
Explanation: The makeDirectory() method creates a new directory in the specified path.
62. What is the default disk in Laravel if no disk is specified?
o (a) local ✅
o (b) public
o (c) cloud
o (d) private
Explanation: The local disk is used as the default if no disk is specified.
63. Which Laravel method generates a temporary signed URL for a file?
o (a) Storage::temporaryUrl() ✅
o (b) Storage::signedUrl()
o (c) Storage::urlSigned()
o (d) Storage::secureUrl()
Explanation: The temporaryUrl() method creates a temporary signed URL that expires after a given time.
64. What is the primary use of the enctype="multipart/form-data" attribute in an HTML form?
o (a) To allow file uploads ✅
o (b) To handle large text inputs
o (c) To improve form security
o (d) To enable AJAX submissions
Explanation: This attribute is required for file uploads, ensuring the form sends data as binary.
65. Which Laravel method checks if a file exists in storage?
o (a) Storage::exists() ✅
o (b) Storage::has()
o (c) Storage::fileExists()
o (d) Storage::check()
Explanation: The exists() method verifies if a file exists in the given storage path.
66. How do you retrieve all subdirectories within a directory in Laravel?
o (a) Storage::folders() ✅
o (b) Storage::directories()
o (c) Storage::getSubfolders()
o (d) Storage::listDirectories()
Explanation: The directories() method lists all subdirectories in a specified path.
67. What does the mimes validation rule check?
o (a) File size
o (b) File extension ✅
o (c) File visibility
o (d) File encoding
Explanation: The mimes rule checks if the uploaded file matches specific MIME types.
68. How can you move a file to a new directory in Laravel?
o (a) Storage::move() ✅
o (b) Storage::rename()
o (c) Storage::transfer()
o (d) Storage::copy()
Explanation: The move() method moves a file from one location to another.
69. Which validation rule ensures that a file is not larger than 2MB?
o (a) max:2000 ✅
o (b) size:2000
o (c) max:2
o (d) filesize:2
Explanation: The max rule defines the maximum file size in kilobytes.
70. What does the getClientOriginalName() method return?
o (a) The file's original name ✅
o (b) The file's storage path
o (c) The file's MIME type
o (d) The file's size
Explanation: This method retrieves the original name of the uploaded file.
71. How can you retrieve the size of an uploaded file in Laravel?
o (a) $file->getSize() ✅
o (b) $file->size()
o (c) $file->length()
o (d) $file->getLength()
Explanation: The getSize() method retrieves the size of the uploaded file in bytes.
72. Which Laravel class handles file uploads?
o (a) File
o (b) UploadedFile ✅
o (c) Request
o (d) Storage
Explanation: The UploadedFile class is used to handle uploaded files in Laravel.
73. How do you define a disk with Amazon S3 in Laravel?
o (a) In filesystems.php ✅
o (b) In .env
o (c) In config/storage.php
o (d) In app.php
Explanation: The filesystems.php file is used to configure storage disks like Amazon S3.
74. What happens if an uploaded file exceeds the maximum file size in Laravel?
o (a) It is automatically resized
o (b) An error is thrown ✅
o (c) The file is truncated
o (d) The file is accepted but not processed
Explanation: Laravel's validation rules ensure that oversized files trigger an error.
75. What does the public_path() helper return?
o (a) The path to the public directory ✅
o (b) The path to the storage directory
o (c) The URL of the storage link
o (d) The URL of a public file
Explanation: The public_path() function provides the absolute path to the public directory.
76. Which of the following is NOT a valid MIME type?
o (a) application/json
o (b) text/html
o (c) image/jpeg
o (d) file/unknown ✅
Explanation: file/unknown is not a valid MIME type.
77. Which storage disk is ideal for temporary files?
o (a) local ✅
o (b) public
o (c) s3
o (d) cloud
Explanation: Temporary files are usually stored locally during processing.
78. What does the getClientMimeType() method return?
o (a) The file's MIME type ✅
o (b) The file's size
o (c) The file's storage path
o (d) The file's original name
Explanation: This method retrieves the MIME type of the uploaded file.
79. Which function generates a unique name for storing files in Laravel?
o (a) str_random()
o (b) uniqid() ✅
o (c) generateFilename()
o (d) Storage::uniqueName()
Explanation: The uniqid() function generates a unique filename.
80. Which of the following is true about Storage::disk()?
o (a) It sets up a new disk
o (b) It retrieves a file from a specified disk ✅
o (c) It removes all files from a disk
o (d) It deletes the disk configuration
Explanation: The disk() method allows operations on a specific disk.
Let me know if you’d like the remaining 20 questions!
4o
You said:
yes
ChatGPT said:
ChatGPT
Advanced Level Questions (81–100)
81. How do you retrieve all files within a directory in Laravel?
o (a) Storage::allFiles() ✅
o (b) Storage::files()
o (c) Storage::listFiles()
o (d) Storage::getFiles()
Explanation: The allFiles() method lists all files in a directory, including those in subdirectories.
82. Which of the following methods deletes multiple files in Laravel storage?
o (a) Storage::deleteMany()
o (b) Storage::delete() ✅
o (c) Storage::remove()
o (d) Storage::deleteAll()
Explanation: The delete() method can accept an array of file paths for deleting multiple files.
83. Which helper function provides the absolute path to the storage directory?
o (a) storage_path() ✅
o (b) base_path()
o (c) app_path()
o (d) public_path()
Explanation: The storage_path() function returns the absolute path to Laravel's storage directory.
84. What does the file validation rule check?
o (a) The file extension
o (b) The file type
o (c) If the input is a valid file ✅
o (d) If the file is public
Explanation: The file validation rule ensures that the input is a valid file.
85. Which function is used to create symbolic links for storage files?
o (a) php artisan storage:link ✅
o (b) php artisan make:link
o (c) php artisan link:storage
o (d) php artisan file:link
Explanation: The storage:link command creates a symbolic link between storage and public directories.
86. What is the purpose of visibility in Laravel storage?
o (a) To make files readable or writable
o (b) To determine if a file is private or public ✅
o (c) To lock files
o (d) To enable file compression
Explanation: Visibility controls whether a file is publicly accessible or private.
87. Which method is used to copy a file to a new location in Laravel?
o (a) Storage::copy() ✅
o (b) Storage::move()
o (c) Storage::replicate()
o (d) Storage::duplicate()
Explanation: The copy() method duplicates a file to a new location without deleting the original.
88. How do you retrieve the URL of a file in public storage?
o (a) Storage::url() ✅
o (b) Storage::getUrl()
o (c) Storage::fileUrl()
o (d) Storage::linkUrl()
Explanation: The url() method returns the public URL of a file stored in a public disk.
89. What does $request->hasFile() check?
o (a) If a file exists in storage
o (b) If the request contains a file ✅
o (c) If the file has a valid extension
o (d) If the file is uploaded to the correct directory
Explanation: The hasFile() method checks if a file is included in the request.
90. Which method saves an uploaded file in Laravel?
o (a) $file->store() ✅
o (b) $file->save()
o (c) $file->upload()
o (d) $file->storeFile()
Explanation: The store() method saves the uploaded file to the specified disk.
91. How do you generate a fake file for testing in Laravel?
o (a) UploadedFile::fake()->create() ✅
o (b) File::fake()->make()
o (c) Storage::fakeFile()
o (d) Faker::file()
Explanation: The UploadedFile::fake() method is used to create fake files for testing.
92. Which Laravel method changes a file's visibility?
o (a) Storage::setVisibility() ✅
o (b) Storage::visibility()
o (c) Storage::changeVisibility()
o (d) Storage::modifyVisibility()
Explanation: The setVisibility() method updates a file's visibility to public or private.
93. Which configuration file defines the default file storage disk?
o (a) config/app.php
o (b) config/storage.php
o (c) config/filesystems.php ✅
o (d) config/settings.php
Explanation: The filesystems.php file is where the default storage disk is configured.
94. What does the path() method return in Laravel storage?
o (a) The public URL of a file
o (b) The absolute path of a file ✅
o (c) The directory name of a file
o (d) The MIME type of a file
Explanation: The path() method retrieves the absolute path to a file in storage.
95. What happens if the disk name provided in Storage::disk() does not exist?
o (a) An error is thrown ✅
o (b) A new disk is created
o (c) The local disk is used
o (d) Nothing happens
Explanation: If the disk name is invalid, Laravel throws an error indicating the disk is not configured.
96. Which function in Laravel storage checks if a file is readable?
o (a) Storage::isReadable() ✅
o (b) Storage::canRead()
o (c) Storage::readable()
o (d) Storage::fileReadable()
Explanation: The isReadable() method verifies if a file can be read.
97. Which command fakes a storage disk in Laravel?
o (a) Storage::fake() ✅
o (b) Storage::mock()
o (c) Storage::testDisk()
o (d) Storage::simulate()
Explanation: The fake() method creates a fake disk for testing.
98. What does $request->file('file')->isValid() check?
o (a) If the file exists in storage
o (b) If the file was uploaded without errors ✅
o (c) If the file is readable
o (d) If the file matches validation rules
Explanation: The isValid() method checks if the uploaded file encountered any issues.
99. What is the result of Storage::deleteDirectory('folder')?
o (a) Deletes the specified directory and all its contents ✅
o (b) Deletes the directory only if it's empty
o (c) Deletes all directories except the specified one
o (d) Deletes the directory and retains the files
Explanation: This method deletes the directory and all its contents.
100. What does the storage:link command accomplish in Laravel?
- (a) Creates a symbolic link between storage and public ✅
- (b) Deletes unnecessary storage files
- (c) Clears the storage cache
- (d) Renames the storage directory
bash
Copy code
**Explanation**: The `storage:link` command links the `storage/app/public` directory to the `public/storage` directory for file access.
Comments
Post a Comment