Open Source Server Side Scripting ECA 236 Open Source Server Side Scripting Files & Directories.

Slides:



Advertisements
Similar presentations
PHP Form and File Handling
Advertisements

UFCE8V-20-3 Information Systems Development 3 (SHAPE HK) Lecture 3 PHP (2) : Functions, User Defined Functions & Environment Variables.
Files in C Rohit Khokher. Files in C Real life situations involve large volume of data and in such cases, the console oriented I/O operations pose two.
1 PHP Storing and Retrieving Data. string fgets(resource handle [, int length]) Reads and returns one line from a file; moves file pointer to next line.
More on PHP Coding Lab no. 6 Advance Database Management System.
More on Functions PHP mod 4 B. Simple Mail Transfer Protocol (SMTP) mail($to, $subject, $msg, $mailheaders); m08/8-1simple_form.html m08/8-1send_simpleform.php.
FILES Files types and operations. Files Files are used to store data Data can be Text (ASCII only: 0  127) Binary (Full range: 0  256) Each file resides.
IS 1181 IS 118 Introduction to Development Tools Week 2.
CSE1301 Computer Programming: Lecture 19 File I/O
COMPREHENSIVE Excel Tutorial 8 Developing an Excel Application.
PHP Programming. Topics Background and History of PHP Installation Comments in PHP Variables Conditions Loops Functions File Handling Database Handling.
PHP Tutorials 02 Olarik Surinta Management Information System Faculty of Informatics.
Advance web Programming Chapter 3: MySQL Date: 28 April 2014 Advance web Programming Chapter 3: MySQL Date: 28 April 2014 Dr. Mogeeb A. A. Mosleh .
Open Source Server Side Scripting ECA 236 Open Source Server Side Scripting PHP Form Handling.
ITM 352 © Port,KazmanFile I/O - 1 File I/O in PHP Persistent Data.
Open Source Server Side Scripting ECA 236 Open Source Server Side Scripting Cookies & Sessions.
Open Source Server Side Scripting ECA 236 Open Source Server Side Scripting Includes and Dates.
A First Book of ANSI C Fourth Edition Chapter 10 Data Files.
18. PHP File Operations Opening a File
PHP1-1 PHP Lecture 2 Xingquan (Hill) Zhu
Shell Script Programming. 2 Using UNIX Shell Scripts Unlike high-level language programs, shell scripts do not have to be converted into machine language.
Website Development with PHP and MySQL Saving Data.
PHP2. PHP Form Handling The PHP $_GET and $_POST variables are used to retrieve information from forms, like user input. Name: Age:
Introduction to Programming Using C Files. 2 Contents Files Working with files Sequential files Records.
File IO and command line input CSE 2451 Rong Shi.
Open Source Server Side Scripting ECA 236 Open Source Server Side Scripting PHP & MySQL.
What is PHP? PHP stands for PHP: Hypertext Preprocessor PHP is a server-side scripting language, like ASP PHP scripts are executed on the server PHP supports.
Open Source Server Side Scripting ECA 236 Open Source Server Side Scripting MySQL – Inserting Data.
Chapter 15 Introduction to PL/SQL. Chapter Objectives  Explain the benefits of using PL/SQL blocks versus several SQL statements  Identify the sections.
1 Chapter 7 – Object-Oriented Programming and File Handling spring into PHP 5 by Steven Holzner Slides were developed by Jack Davis College of Information.
Storing and Retrieving Data
5 1 Data Files CGI/Perl Programming By Diane Zak.
Introduction to File Processing with PHP - Part 2 Indexed Files.
Outline Overview Opening a file How to create a file ? Closing a File Check End-of-file Reading a File Line by Line Reading a File Character by Character.
PHP 4 Files, Functions. Opening a File The fopen() function is used to open files in PHP. The first parameter of this function contains the name of the.
Chapter 5 Working with Files and Directories PHP Programming with MySQL 2 nd Edition.
8 th Semester, Batch 2008 Department Of Computer Science SSUET.
Using Text Files in Excel File I/O Methods. Working With Text Files A file can be accessed in any of three ways: –Sequential access: By far the most common.
Chapter 11: Data Files and File Processing Files and streams Creating a sequential access file Reading data from a sequential access file Using fgetc()
Chapter 7 : File Processing1 File-Oriented Input & Output CHAPTER 7.
1 CHAPTER6 CHAPTER 6. Objectives: You’ll learn about;  Introduction  Files and streams  Creating a sequential access file  Reading data from a sequential.
CNG 140 C Programming (Lecture set 10) Spring Chapter 10 Data Files.
8 Chapter Eight Server-side Scripts. 8 Chapter Objectives Create dynamic Web pages that retrieve and display database data using Active Server Pages Process.
PHP-5- Working with Files and Directories. Reading Files PHP’s file manipulation API is extremely flexible: it lets you read files into a string or into.
GAME203 – C Files stdio.h C standard Input/Output “getchar()”
FILES. open() The open() function takes a filename and path as input and returns a file object. file object = open(file_name [, access_mode][, buffering])
Files A collection of related data treated as a unit. Two types Text
Web Page Designing With Dreamweaver MX\Session 1\1 of 9 Session 3 PHP Advanced.
Files. FILE * u In C, we use a FILE * data type to access files. u FILE * is defined in /usr/include/stdio.h u An example: #include int main() { FILE.
Dr. Abdullah Almutairi Spring PHP is a server scripting language, and a powerful tool for making dynamic and interactive Web pages. PHP is a widely-used,
C Programming Day 2. 2 Copyright © 2005, Infosys Technologies Ltd ER/CORP/CRS/LA07/003 Version No. 1.0 Union –mechanism to create user defined data types.
Flock in PHP By Chao Liang. Basic Flock Concept What is flock? Abbreviation for file locking Why flock? Data consistency. Prevent file corruption. Different.
FILES AND EXCEPTIONS Topics Introduction to File Input and Output Using Loops to Process Files Processing Records Exceptions.
FILE I/O: Low-level 1. The Big Picture 2 Low-Level, cont. Some files are mixed format that are not readable by high- level functions such as xlsread()
Silberschatz and Galvin  C Programming Language Kingdom of Saudi Arabia Ministry of Higher Education Al-Majma’ah University College of Education.
PHP using MySQL Database for Web Development (part II)
Excel Tutorial 8 Developing an Excel Application
File I/O.
Introduction to Scripting
File Input/Output.
Topics Introduction to File Input and Output
<?php require("header.htm"); ?>
Beginning C Lecture 11 Lecturer: Dr. Zhao Qinpei
PHP.
File I/O in C Lecture 7 Narrator: Lecture 7: File I/O in C.
Text and Binary File Processing
File Input and Output.
Files.
Lecture 6: Processing Forms with PHP
Topics Introduction to File Input and Output
Presentation transcript:

