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.

Slides:



Advertisements
Similar presentations
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.
Advertisements

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.
COMP An Introduction to Computer Programming : University of the West Indies COMP6015 An Introduction to Computer Programming Lecture 06.
© Copyright 1992–2004 by Deitel & Associates, Inc. and Pearson Education Inc. All Rights Reserved Introduction Data files –Can be created, updated,
 2007 Pearson Education, Inc. All rights reserved C File Processing.
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.
Chapter 11 C File Processing Acknowledgment The notes are adapted from those provided by Deitel & Associates, Inc. and Pearson Education Inc.
FILE UPLOADS CHAPTER 11. THE BASIC PROCESS 1.The HTML form displays the control to locate and upload a file 2.Upon form submission, the server first stores.
04/09/20151 PHP & MySQL 'Slide materials are based on W3Schools PHP tutorial, 'PHP website 'MySQL website.
More PHP. PHP Version Differences PHP 5.0 requires use of the _REQUEST or _GET or _POST variables to access variables passed in by forms _REQUEST is an.
PHP Tutorials 02 Olarik Surinta Management Information System Faculty of Informatics.
Intermediate PHP (2) File Input/Output & User Defined Functions.
ITM 352 © Port,KazmanFile I/O - 1 File I/O in PHP Persistent Data.
NMED 3850 A Advanced Online Design January 26, 2010 V. Mahadevan.
School of Computing and Information Systems CS 371 Web Application Programming PHP - Basics Serving up web pages.
Web Programming with PHP (2). PHP Scripts o every PHP script is a collection of one or more statements; these statements appear as a collection of names,
More PHP. PHP Version Differences PHP 5.0 requires use of the _REQUEST or _GET or _POST variables to access variables passed in by forms _REQUEST is an.
18. PHP File Operations Opening a File
File Handling Spring 2013Programming and Data Structure1.
PHP2. PHP Form Handling The PHP $_GET and $_POST variables are used to retrieve information from forms, like user input. Name: Age:
COMP An Introduction to Computer Programming : University of the West Indies COMP6015 An Introduction to Computer Programming Lecture 05.
 2007 Pearson Education, Inc. All rights reserved C File Processing.
Characters and Strings File Processing Exercise C Programming:Part 3.
PHP: A RRAYS, S TRINGS, AND F ILES CSCI 297 Scripting Languages - Day Three.
1Computer Sciences Department Princess Nourah bint Abdulrahman University.
File IO and command line input CSE 2451 Rong Shi.
Chapter 8 File-Oriented Input and Output. 8.1 INTRODUCTION a file can also be designed to store data. We can easily update files, A data file as input.
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.
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.
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.
Outline if...else...elseif Statements Switch Loops Functions Arrays Forms.
Open Source Server Side Scripting ECA 236 Open Source Server Side Scripting Files & Directories.
LECTURE 4 Files, File open, Read, Write. File Upload - In this lecture we will teach you how to open, read, and close a file on the server. - PHP Open.
8 th Semester, Batch 2008 Department Of Computer Science SSUET.
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 11 File Processing. Objectives In this chapter, you will learn: –To be able to create, read, write and update files. –To become familiar with.
 2000 Prentice Hall, Inc. All rights reserved Introduction Data files –Can be created, updated, and processed by C programs –Are used for permanent.
