PHP File Handling

PHP File Handling

File handling is an important part of any web application. You often need to open and process a file for different tasks.

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.

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

Note that while creating a function its name should start with keyword function and all the PHP code should be put inside { and } braces as shown in the following example below:

PHP files Functions

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.
  • “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 file_exist function has the following syntax.

  •       
                            <?php
    file_exists($filename); 
    ?>
                        

    The code below uses file_exists function to determine if the file my_settings.txt exists.

  •       
                            <?php
    if (file_exists('my_settings.txt'))
        {    
            echo 'file found!';
        } 
    else
        {     
            echo 'my_settings.txt does not exist';
        } 
    ?<
                        

    PHP Manipulating Files

    PHP has several functions for creating, reading, uploading, and editing files. When you are manipulating files you must be very careful. You can do a lot of damage if you do something wrong. Common errors are: editing the wrong file, filling a hard-drive with garbage data, and deleting the content of a file by accident.

    PHP readfile( ) Function

    The readfile() function reads a file and writes it to the output buffer.

    The PHP code to read the file and write it to the output buffer is as follows (the readfile() function returns the number of bytes read on success). The readfile() function is useful if all you want to do is open up a file and read its contents.

  •       
                            <?php
    echo readfile("webdictionary.txt");
    ?>
                        

    Dess App

    DessApp is an Integrated E-learning Education, Interactive and User-friendly features, smarter options and redefining your school costs effectively and efficiently.

    View
    1 1