CSE 154 LECTURE 16: FILE I/O; FUNCTIONS. Query strings and parameters URL?name=value&name=value...

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.
More on PHP Coding Lab no. 6 Advance Database Management System.
CS Lecture 03 Outline Sed and awk from previous lecture Writing simple bash script Assignment 1 discussion 1CS 311 Operating SystemsLecture 03.
1 CSE 390a Lecture 2 Exploring Shell Commands, Streams, and Redirection slides created by Marty Stepp, modified by Josh Goodwin
Form Basics CS Web Data  Most interesting web pages revolve around data  examples: Google, IMDB, Digg, Facebook, YouTube, Rotten Tomatoes  can.
CSE 303 Lecture 2 Introduction to bash shell
1 CS428 Web Engineering Lecture 19 Data Types (PHP - II)
CSE 154 LECTURE 9: FORMS. Web data most interesting web pages revolve around data examples: Google, IMDB, Digg, Facebook, YouTube, Rotten Tomatoes can.
JSP Standard Tag Library
More on PHP: Arrays, Functions and Files
Web forms in PHP Forms Recap  Way of allowing user interaction  Allows users to input data that can then be processed by a program / stored in a back-end.
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.
CSE 143 Lecture 7 Stacks and Queues reading: "Appendix Q" (see course website) slides created by Marty Stepp and Hélène Martin
CSE 143 Lecture 11 Recursive Programming reading: slides created by Marty Stepp and Hélène Martin
1 PHP and MySQL. 2 Topics  Querying Data with PHP  User-Driven Querying  Writing Data with PHP and MySQL PHP and MySQL.
Chap 3 – PHP Quick Start COMP RL Professor Mattos.
Chapter 8 Cookies And Security JavaScript, Third Edition.
PHP - Basic Language Constructs CSCI 297 Scripting Languages - Day Two.
Mark Dixon 1 03 – Passing Data between pages: Forms, Sessions, & Query Strings.
HTML FORMS GET/POST METHODS. HTML FORMS HTML Forms HTML forms are used to pass data to a server. A form can contain input elements like text fields, checkboxes,
Linux file system "On a UNIX system, everything is a file; if something is not a file, it is a process." Sorts of files (on a Linux system) Directories:
Lecture 24CS311 – Operating Systems 1 1 CS311 – Lecture 24 Outline Final Exam Study Guide Note: These lecture notes are not intended replace your notes.
1 PHP Intro PHP Strings After this lecture, you should be able to: Manipulate and Output PHP Strings: Manipulate and Output PHP Strings: Single- or Double-quoted.
CSE 154 LECTURE 5: INTRO TO PHP. URLs and web servers usually when you type a URL in your browser: your computer looks up the.
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.
LIN Unix Lecture 5 Unix Shell Scripts. LIN Command Coordination ; && || command1 ; command2 Interpretation: Do command 1. Then do command.
16. Python Files I/O Printing to the Screen: The simplest way to produce output is using the print statement where you can pass zero or more expressions,
CSE 374 Programming Concepts & Tools Hal Perkins Fall 2015 Lecture 2a – A Unix Command Sampler (Courtesy of David Notkin, CSE 303)
Department of Electrical and Computer Engineering Introduction to Perl By Hector M Lugo-Cordero August 26, 2008.
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.
1 CSE 390a Lecture 2 Exploring Shell Commands, Streams, and Redirection slides created by Marty Stepp, modified by Jessica Miller & Ruth Anderson
Arrays Strings and regular expressions Basic PHP Syntax CS380 1.
Introduction to Programming the WWW I CMSC Winter 2003 Lecture 17.
CITA 310 Section 4 Apache Configuration (Selected Topics from Textbook Chapter 6)
CGS 3066: Web Programming and Design Spring 2016 PHP.
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.
CSE 154 LECTURE 15: EMBEDDED PHP. PHP syntax template HTML content HTML content HTML content... PHP any contents of.
PHP. What is PHP? PHP Hypertext Processor – Dynamic web development – Scripting language – Can be procedural or OOP(preferred) – PHP code can be embedded.
Radoslav Georgiev Telerik Corporation
FILES AND EXCEPTIONS Topics Introduction to File Input and Output Using Loops to Process Files Processing Records Exceptions.
University of Kansas Department of Electrical Engineering and Computer Science Dr. Susan Gauch April 21, 2005 I T T C Introduction to Web Technologies.
Pemrogaman Web.  File handling is an important part of any web application. You often need to open and process a file for different tasks.
Exploring Shell Commands, Streams, and Redirection
Lecture 16: File I/O; Functions
PHP for Server-Side Programming
CS 330 Class 7 Comments on Exam Programming plan for today:
Linux file system "On a UNIX system, everything is a file;
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.
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 ©
Arrays and files BIS1523 – Lecture 15.
Exploring Shell Commands, Streams, and Redirection
CSE 374 Programming Concepts & Tools
Exploring Shell Commands, Streams, and Redirection
CSE 390a Lecture 2 Exploring Shell Commands, Streams, and Redirection
Exploring Shell Commands, Streams, and Redirection
Video list editor BIS1523 – Lecture 24.
Exploring Shell Commands, Streams, and Redirection
Exploring Shell Commands, Streams, and Redirection
UNIX Reference Sheets CSE 2031 Fall 2010.
CSE 390a Lecture 2 Exploring Shell Commands, Streams, and Redirection
CSE 154 Lecture 15: MORE PHP.
Exploring Shell Commands, Streams, and Redirection
PHP and JSON Topics Review JSON.
Exploring Shell Commands, Streams, and Redirection
Exploring Shell Commands, Streams, and Redirection
SEEM 4540 Tutorial 4 Basic PHP based on w3Schools
Exploring Shell Commands, Streams, and Redirection
Presentation transcript:

