Which of the following function is used to find files in php
Show
Menu
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-
Click here to read 1000+ Related Questions on Functions(PHP)Join The DiscussionRelated 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:
In this tutorial, you will learn-
PHP File Formats SupportPHP file functions support a wide range of file formats that include:
PHP file() FunctionPHP 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() FunctionThis function is used to determine whether a file exists or not.
The file_exist function has the following syntax. HERE,
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 http://localhost/phptuts/file_function.php in your browser You will get the following results.
PHP fopen() FunctionThe fopen function is used to open files. It has the following syntax HERE,
PHP fwrite() FunctionThe fwrite function is used to write files. It has the following syntax HERE,
PHP fclose() FunctionThe fclose() function is used to close a file in php which is already open It has the following syntax. HERE,
Let’s now look at an example that creates my_settings.txt. We will use the following functions.
The code below “create_my_settings_file.php” implements the above example.
Testing the codeOpen the URL http://localhost/phptuts/create_my_settings.php in your browser. You will get the following page . Note: if your disk is full or you do not have permission to write files, you will get an error message. Switch back to the URL http://localhost/phptuts/file_function.php . What results do you get? PHP fgets() FunctionThe fgets function is used to read php files line by line. It has the following basic syntax. fgets($handle); HERE,
Let’s now look at an example that reads my_settings.txt file using the fopen and fgets functions. The code below read_my_settings.php implements the above example. HERE,
PHP copy() FunctionThe PHP copy function is used to copy files. It has the following basic syntax. copy($file,$copied_file); HERE,
The code below illustrates the implementation Deleting a fileThe unlink function is used to delete the file. The code below illustrates the implementation. PHP file_get_contents() FunctionThe file_get_contents function is used to read the entire file contents. The code below illustrates the implementation. The difference between file_get_contents and fgets is that file_get_contents returns the file data as a string while fgets reads the file line by line. "; // Enables display of line feeds echo file_get_contents("my_settings.txt"); echo ""; // Terminates pre tag ?> Summary
Which PHP function is used to find files?The file_get_contents function is used to read the entire file contents.
What are files in PHP?So what exactly is a PHP file? Generally speaking, a PHP file is a plain-text file which contains code written in the PHP programming language. Since PHP is a server-side (back-end) scripting language, the code written in the PHP file is executed on the server.
|