PHP

  • File Handling
  • Error Handling
  • Headers
  • Uploading a file



- Nishant Shrivastava
nishant@weboniselab.com
github :  <nishant-shrivastava>


File Handling

  • r :  Read only. Starts at the beginning of the file
  • r+ : Read/Write. Starts at the beginning of the file
  • w : Write only. Opens and clears the contents of file; or creates a new file if it doesn't exist
  • w+ : Read/Write. Opens and clears the contents of file; or creates a new file if it doesn't exist
  • a : Append. Opens and writes to the end of the file or creates a new file if it doesn't exist
  • a+ : Read/Append. Preserves file content by writing to the end of the file
  • x : Write only. Creates a new file. Returns FALSE and an error if file already exists
  • x+ : Read/Write. Creates a new file. Returns FALSE and an error if file already exists

Opening a File

  # The fopen() function is used to open files in PHP.<?php$file=fopen("welcome.txt","r") or exit("Unable to open file!");?>

Closing A FILE

  # The fclose() function is used to close an open file:<?php$file = fopen("test.txt","r");//some code to be executedfclose($file);?>

Check End-of-file

 # The feof() function checks if the "end-of-file" (EOF) has been reached.<? ?>


Reading a file Line by line

The fgets() function is used to read a single line from a file.Note: After a call to this function the file pointer has moved to the next line.   
<?php $file = fopen("welcome.txt", "r") or exit("Unable to open file!"); //Output a line of the file until the end is reached while(!feof($file)) {   echo fgets($file). "<br>";   } fclose($file); ?>


Reading a file Character By Character


 # The fgetc() function is used to read a single character from a file.Note: After a call to this function the file pointer moves to the next character.
<?php $file=fopen("welcome.txt","r") or exit("Unable to open file!"); while (!feof($file)) {   echo fgetc($file);
} fclose($file); ?>

Error-Handling


  • die();
 <?php if(!file_exists("welcome.txt")) {  die("File not found");  } else { $file=fopen("welcome.txt","r");  }?>

error-handling

Creating a custom error-handler

<?php error_function(error_level,error_message, error_file,error_line,error_context); ?>

# error_level^ : Specifies the error report level for the user-defined error.# error_message^ : Specifies the error message for the user-defined error# error_file : Specifies the file-name in which the error occurred# error_line : Specifies the line number in which the error occurred# error_context : Specifies an array containing every variable, and their values, in use when the error occurred

Exception Handling


  • What is exception?

 Exception handling is used to change the normal flow of the code execution if a specified error (exceptional) condition occurs. This condition is called an exception.This is what normally happens when an exception is triggered: - The current code state is saved - The code execution will switch to a predefined (custom) exception handler function - Depending on the situation, the handler may then resume the execution from the saved code state, terminate the script execution or continue the script from a different location in the code

Examples


 # Basic<?php//create function with an exceptionfunction checkNum($number) {    if($number>1) {    throw new Exception("Value must be 1 or below");    }  return true; }//trigger exceptioncheckNum(2);?>


 # Try , Throw and Catch<?php//create function with an exceptionfunction checkNum($number) {     if($number>1) {    throw new Exception("Value must be 1 or below");    }     return true;  }//trigger exception in a "try" blocktry {  checkNum(2);  //If the exception is thrown, this text will not be shown  echo 'If you see this, the number is 1 or below';  }//catch exceptioncatch(Exception $e) {  echo 'Message: ' .$e->getMessage();  }?>

Title


Headers


# header() is used to send a raw HTTP header.
<?phpheader('Location: http://www.example.com/');exit;?>




Thank you

:)
Made with Slides.com