Website Development Tutorial 3. Outline Getting data from the user using forms Sending data from a form to a PHP program Writing the data to a file for.

Slides:



Advertisements
Similar presentations
File Management in C. What is a File? A file is a collection of related data that a computers treats as a single unit. Computers store files to secondary.
Advertisements

UFCE8V-20-3 Information Systems Development 3 (SHAPE HK) Lecture 3 PHP (2) : Functions, User Defined Functions & Environment Variables.
File Management in C. A file is a collection of related data that a computers treats as a single unit. File is a collection of data stored permanently.
More on PHP Coding Lab no. 6 Advance Database Management System.
CS 241 Section Week #5 2/23/12. 2 Topics This Section MP4 overview Function Pointers Pthreads File I/O.
Chapter 11: Data Files & File Processing In this chapter, you will learn about Files and streams Creating a sequential access file Reading data from a.
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.
 2007 Pearson Education, Inc. All rights reserved C File Processing.
Chapter 11 C File Processing Acknowledgment The notes are adapted from those provided by Deitel & Associates, Inc. and Pearson Education Inc.
IS 1181 IS 118 Introduction to Development Tools Week 2.
PHP Tutorials 02 Olarik Surinta Management Information System Faculty of Informatics.
Jozef Goetz contribution, Credits:
(c) Manzur Ashraf, Short course, KFUPM PHP & MySQL 1 Basic PHP Class 2.
ITM 352 © Port,KazmanFile I/O - 1 File I/O in PHP Persistent Data.
N305: C Programming Copyright ©2006  Department of Computer & Information Science File Handling in C.
15/10/20151 PHP & MySQL 'Slide materials are based on W3Schools PHP tutorial, 'PHP website 'MySQL website.
PHP - Part 2. More operators... Arithmetic and Assignment Operators e.g., using + and =  $IntA=5; $intB=8; $intC=$intA+$intB; //intC is 13  // Same.
18. PHP File Operations Opening a File
PHP1-1 PHP Lecture 2 Xingquan (Hill) Zhu
PHP2. PHP Form Handling The PHP $_GET and $_POST variables are used to retrieve information from forms, like user input. Name: Age:
 2007 Pearson Education, Inc. All rights reserved C File Processing.
1. Introduction File Declaration and Initialization Creating and Opening File Closing File EOF Reading from and Writing into a File Extra : Random Access.
PHP: A RRAYS, S TRINGS, AND F ILES CSCI 297 Scripting Languages - Day Three.
Fall 2004CSI University of Ottawa Introduction to PHP Basic principles and syntax.
COOKIES and SESSIONS. COOKIES A cookie is often used to identify a user. A cookie is a small file that the server embeds on the user's computer. Each.
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.
File I/O, Project 1: List ADT Bryce Boe 2013/07/02 CS24, Summer 2013 C.
CSEB114: Principle of programming Chapter 11: Data Files & File Processing.
Storing and Retrieving Data
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.
1 File Handling. 2 Storage seen so far All variables stored in memory Problem: the contents of memory are wiped out when the computer is powered off Example:
PHP Programming.
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.
Open Source Server Side Scripting ECA 236 Open Source Server Side Scripting Files & Directories.
8 th Semester, Batch 2008 Department Of Computer Science SSUET.
Advanced Web 2012 Lecture 6 Sean Costain Files Sean Costain 2012 Php allows for the : Creation Reading Appending Deleting Uploading And Closing.
COMP 116: Introduction to Scientific Programming Lecture 29: File I/O.
1 IT 252 Computer Organization and Architecture Interaction Between Systems: File Sharing R. Helps.
Chapter 11 File Processing. Objectives In this chapter, you will learn: –To be able to create, read, write and update files. –To become familiar with.
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.
CHAPTER 8 PHP Advanced อ. ยืนยง กันทะเนตร คณะเทคโนโลยีสารสนเทศและการสื่อสาร มหาวิทยาลัยพะเยา 1.
1 CSC103: Introduction to Computer and Programming Lecture No 27.
Files A collection of related data treated as a unit. Two types Text
CS 241 Section Week #5 9/22/11. 2 Topics This Section File I/O Advanced C.
Web Page Designing With Dreamweaver MX\Session 1\1 of 9 Session 3 PHP Advanced.
FILES IN C. File Operations  Creation of a new file  Opening an existing file  Reading from a file  Writing to a file  Moving to a specific location.
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.
Lecture 20: C File Processing. Why Using Files? Storage of data in variables and arrays is temporary Data lost when a program terminates. Files are 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.
Programming for the Web Data Structure Writing to a file Dónal Mulligan BSc MA
 2007 Pearson Education, Inc. All rights reserved C File Processing.
