Lecture 16: File I/O; Functions

Slides:



Advertisements
Similar presentations
PHP include file 1 CS380. PHP Include File  Insert the content of one PHP file into another PHP file before the server executes it  Use the  include()
Advertisements

UFCE8V-20-3 Information Systems Development 3 (SHAPE HK) Lecture 3 PHP (2) : Functions, User Defined Functions & Environment Variables.
CGI & HTML forms CGI Common Gateway Interface  A web server is only a pipe between user-agents  and content – it does not generate content.
More forms CS Reset Buttons  specify custom text on the button by setting its value attribute CS380 2 Name: Food: Meat? HTML.
More on PHP Coding Lab no. 6 Advance Database Management System.
FILE TRANSFER PROTOCOL Short for File Transfer Protocol, the protocol for exchanging files over the Internet. FTP works in the same way as HTTP for transferring.
HTTP Hypertext Transfer Protocol. HTTP messages HTTP is the language that web clients and web servers use to talk to each other –HTTP is largely “under.
Form Basics CS Web Data  Most interesting web pages revolve around data  examples: Google, IMDB, Digg, Facebook, YouTube, Rotten Tomatoes  can.
CGI Programming: Part 1. What is CGI? CGI = Common Gateway Interface Provides a standardized way for web browsers to: –Call programs on a server. –Pass.
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.
CSE 154 LECTURE 9: FORMS. Web data most interesting web pages revolve around data examples: Google, IMDB, Digg, Facebook, YouTube, Rotten Tomatoes can.
CGS – 4854 Summer 2012 Web Site Construction and Management Instructor: Francisco R. Ortega Date: May 5 th,2012.
More on PHP: Arrays, Functions and Files
Reading Data in Web Pages tMyn1 Reading Data in Web Pages A very common application of PHP is to have an HTML form gather information from a website's.
FALL 2005CSI 4118 – UNIVERSITY OF OTTAWA1 Part 4 Web technologies: HTTP, CGI, PHP,Java applets)
Simple Web Services. Internet Basics The Internet is based on a communication protocol named TCP (Transmission Control Protocol) TCP allows programs running.
Chapter 33 CGI Technology for Dynamic Web Documents There are two alternative forms of retrieving web documents. Instead of retrieving static HTML documents,
Simple Web Services. Internet Basics The Internet is based on a communication protocol named TCP (Transmission Control Protocol) TCP allows programs running.
CSE 154 LECTURE 12: COOKIES. Including files: include include("filename"); PHP include("header.html"); include("shared-code.php"); PHP inserts the entire.
Introduction to Programming the WWW I CMSC Summer 2004 Lecture 6.
18. PHP File Operations Opening a File
Chapter 5 Working with Files and Directories PHP Programming with MySQL 2 nd Edition.
Open Source Server Side Scripting ECA 236 Open Source Server Side Scripting Files & Directories.
CSE 341, S. Tanimoto Lisp CGI - 1 Lisp CGI Programming for the Web Web servers can invoke Lisp to “intelligently” create web pages on the fly. We will.
ECMM6018 Enterprise Networking for Electronic Commerce Tutorial 7
 Previous lessons have focused on client-side scripts  Programs embedded in the page’s HTML code  Can also execute scripts on the server  Server-side.
CHAPTER 8 PHP Advanced อ. ยืนยง กันทะเนตร คณะเทคโนโลยีสารสนเทศและการสื่อสาร มหาวิทยาลัยพะเยา 1.
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.
COSC 2328 – Web Programming.  PHP is a server scripting language  It’s widely-used and free  It’s an alternative to Microsoft’s ASP and Ruby  PHP.
CSE 154 LECTURE 17: WEB SERVICES. What is a web service? web service: software functionality that can be invoked through the internet using common protocols.
Web Services Essentials. What is a web service? web service: software functionality that can be invoked through the internet using common protocols like.
CSE 154 LECTURE 18: FORMS AND UPLOADING FILES. Exercise: Baby name web service JSON Modify our babynames.php service to produce its output as JSON. For.
PHP. What is PHP? PHP Hypertext Processor – Dynamic web development – Scripting language – Can be procedural or OOP(preferred) – PHP code can be embedded.
CSE 154 LECTURE 16: FILE I/O; FUNCTIONS. Query strings and parameters URL?name=value&name=value...
Simple PHP Web Applications Server Environment
1 Using MVC 6. MVC vs. ASP Web Forms Both run under ASP.NET Can coexist In Web Forms, browser requests page. xxx.aspx and xxx.aspx.cs on the server Page.
National College of Science & Information Technology.
Pemrogaman Web.  File handling is an important part of any web application. You often need to open and process a file for different tasks.
PHP using MySQL Database for Web Development (part II)
CSE 154 Lecture 20: Cookies.
PHP for Server-Side Programming
CS 330 Class 7 Comments on Exam Programming plan for today:
FTP Lecture supp.
19.10 Using Cookies A cookie is a piece of information that’s stored by a server in a text file on a client’s computer to maintain information about.
Objectives In this chapter, you will:
Chapter 19 PHP Part III Credits: Parts of the slides are based on slides created by textbook authors, P.J. Deitel and H. M. Deitel by Prentice Hall ©
* Lecture # 7 Instructor: Rida Noor Department of Computer Science
Arrays and files BIS1523 – Lecture 15.
CSE 154 Lecture 24: JSON.
>> PHP: Form Processing
IS333D: MULTI-TIER APPLICATION DEVELOPMENT
Lecture 19: Forms and uploading files
Topics Introduction to File Input and Output
Hypertext Transfer Protocol
CSE 143 Lecture 11 Recursive Programming reading:
Hypertext Transfer Protocol
Video list editor BIS1523 – Lecture 24.
Web Programming Language
Lecture 5: Functions and Parameters
PART 6 Application Layer
Hypertext Transfer Protocol
Module 6 Working with Files and Directories
CSE 154 Lecture 15: MORE PHP.
Topics Introduction to File Input and Output
PHP-II.
Hypertext Transfer Protocol
Mr. Justin “JET” Turner CSCI 3000 – Fall 2016 Section DA MW 4:05-5:20
PHP and JSON Topics Review JSON.
SEEM 4540 Tutorial 4 Basic PHP based on w3Schools
Presentation transcript:

