Menu
H
HomeA
AptitudeE
EnglishR
ReasoningD
DIG
GKC
Current AffairsI
Interview- Computer
C
Computer FundamentalsN
NetworkingS
SQLD
Database
- Programming
C
C ProgramJ
Java ProgramH
HTMLC
J
JavascriptP
PHP Program
- Engineering
C
Computer ScienceE
Electrical EngineeringM
Mechanical EngineeringC
Civil EngineeringC
Chemical Engineering
- More
B
Banking AwarenessC
CommerceM
Management
A
Ask Question
- Home
- Aptitude
- English
- Reasoning
- DI
- GK
- Current Affairs
- Interview
- Computer
- Computer Fundamentals
- Networking
- SQL
- Database
- Programming
- C Program
- Java Program
- HTML
- CSS
- Javascript
- PHP Program
- Engineering
- Computer Science
- Electrical Engineering
- Mechanical Engineering
- Civil Engineering
- Chemical Engineering
- More
- Banking Awareness
- Commerce
- Management
- Ask Question
Which one of the following PHP functions can be used to find files?
A. glob[]
B. file[]
C. fold[]
D. get_file[]
Answer: Option A
Solution[By Examveda Team]
Here is an example-
// get all php files AND txt files
$files = glob['*.{php,txt}', GLOB_BRACE];
print_r[$files];
/* output looks like:
Array
[
[0] => phptest.php
[1] => pi.php
[2] => post_output.php
.
.
.
]
Click here to read 1000+ Related Questions on Functions[PHP]
Join The Discussion
Related Questions on Functions
What is a File?
A file is simply a resource for storing information on a computer.
Files are usually used to store information such as:
- Configuration settings of a program
- Simple data such as contact names against the phone numbers.
- Images, Pictures, Photos, etc.
In this tutorial, you will learn-
- PHP File Formats Support
- PHP file[] Function
- PHP file_exists[] Function
- PHP fopen[] Function
- PHP fwrite[] Function
- PHP fclose[] Function
- PHP fgets[] Function
- PHP copy[] Function
- Deleting a file
- PHP file_get_contents[] Function
PHP File Formats Support
PHP file functions support a wide range of file formats that include:
- File.txt
- File.log
- File.custom_extension i.e. file.xyz
- File.csv
- File.gif, file.jpg etc
- Files provide a permanent cost effective data storage solution for simple data compared to databases that require other software and skills to manage DBMS systems.
- You want to store simple data such as server logs for later retrieval and analysis
- You want to store program settings i.e. program.ini
PHP file[] Function
PHP provides a convenient way of working with files via its rich collection of built in functions.
Operating systems such as Windows and MAC OS are not case sensitive while Linux or Unix operating systems are case sensitive.
Adopting a naming conversion such as lower case letters only for file naming is a good practice that ensures maximum cross platform compatibility.
Let’s now look at some of the most commonly used PHP file functions.
PHP file_exists[] Function
This function is used to determine whether a file exists or not.
- It comes in handy when we want to know if a file exists or not before processing it.
- You can also use this function when creating a new file and you want to ensure that the file does not already exist on the server.
The file_exist function has the following syntax.
HERE,
- “file_exists[]” is the PHP function that returns true if the file exists and false if it does not exist.
- “$file_name” is the path and name of the file to be checked
The code below uses file_exists function to determine if the file my_settings.txt exists.
Save the above code in a file named file_function.php Assuming you saved the file in phptuts folder in htdocs, open the URL //localhost/phptuts/file_function.php in your browser You will get the following results.
PHP fopen[] Function
The fopen function is used to open files. It has the following syntax
HERE,
- “fopen” is the PHP open file function
- “$file_name” is the name of the file to be opened
- “$mode” is the mode in which the file should be opened, the table below shows the modes
r |
|
r+ |
|
w |
|
w+ |
|
a |
|
a+ |
|
- “$use_include_path” is optional, default is false, if set to true, the function searches in the include path too.
- “$context” is optional, can be used to specify the context support.
PHP fwrite[] Function
The fwrite function is used to write files.
It has the following syntax
HERE,
- “fwrite” is the PHP function for writing to files
- “$handle” is the file pointer resource
- “$string” is the data to be written in the file.
- “$length” is optional, can be used to specify the maximum file length.
PHP fclose[] Function
The fclose[] function is used to close a file in php which is already open
It has the following syntax.
HERE,
- “fclose” is the PHP function for closing an open file
- “$handle” is the file pointer resource.
Let’s now look at an example that creates my_settings.txt.
We will use the following functions.
- Fopen
- Fwrite
- fclose
The code below “create_my_settings_file.php” implements the above example.
Open a file | |
Closing a file | |
Create File |
|