Join the social network of Tech Nerds, increase skill rank, get work, manage projects...
 
  • PHP: Read CSV FILE, Write CSV FILE and READWRITE CSV FILE

    • 0
    • 0
    • 0
    • 0
    • 0
    • 0
    • 0
    • 0
    • 763
    Comment on it

    1> Read CSV file

    When you read the CSV file in php you use the code given below.Here we use the fopen(), fgetcsv() functions to read the csv file.

    The below code reads data from a CSV file read.csv

    <?php
    
    if (($handle = fopen("read.csv", "r")) !== FALSE) {
        while (($data = fgetcsv($handle, 1000, ",")) !== FALSE) {
            $num = count($data);        
            for ($c=0; $c < $num; $c++) {            
                echo $data[$c]; 
                echo "<br />";        
            }
        }
        fclose($handle);
    }
    
    ?>
    

    Here, In above code, first we use the read.csv file which is a given file and put this file into the folder where you fetch the file. Then, the above code use the fopen() function to open the given file in "r" mode (means read mode) and then we use fgetcsv() function which is used to get the line from file pointer and parse csv fields. This works on PHP 4 and PHP 5. After that parsing reads for fields in csv and returns an field array.


    2> Write CSV file

    When you writes the data to a csv file like file.csv and then given into the code in array form, the element of array is in comma separated form.

    The below code writes the data into the csv format.

    <?php
    
    $list = array (
        'aaa,bbb,ccc,dddd',
        '123,456,789'
    );
    $fp = fopen('file.csv', 'w');
     foreach ($list as $line) {    
        fputcsv($fp, split(',', $line));
    }
     fclose($fp);
     echo "CSV File Written Successfully!";
    
    ?>
    

    Here, In above code, first we use the list of array which contain the write data into the csv file and after then use the fopen() function and then fputscsv() function (write to file pointer) and split() function to split the given data by comma.

    This works on PHP 5 or greater.


    3> Read from one CSV file and write into the other

    Lastly, if you use the mixture of csv file contain the read as well as writes the csv file.

    The below code contain read data from a csv file read.csv and writes the data to another csv file file then its called of readwrite csv file.

    <?php
    
    // open the csv file in write mode
    $fp = fopen('readwrite.csv', 'w');
     // read csv file
    if (($handle = fopen("read.csv", "r")) !== FALSE) {
        while (($data = fgetcsv($handle, 1000, ",")) !== FALSE) {                      
            // write csv file
            fputcsv($fp, $data);          
        }
        fclose($handle);
        fclose($fp);
        echo "CSV File Written Successfully!";
    }
    
    ?>
    

    The above code will first open the csv file in write mode and then read csv file using fgetcsv() function and last write the csv file using fputcsv() function.

 0 Comment(s)

Sign In
                           OR                           
                           OR                           
Register

Sign up using

                           OR                           
Forgot Password
Fill out the form below and instructions to reset your password will be emailed to you:
Reset Password
Fill out the form below and reset your password: