This tutorial deals with the opening , closing, reading and writing of file. There are dedicated function for performing these operations in PHP. In order to open a file, fopen() can be used. Calling this function typically requires two parameters namely filename and the mode in which the programmer wishes to open the file. The different modes available in PHP are as follows-
|r||This mode opens the file for only reading. The file pointer points to the first character of the file.|
|r++||This mode opens the file for reading as well as writing. The file pointer points to the first character of the file.|
|w||this mode opens the file for only writing. The file pointer points to the beginning of the file. Moreover, the file length is truncated to zero. In case the file doesn’t exist, then the system tries to create a file.|
|w++||This mode opens the file for only writing and reading. The file pointer points to the beginning of the file. Moreover, the file length is truncated to zero. In case the file doesn’t exist, then the system tries to create a file.|
|A||This mode opens the file for only writing. The file pointer points to the end of the file. In case the file doesn’t exist, then the system tries to create a file.|
|a++||This mode opens the file for only writing and reading. The file pointer points to the end of the file. In case the file doesn’t exist, then the system tries to create a file.|
If the requested file is successfully opened, the file pointer for the file is returned. On the other hand, if the file opening operation fails, FALSE is the returned to the calling function, As a rule, be sure to close any file that you open. The function used for this purpose is fclose(), which has been discussed below.
Once a file is open, one of the operations that you will perform is the file reading. This is typically done using fread(), which requires two arguments namely, file pointer and the file length in bytes. If the size of the file is unknown, you can use filesize() function to determine the same and only requires the filename as argument. In order to read a file file, the steps that you will need to follow include-
- Opening the file using fopen()
- Determining the size of file using filesize()
- Reading the contents of the file using fread()
- Closing the file using fclose()
Same code for demonstrating the different steps involved in the process has been provided below for your reference.
<title>PHP File System</title>
echo "File cannot open";
echo "Size of File: $file_size bytes";
You can run this code to see the output it generates. If the file is not present, the program shall give an error message else it shall provide you desired output.
Click Her to get full Access : https://phpgurukul.com/php-file-system/
The other function that you can expect to perform with file is writing. It is possible to rewrite a file or append the same on need basis. The function used for this purpose is fwrite(). which requires two arguments namely file pointer to file and the string that needs to be written to the file.
Besides this, this function also expects a third argument, which is the length of data that is to be written. Typically, if you provide this argument, writing to the file will stop as soon as the number of bytes specified in data-size is reached.
Blog : https://phpgurukul.com/php-tutorials/
Welcome to PHPGurukul. We are a web development team striving our best to provide you with an unusual experience with PHP. Some technologies never fade, and PHP is one of them. From the time it has been introduced, the demand for PHP Projects and PHP developers is growing since 1994. We are here to make your PHP journey more exciting and useful.
Website : https://phpgurukul.com
Showing 1 reaction
Sign in with