Get the latest news, exclusives, sport, celebrities, showbiz, politics, business and lifestyle from The VeryTime,Stay informed and read the latest news today from The VeryTime, the definitive source.

How to Use PHP to Read a CSV File

14
    • 1). Right-click your PHP file and select "Open With." Double-click your PHP editor, or double-click "Notepad" if you do not have a third-party editor installed on your computer. The PHP code loads in the editor.

    • 2). Type the following code within the "<?php" opening tag and the "?>" closing tag. These tags denote a section for PHP code where you type your instructions.

      $file = fopen("customers.csv", "r");

      Replace "customers" with the name of your own CSV file. The "r" parameter indicates that you want to "read" in the file. The file is set to the "$file" variable.

    • 3). Type the following code to parse the data and print it to the Web page:

      while (!feof($file) ) {

      $line = fgetcsv($file, 1024);

      print $line[0] . $line[1]. $line[2] . "<BR>";

      }

      This code reads each record and prints the fields to the Web page.

    • 4). Type the code to close the file:

      fclose($file);

      You must close the file before closing the PHP code or the Web server holds a lock on it, which prevents you from later editing the file.

    • 5). Press "Ctrl"+"S" to save the changes. Open the file in your PHP debugger or in the browser to test the changes.

Source...
Subscribe to our newsletter
Sign up here to get the latest news, updates and special offers delivered directly to your inbox.
You can unsubscribe at any time

Leave A Reply

Your email address will not be published.