Book Image

Building a Web Application with PHP and MariaDB: A Reference Guide

By : Sai S Sriparasa
Book Image

Building a Web Application with PHP and MariaDB: A Reference Guide

By: Sai S Sriparasa

Overview of this book

Table of Contents (17 chapters)
Building a Web Application with PHP and MariaDB: A Reference Guide
Credits
About the Author
About the Reviewers
www.PacktPub.com
Preface
Index

Logging


The last file operation that we will work with is logging. We will begin by looking at how information can be logged to web server logs and build a simple logging library that can be used across the application. Let's begin by using the error_log function provided by PHP to log information into the web server logfiles. For this example, let's use the export action that we created in the last section to add a message that will be logged.

The following codec needs to be added to the controllers/students.php file:

public function export(){
  $data = $this->model->getStudents();
  $handle = fopen(ROOT_DIR.'/assets/files/students.csv', 'w+');
  foreach ($data as $student) {
    fputcsv($handle, array($student['student_id'], $student['first_name'], $student['last_name'], $student['address'],$student['city'],$student['state']));
  }
  fclose($handle);

  header('Content-Disposition: attachment; filename="students.csv"');
  header('Content-Type:application/csv');

  readfile(ROOT_DIR...