Open Source Server Side Scripting ECA 236 Open Source Server Side Scripting Files & Directories

Open Source Server Side Scripting 2 ECA 236 flat files  2 ways of storing information  flat files  databases  advantages of flat files  no specific knowledge of databases required  no extra charge

Open Source Server Side Scripting 3 ECA 236 flat files cont …  disadvantages of flat files  slow when working with large files  searching is difficult and slow  problems allowing more than one user simultaneous access  files are processed sequentially  difficult to allow different levels of access

Open Source Server Side Scripting 4 ECA 236 permissions  directory access ( permissions ) may be granted to:  owner: generally has permission to read and write  groups: users can be organized into groups  world: everyone else  types of file permissions  r readable  w writeable  x executable

Open Source Server Side Scripting 5 ECA 236 file processing  to write to a file  open the file – create it if it does not already exist  write data to the file  close the file  to read from a file  open the file – if it cannot be opened, exit gracefully  read data from the file  close the file

Open Source Server Side Scripting 6 ECA 236 opening a file  fopen( ) takes two parameters  the file to be opened  the mode in which to open it  path to file name  create a variable to hold the path to the file  $_SERVER[ 'DOCUMENT_ROOT‘ ] contains the document root directory  $dir_name = dirname($_SERVER["PHP_SELF"]);

Open Source Server Side Scripting 7 ECA 236 opening a file  path to file name  create a variable to hold the path to the file  $_SERVER[ 'DOCUMENT_ROOT‘ ] contains the document root directory  $dir_name = dirname($_SERVER["PHP_SELF"]); $doc_root = $_SERVER["DOCUMENT_ROOT"]; $dir_name = dirname($_SERVER["PHP_SELF"]); $fp = fopen( $doc_root. $dir_name. "/dogs_1.txt", 'w' ) or die ( “Cannot open the file” );

Open Source Server Side Scripting 8 ECA 236 opening a file cont …  fopen( ) takes 4 parameters the 3 rd and 4 th are optional  the file to be opened  the mode in which to open it  search option  option to open file in remote location  mode  indicates the purpose for opening the file  read  write >overwrite >append  both

Open Source Server Side Scripting 9 ECA 236 opening a file cont … Mode Description r Read Mode: Open for reading only. Place the file pointer at the beginning of the file. r+ Read mode: Open for reading and writing. Place the file pointer at the beginning of the file. w Write Mode: Open for writing only. Place the file pointer at the beginning of the file. If the file already exists, delete the existing data. If the file does not exist, attempt to create it. w+ Write Mode: Open for writing and reading. Place the file pointer at the beginning of the file. If the file already exists, delete the existing data. If the file does not exist, attempt to create it. a Append mode: Open the file for appending only. Start from the end of the existing data. If the file does not exist, attempt to create it. a+ Append mode: Open the file for appending and reading. Start from the end of the existing data. If the file does not exist, attempt to create it.

Open Source Server Side Scripting 10 ECA 236 opening a file cont … Mode Description x Cautious Write Mode: Open the file for writing, beginning from the start of the file. If the file already exists, it will not be opened, fopen( ) will return false, and PHP will generate an error. x+ Cautious Write Mode: Open the file for writing and reading, beginning from the start of the file. If the file already exists, it will not be opened, fopen( ) will return false, and PHP will generate an error. b Binary Mode: Used in conjunction with one of the other modes. Use this mode if the Operating System differentiates between binary and text files. Windows OS differentiates, Unix OS does not. PHP developers recommend the use of this option for maximum portability. This is the default mode. t Text Mode: Used in conjunction with one of the other modes. This mode is an option only with Windows OS. It is not recommended except before porting the code with the b option.

Open Source Server Side Scripting 11 ECA 236 opening a file cont …  assign the fopen( ) call to a file pointer  use the file pointer to refer to the open file  if the file cannot be opened  operator suppresses the notice  die( ) is called to generate error message and stop execution of script, thus exiting $fp = fopen( $file_name, "w" ) or die ( “Cannot open the file” );

Open Source Server Side Scripting 12 ECA 236 writing to a file  fwrite( ) takes three parameters, the third is optional  file pointer  string which will be written to the file  maximum number of bytes  if exceeded, PHP truncates the output string fwrite( $fp, $string_output, 1000 );

Open Source Server Side Scripting 13 ECA 236 writing to a file cont …  separate the sections of data with a delimiter  pipe, comma, tab, etc  if the data will be used in another application, follow the rules of the other application  the delimiter should not be one which may be used in the user’s input  end the string with a record separator (newline ) $string_output = “$name|$gender|$height|$weight|$age\n”;

Open Source Server Side Scripting 14 ECA 236 closing the file  use the fclose( ) function to close the file  fclose( ) takes one parameter, the file pointer fclose( $fp );

Open Source Server Side Scripting 15 ECA 236 reading from a file  open the file with the fopen( ) function  use the correct mode to write to the $fp = fopen( $file_name, “r" ) or die ( “Cannot open the file” ); if ( filesize( "$doc_root/files/file_name.txt“ ) != 0 ){ while( !feof( $fp ) ) { $line = fgets( $fp, 1000 ); echo $line. “ ”; } //end while } // end if fclose( $fp );

Open Source Server Side Scripting 16 ECA 236 reading from a file cont …  if the file opens correctly, read from the file until the end of the file is reached  feof( ) returns TRUE when the end of the file is reached  feof( ) takes one parameter, the file pointer while( !feof( $fp ) ) {

Open Source Server Side Scripting 17 ECA 236 reading from a file cont …  when using feof( )check to see if the filesize is 0  if the filesize is zero an infinite loop is generated  if there is a problem opening the file an infinite loop is generated  filesize( ) returns the size of the file in bytes  filesize( ) takes one parameter, the path to the file if ( filesize("$doc_root/files/file_name.txt" != 0 ){

Open Source Server Side Scripting 18 ECA 236 reading from a file cont …  fgets( ) reads from the file one line at a time  fgets( ) takes two parameters, the second is optional  the file pointer  maximum number of bytes to be returned  fgets( ) returns each line as a string $line = fgets( $fp, 1000 );

Open Source Server Side Scripting 19 ECA 236 reading from a file cont …  fgetss( ) reads from the file one line at a time  fgetss( ) takes three parameters, the third is optional  the file pointer  maximum number of bytes to be returned  allowable tags  fgetss( ) returns each line as a string, with HTML and PHP removed $line = fgetss( $fp, 1000 );

Open Source Server Side Scripting 20 ECA 236 reading from a file cont …  fgetcsv( ) reads from the file one line at a time  fgetcsv( ) takes four parameters, the third and fourth are optional  the file pointer  maximum number of bytes to be returned  delimiter ( defaults to comma )  enclosure ( defaults to double quotation marks )  fgetcsv( ) returns each line as an array, separated on the delimiter $line_array = fgetcsv( $fp, 1000, “|” );

Open Source Server Side Scripting 21 ECA 236 reading from a file cont …  readfile( )  opens the file  echoes entire file to browser  closes file  no need to manually open and close file  readfile( ) takes the path to the file as an argument readfile( "$doc_root/files/file_name.txt" );

Open Source Server Side Scripting 22 ECA 236 reading from a file cont …  file( )  opens the file  file is returned as an array, each line as an array element ( newline is still attached )  closes file  no need to manually open and close file  file( ) takes the path to the file as an argument $file_array = file( "$doc_root/files/file_name.txt" );

Open Source Server Side Scripting 23 ECA 236 reading from a file cont …  file_get_contents( )  opens the file  file is returned as a string  closes file  no need to manually open and close file  file_get_contents( ) takes the path to the file as an argument $file_string = file_get_contents( "$doc_root/files/file_name.txt" );

Open Source Server Side Scripting 24 ECA 236 reading from a file cont …  fgetc( ) takes one parameter  file pointer  fgetc( ) returns one character at a time  in addition to string characters, fgetc( ) reads newline and eof characters $char = fgetc( $fp );

Open Source Server Side Scripting 25 ECA 236 reading from a file cont …  file_exists( )  checks whether a file or directory exists  returns Boolean TRUE if it exists  FALSE if it does not  file_exists( ) takes the path to the file as an argument file_exists( "$doc_root/files/file_name.txt" );

Open Source Server Side Scripting 26 ECA 236 reading from a file cont …  unlink( )  deletes a file  unlink( ) takes the path to the file as an argument unlink( "$doc_root/files/file_name.txt" );

Open Source Server Side Scripting 27 ECA 236 file locking  flock( ) takes two parameters  file pointer  a constant indicating the kind of lock to acquire Constant Description LOCK_SH Reading Lock: the file can be shared with other readers. LOCK_EX Writing Lock: the file cannot be shared. The lock is exclusive. LOCK_UN Unlock: release the existing lock.

Open Source Server Side Scripting 28 ECA 236 security  to create a file in a directory above the document root  make sure directory exists before writing to it  test it carefully on your server $doc_root = $_SERVER[ 'DOCUMENT_ROOT' ]; $file_name ="$doc_root/../files/file_name.txt"; $fp = fopen( $file_name, "w+" );

Open Source Server Side Scripting 29 ECA 236 security cont …  crypt( ) function  one-way algorithm to encrypt a string value  no function to decrypt the string  crypt( ) takes two arguments  string to be encrypted  salt string  salt does not work the same on different systems

Open Source Server Side Scripting 30 ECA 236 security cont …  crypt( ) function  given the same string and the same salt, the same encrypted result is returned each time  we do not need to know the original value of the encrypted string  compare encrypted result to password entered by user after running through the crypt( ) function, with the stored, encrypted password as the salt if( crypt( $user_entered_pw, $enc_pw_stored ) == $enc_pw_stored ) { // code if passwords match }