CSE 154 LECTURE 16: FILE I/O; FUNCTIONS

Query strings and parameters URL?name=value&name=value 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 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 are GET parameters test whether a given parameter was passed with isset

Default parameter values function name(parameterName = value,..., parameterName = value) { statements; } PHP function print_separated($str, $separator = ", ") { if (strlen($str) > 0) { print $str[0]; for ($i = 1; $i < strlen($str); $i++) { print $separator. $str[$i]; } } PHP print_separated("hello"); # h, e, l, l, o print_separated("hello", "-"); # h-e-l-l-o PHP if no value is passed, the default will be used (defaults must come last)

PHP file I/O functions function name(s)category filefile, file_get_contents, file_put_contentsfile_get_contents file_put_contents reading/writing entire files basenamebasename, file_exists, filesize, fileperms, filemtime, is_dir, is_readable, is_writable, disk_free_spacefile_existsfilesize filepermsfilemtimeis_dir is_readableis_writabledisk_free_space asking for information copycopy, rename, unlink, chmod, chgrp, chown, mkdir, rmdirrenameunlinkchmod chgrpchownmkdirrmdir manipulating files and directories globglob, scandirscandirreading directories

Reading/writing files contents of foo.txtfile("foo.txt")file_get_contents("foo.txt") Hello how r u? I'm fine array( "Hello\n", # 0 "how r u?\n", # 1 "\n", # 2 "I'm fine\n" # 3 ) "Hello\n how r u?\n # a single \n # string I'm fine\n" file function returns lines of a file as an array (\n at end of each) file_get_contents returns entire contents of a file as a single string file_put_contents writes a string into a file

The file function # display lines of file as a bulleted list $lines = file("todolist.txt"); foreach ($lines as $line) { # for ($i = 0; $i < count($lines); $i++) print $line; } PHP file returns the lines of a file as an array of strings each ends with \n ; to strip it, use an optional second parameter: $lines = file("todolist.txt", FILE_IGNORE_NEW_LINES); PHP common idiom: foreach or for loop over lines of file

Splitting/joining strings $array = explode(delimiter, string); $string = implode(delimiter, array); PHP $s = "CSE 190 M"; $a = explode(" ", $s); # ("CSE", "190", "M") $s2 = implode("...", $a); # "CSE M“ PHP explode and implode convert between strings and arrays for more complex string splitting, you can use regular expressions (later)

Example with explode Martin D Stepp Jessica K Miller Victoria R Kirst contents of input file names.txt foreach (file("names.txt") as $name) { $tokens = explode(" ", $name); ?> author:, <?php } author: Stepp, Marty author: Miller, Jessica author: Kirst, Victoria output

Unpacking an array: list list($var1,..., $varN) = array; PHP Allison Obourn (206) contents of input file personal.txt list($name, $phone, $ssn) = file("personal.txt");... list($area_code, $prefix, $suffix) = explode(" ", $phone); PHP the odd list function "unpacks" an array into a set of variables you declare when you know a file or line's exact length/format, use file and list to unpack it

Reading directories functiondescription globreturns an array of all file names that match a given pattern (returns a file path and name, such as "foo/bar/myfile.txt") scandirreturns 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 # 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 I found a file: PHP _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 contentsnew contents Roses are red, Violets are blue. All my base, Are belong to you. 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