Lecture 16: File I/O; Functions CSE 154 Lecture 16: File I/O; Functions

What is a web service? web service: software functionality that can be invoked through the internet using common protocols like a remote function(s) you can call by contacting a program on a web server many web services accept parameters and produce results can be written in PHP and contacted by the browser in HTML and/or Ajax code service's output might be HTML but could be text, XML, JSON or other content examples seen in CSE 154: quote.php, animalgame.php,  books_json.php, urban.php, weather.php

Setting content type with header header("Content-type: type/subtype"); PHP header("Content-type: text/plain"); print "This output will appear as plain text now!\n"; PHP by default, a PHP file's output is assumed to be HTML (text/html) use the header function to specify non-HTML output must appear before any other output generated by the script

Recall: Content ("MIME") types related file extension text/plain .txt text/html .html, .htm, ... text/xml .xml application/json .json text/css .css text/javascript .js image/gif .gif Lists of MIME types: by type, by extension

Query strings and parameters URL?name=value&name=value... http://www.google.com/search?q=Romney http://example.com/student_login.php?username=obourn&id=1234567 query string: a set of parameters passed from a browser to a web server often passed by placing name/value pairs at the end of a URL above, parameter username has value obourn, and sid has value 1234567 PHP code on the server can examine and utilize the value of parameters a way for PHP code to produce different output based on values passed by the user

Query parameters: $_GET, $_POST $user_name = $_GET["username"]; $id_number = (int) $_GET["id"]; $eats_meat = FALSE; if (isset($_GET["meat"])) { $eats_meat = TRUE; } PHP $_GET["parameter name"] or $_POST["parameter name"] returns a GET/POST parameter's value as a string parameters specified as http://....?name=value&name=value are GET parameters test whether a given parameter was passed with isset

Example: Exponent web service Write a web service that accepts a base and exponent and outputs base raised to the exponent power. For example, the following query should output 81 : http://example.com/exponent.php?base=3&exponent=4 solution: <?php header("Content-type: text/plain"); $base = (int) $_GET["base"]; $exp = (int) $_GET["exponent"]; $result = pow($base, $exp); print $result; ?> PHP

PHP file I/O functions function name(s) category file, file_get_contents,  file_put_contents reading/writing entire files basename, file_exists, filesize,  fileperms, filemtime, is_dir,  is_readable, is_writable, disk_free_space asking for information copy, rename, unlink, chmod,  chgrp, chown, mkdir, rmdir manipulating files and directories glob, scandir reading directories

Reading directories function description glob returns an array of all file names that match a given pattern  (returns a file path and name, such as "foo/bar/myfile.txt") scandir returns an array of all file names in a given directory  (returns just the file names, such as "myfile.txt") glob can accept a general path with the * wildcard character (more powerful)

glob example glob can match a "wildcard" path with the * character # reverse all poems in the poetry directory $poems = glob("poetry/poem*.dat"); foreach ($poems as $poemfile) { $text = file_get_contents($poemfile); file_put_contents($poemfile, strrev($text)); print "I just reversed " . basename($poemfile) . "\n"; } PHP glob can match a "wildcard" path with the * character glob("foo/bar/*.doc") returns all .doc files in the foo/bar subdirectory glob("food*") returns all files whose names begin with "food" the basename function strips any leading directory from a file path basename("foo/bar/baz.txt") returns "baz.txt"

scandir example <ul> <?php foreach (scandir("taxes/old") as $filename) { ?> <li>I found a file: <?= $filename ?></li> <?php } ?> </ul> PHP . .. 2007_w2.pdf 2006_1099.doc output scandir includes current directory (".") and parent ("..") in the array don't need basename with scandir; returns file names only without directory

Reading/writing an entire file # reverse a file $text = file_get_contents("poem.txt"); $text = strrev($text); file_put_contents("poem.txt", $text); PHP file_get_contents returns entire contents of a file as a string if the file doesn't exist, you will get a warning and an empty return string file_put_contents writes a string into a file, replacing its old contents if the file doesn't exist, it will be created

Appending to a file # add a line to a file $new_text = "P.S. ILY, GTG TTYL!~"; file_put_contents("poem.txt", $new_text, FILE_APPEND); PHP old contents new contents Roses are red, Violets are blue. All my base, Are belong to you. P.S. ILY, GTG TTYL!~ file_put_contents can be called with an optional third parameter to append (add to the end) rather than overwrite