PHP File Handling. Opening a file Fopen(filename,mode) Closing a file Fclose(filename)
Chapter 6 Persistence-Saving and Retrieving Data
Chapter 22 – part a Stream refer to any source of input or any destination for output. Many small programs, obtain all their input from one stream usually.
TMF1414 Introduction to Programming
1 CHAPTER 10 ADVANCED PHP.
File I/O.
Files I/O, Streams, I/O Redirection, Reading with fscanf
<?php require("header.htm"); ?>
CSE 333 – Section 3 POSIX I/O Functions.
File Manipulation Knowledge Understand the concept of file input and output Skills Able to write an application program involving input output files.
Fundamental of Programming (C)
Python 19 Mr. Husch.
Files.
File I/O in PHP Persistent Data.
Lecture 6: Processing Forms with PHP
Python 19 Mr. Husch.
CSc 352 File I/O Saumya Debray Dept. of Computer Science
EECE.2160 ECE Application Programming
Presentation transcript:

Website Development Tutorial 3

Outline Getting data from the user using forms Sending data from a form to a PHP program Writing the data to a file for permanent storage Reading data back from a file

File reading and writing Useful for data sets too small for a database Enables code to read and write to a file Typical session might follow these steps Open the file for read/write Read in the contents Close the file (if only reading data) Perform operations of the file contents Write the results Close the file W3schools Tutorial

The fopen() function is used to open files in PHP. <?php $file=fopen(“guestbook.txt","r"); ?>

If you can’t open the file <?php $file=fopen(“guestbook.txt","r") or exit("Unable to open file!"); ?>

Closing the file <?php $file = fopen(“guestbook.txt","r"); //some code to be executed fclose($file); ?>

read a file line by line, until the end of file is reached: <?php $file = fopen(“guestbook.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). " "; } fclose($file); ?>

Any writing to a file is done with the use of “fwrite()” or “fputs()” functions, which both use a file pointer and a string to perform the writing:

General Example $fd = fopen($filename, “r+”) or die(“Can’t open file $filename”); $fstring = fread($fd, filesize($filename)); $fout = fwrite($fd,$fstring); fclose($fd);

File open fopen() Have to assign the result to a variable Usually $fd (file descriptor) or $fp (file pointer) fopen() doesn’t return anything useful if unsuccessful (from a test point of view) so use die() to test for failure Six modes for opening “r” – read only “r+” – read and write if the file exists already “w” – write only “w+” – write and read even if the file doesn’t exist “a” – write only to the end of the file “a+” – read and write to the end of the file

File read fread() Takes a file-pointer and file size (in bytes) Most common way to send size is to use the filesize() function $fstring = fread($fd, filesize($filename)); Notice it is filesize($filename), not filesize($fd) – a common mistake Useful for turning a file’s contents into a string, can then use all the string functions to slice and dice the string Can also send the information to an array through use of file() and explode() functions – more later on these

File write Used for writing a string to a file Expects a file pointer and a string Returns the number of characters saved $fout = fwrite($fp, $string); if ($fout != strlen($fstring)) { echo “file write failed”; }

Important note on writing! If we use modes “w” and “w+” we will overwrite all the data in the file Which means that, EVERYTHING WILL BE LOST! If you want to add to beginning of file use “r+” If you want to add to end of file use “a+”

File close Does what it says on the tin fclose($fd); Not essential, PHP close all files at end of script, but its good house keeping