File Handling In PHP and Their Uses/Functions

file-handling-in-php-and-their-uses-functions

File handling in PHP is a technique used to manipulate files on a server. It allows developers to create, read, write, append, and delete files on a server. PHP provides a set of functions to perform file-handling operations.

Here are some common file-handling functions in PHP:

1. fopen(): This function is used to open a file or a URL.

2. fclose(): This function is used to close a file pointer. It takes the file pointer resource as a parameter and returns TRUE on success or FALSE on failure.

3. fread(): This function is used to read from an open file. It takes the file pointer resource and the number of bytes to be read as parameters and returns the read string or FALSE on failure.

4. fwrite(): This function is used to write to an open file. It takes the file pointer resource, the string to be written, and the length of the string as parameters and returns the number of bytes written or FALSE on failure.

5. fgets(): This function is used to read a line from an open file. It takes the file pointer resource as a parameter and returns a string containing the next line from the file or FALSE on failure.

6. fputs(): This function is used to write a string to an open file. It takes the file pointer resource, the string to be written, and the length of the string as parameters and returns the number of bytes written or FALSE on failure.

7. fseek(): This function is used to seek a specific position in an open file. It takes the file pointer resource, the number of bytes to seek, and the position as parameters and returns 0 on success or -1 on failure.

8. ftell(): This function is used to get the current position of an open file. It takes the file pointer resource as a parameter and returns the current position or FALSE on failure.

9. feof(): This function is used to check whether the end of a file has been reached. It takes the file pointer resource as a parameter and returns TRUE if the end of the file has been reached or FALSE otherwise.

10. fgetc(): This function is used to read a single character from an open file. It takes the file pointer resource as a parameter and returns a string containing the character or FALSE on failure.

11. fputcsv(): This function is used to write a field array as a CSV line to an open file. It takes the file pointer resource, the field array, the delimiter, and the enclosure as parameters and returns the length of the written string or FALSE on failure.

12. fgetcsv(): This function is used to read a CSV line from an open file and parse it into fields. It takes the file pointer resource, the length of the line, and the delimiter as parameters and returns an array containing the fields or FALSE on failure.

13. fpassthru(): This function is used to read and write data from/to a file. It takes the file pointer resource as a parameter and returns the number of bytes read or FALSE on failure.

14. ftruncate(): This function is used to truncate an open file to a specified length. It takes the file pointer resource and the size to truncate to as parameters and returns TRUE on success or FALSE on failure.

15. fstat(): This function is used to get information about an open file. It takes the file pointer resource as a parameter and returns an array containing the file information or FALSE on failure.

16. flock(): This function is used to perform a lock on an open file. It takes the file pointer resource, the operation, and an optional variable to store the would-block value as parameters and returns TRUE on success or FALSE on failure.

17. fflush(): This function is used to flush the output buffer of an open file. It takes the file pointer resource as a parameter and returns TRUE on success or FALSE on failure.

18. fclose(): This function is used to close an open file. It takes the file pointer resource as a parameter and returns TRUE on success or FALSE on failure.

19. pclose(): This function is used to close an open pipe. It takes the file pointer resource as a parameter and returns the termination status of the command that was piped to the file or FALSE on failure.

20. popen(): This function is used to open a pipe to or from a command. It takes the command to be executed and the mode as parameters and returns a file pointer resource or FALSE on failure.

These are only a few of the commands, in our next article, we hit out more than 60 syntaxes……

Related posts:

About Author


Discover more from SURFCLOUD TECHNOLOGY

Subscribe to get the latest posts sent to your email.

Leave a Reply

Your email address will not be published. Required fields are marked *

Discover more from SURFCLOUD TECHNOLOGY

Subscribe now to keep reading and get access to the full archive.

Continue reading