© Copyright 1992–2004 by Deitel & Associates, Inc. and Pearson Education Inc. All Rights Reserved. 1 Chapter 11 – File Processing Outline 11.1Introduction.
chap8 Chapter 12 Files (reference: Deitel ’ s chap 11)
C File Processing. Objectives To be able to create, write and update files. To become familiar with sequential access file processing. To become familiar.
GAME203 – C Files stdio.h C standard Input/Output “getchar()”
ITM © Port, Kazman1 ITM 352 Simple Arrays. ITM © Port, Kazman2 Arrays r Collections r Array basics m Declaration, allocation, and initialization.
Strings, Characters, and Regular Expressions Session 10 Mata kuliah: M0874 – Programming II Tahun: 2010.
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.
 2007 Pearson Education, Inc. All rights reserved. 1 C File Processing.
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.
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.
Radoslav Georgiev Telerik Corporation
PHP. 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 File Handling. Opening a file Fopen(filename,mode) Closing a file Fclose(filename)
Silberschatz and Galvin  C Programming Language Kingdom of Saudi Arabia Ministry of Higher Education Al-Majma’ah University College of Education.
Arrays and Strings. Arrays in PHP Arrays are made up of multiple memory blocks, each with the same name and differentiated by an index number Each block.
CS 371 Web Application Programming
CGS 3066: Web Programming and Design Spring 2016
TMF1414 Introduction to Programming
1 CHAPTER 10 ADVANCED PHP.
<?php require("header.htm"); ?>
Beginning C Lecture 11 Lecturer: Dr. Zhao Qinpei
File Input and Output.
Embedded PHP in HTML 5 & Strings in PHP
Fundamental of Programming (C)
CSc 352 File I/O Saumya Debray Dept. of Computer Science
Professor Jodi Neely-Ritz University of Florida
Presentation transcript:

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 file to be opened and the second parameter specifies in which mode the file should be opened in (r, r+, w, w+, a, a+, x, x+): <?php $f=fopen("welcome.txt","r"); ?>

Opening a File <?php if (!($f=fopen("welcome.txt","r"))) exit("Unable to open file!"); ?>

Closing a File The fclose() function is used to close a file. fclose($f);

Reading from a File The feof() function is used to determine if the end of file is true. The fgetc() function is used to read a single character from a file. The fputs($f,sting) function is used to write. <?php if (!($f=fopen("welcome.txt","r"))) exit("Unable to open file."); while (!feof($f)) { $x=fgetc($f); echo $x; } fclose($f); ?>

Reading from a File string fgets ( resource $handle [, int $length ] ) reads a file from the file string fread ( resource $handle, int $length ) reads up to length bytes from the file pointer referenced by handle. Reading stops as soon as: –length bytes have been read –EOF (end of file) is reached –a packet becomes available (for network streams) –8192 bytes have been read (after opening userspace stream)

Reading from a File <?php // Get a file into an array: HTML source of a URL. $lines = file (' // Loop through our array: html + line numbers. foreach ($lines as $line_num => $line) { echo "Line # {$line_num} : "; echo htmlspecialchars($line). " \n"; }... //htmlspecialchars - Convert special characters to //HTML entities

Error Control Operators PHP supports one error control operator: the at sign When prepended to an expression in PHP, any error messages that might be generated by that expression will be ignored. $my_file ('non_existent_file') or die ("Failed opening file: error was '$php_errormsg'"); $value $cache[$key]; // will not issue a notice if the index $key doesn't exist.

Strings string substr ( string $string, int $start [, int $length ] ) returns the portion of string specified by the start and length parameters. $username = substr($username,0,30); //((Mid)) int strlen ( string $string ) - Returns the length of the given string trim(string $string ) – truncates the string str_replace (mixed $lookfor, mised $replacewith, mixed $stringToSearchIn) - Replace all occurrences of the search string with the replacement string strstr - Find first occurrence of a string : for example: strstr($ ,

Functions

Functions

Functions In PHP 3, functions must be defined before they are referenced. No such requirement exists in PHP 4. Except when a function is conditionally defined

Conditionally defined functions <?php $makefoo = true; // We can't call foo() bar(); if ($makefoo) { function foo () { echo "I don't exist until program execution reaches me.\n"; } } if ($makefoo) foo(); function bar() { echo "I exist immediately upon program start.\n"; } ?>

The global keyword <?php $a = 1; $b = 2; function Sum() { $GLOBALS["b"] = $GLOBALS["a"] + $GLOBALS["b"]; } Sum(); echo $b; ?> <?php $a = 1; $b = 2; function Sum() { global $a, $b; $b = $a + $b; } Sum(); echo $b; ?>