martin kruliš 19. 3. 2015 by martin kruliš (v1.0)1

32
Files and Filesystem Martin Kruliš 19. 3. 2015 by Martin Kruliš (v1.0) 1

Upload: kimberly-sutton

Post on 25-Dec-2015

218 views

Category:

Documents


0 download

TRANSCRIPT

Page 1: Martin Kruliš 19. 3. 2015 by Martin Kruliš (v1.0)1

by Martin Kruliš (v1.0) 1

Files and FilesystemMartin Kruliš

19. 3. 2015

Page 2: Martin Kruliš 19. 3. 2015 by Martin Kruliš (v1.0)1

by Martin Kruliš (v1.0) 2

Low Level C-like API◦ fopen(), fread(), fwrite(), fclose(), …◦ Data are treated as strings

Note that per-byte processing is extremely slow

File-wide Operations◦ fpassthru(), readfile() – whole file is spilled to

the output◦ file_get_contents(), file_put_contents()

Read/write the entire file to/from a string◦ file() – reads entire file as an array of lines

19. 3. 2015

Working with Files

Page 3: Martin Kruliš 19. 3. 2015 by Martin Kruliš (v1.0)1

by Martin Kruliš (v1.0) 3

POSIX Based API◦ Only partial functionality on non-POSIX OSes◦ touch() – sets current time to a file◦ tmpfile() – creates temporary file◦ rename(), unlink() – unlink ~ delete◦ mkdir(), rmdir() – make/remove dir

Synchronization Issues◦ POSIX atomic operations (append, mkdir, …)◦ Advisory file locking mechanism – flock()

Locks are bounded to processes, multi-threaded web server need not guarantee sync. between PHP scripts

19. 3. 2015

File System

Page 4: Martin Kruliš 19. 3. 2015 by Martin Kruliš (v1.0)1

by Martin Kruliš (v1.0) 4

File Properties (Size, Type, Creation Time, …)◦ Dedicated functions for some properties

filesize(), filetype(), filectime()◦ Reading all properties at once

stat(), lstat(), fstat()

POSIX Access Rights (rwx)◦ Testing functions

is_file(), is_readable(), is_writeable(), …◦ Modification of rights and owner

chmod(), chown(), chgrp()

19. 3. 2015

File Properties and Rights

Page 5: Martin Kruliš 19. 3. 2015 by Martin Kruliš (v1.0)1

by Martin Kruliš (v1.0) 5

Traversing Directories◦ Iterative – opendir(), readdir(), closedir()◦ scandir() – returns array with directory contents◦ dir() – returns object of Directory class

Directory Paths◦ glob() – expanding wildcards◦ dirname(), basename() – get part of path◦ pathinfo() – parses path and return components◦ realpath() – converts symbolic to real path

Absolute path without links, ./, and ../

19. 3. 2015

Directories and Paths

Page 6: Martin Kruliš 19. 3. 2015 by Martin Kruliš (v1.0)1

by Martin Kruliš (v1.0) 6

CSV Files◦ Comma (semicolon) separated values◦ API uses file handlers from fopen()

fgetcsv() – reads one CSV line fputcsv() – writes one CSV line

INI Files◦ Configuration files with “name = value” items◦ parse_ini_file() – reads the entire file and

yields an associative array Optionally divides the data to sections

19. 3. 2015

Formatted Files

Page 7: Martin Kruliš 19. 3. 2015 by Martin Kruliš (v1.0)1

by Martin Kruliš (v1.0) 7

ZIP Files (*.zip)◦ Rather complicated (ZIP is multi-file container)◦ Represented by ZipArchive class

Zlib – gzip library (*.gz)◦ Similar functions to standard C-like API

gzopen(), gzread(), gzwrite(), gzclose(), … BZip Library (*.bz2)

◦ Similar to Zlib (bzopen(), …) Other libraries are available (RAR, LZF, …)

◦ TAR only as PECL extension

19. 3. 2015

Compression

Page 8: Martin Kruliš 19. 3. 2015 by Martin Kruliš (v1.0)1

by Martin Kruliš (v1.0) 8

XML and JSONMartin Kruliš

19. 3. 2015

When confronted with a problem, some people thinks: “Hey, I’ll use

XML!”Then they have two problems…

Page 9: Martin Kruliš 19. 3. 2015 by Martin Kruliš (v1.0)1

by Martin Kruliš (v1.0) 9

Parsing Process◦ Loading XML from text file into memory structures

Simple API for XML (SAX)◦ The document is processed sequentially◦ Important “events” are reported to the user

Opening/closing an element, attribute found, … Document Object Model (DOM)

◦ Tree-based structure with object oriented API◦ Nodes of the tree represent elements, attributes,

text content, ….◦ More suitable for mutable documents

19. 3. 2015

Parsing XML

Page 10: Martin Kruliš 19. 3. 2015 by Martin Kruliš (v1.0)1

by Martin Kruliš (v1.0) 10

XPath Query Language◦ Inspired by filesys. paths (elements ~ directories)◦ Various types of queries that yield

Boolean, number, string, or set of DOM nodes◦ Quite expressive (num. operations, conditions, …)

XSLT Transformations◦ XSLT describes transformation of one XML

document to another (or to HTML, or to plain-text) XSLT rules are stored in XML document

◦ Uses XPath for search queries◦ Turing complete language

19. 3. 2015

XPath and XSLT

Page 11: Martin Kruliš 19. 3. 2015 by Martin Kruliš (v1.0)1

by Martin Kruliš (v1.0) 11

XML Utilities in PHP◦ SAX parser◦ DOM API and parser◦ XPath query engine (over a DOM API)◦ XSLT processor◦ PHP-specific Simple XML API (simplified DOM)

Most of the API is object oriented◦ DOM is object oriented by design◦ Simple XML is based on specific properties of PHP

classes (e.g., iterators or magic methods)

19. 3. 2015

XML and PHP

Page 12: Martin Kruliš 19. 3. 2015 by Martin Kruliš (v1.0)1

by Martin Kruliš (v1.0) 12

Event Driven Parsing◦ The document is processed as a stream

In a single call of xml_parse()◦ Important encounters are reported via callbacks

xml_set_element_handler() xml_set_character_handler() …

19. 3. 2015

PHP SAX Parser

Example 1

Page 13: Martin Kruliš 19. 3. 2015 by Martin Kruliš (v1.0)1

by Martin Kruliš (v1.0) 13

Document Object Model in PHP◦ Set of PHP-native classes◦ Implementing DOM 3 as defined by W3C

DOMDocument – entire XML document DOMElement – tree node of one XML element DOMAttr – attribute of an element DOMNode – base class for all tree classes DOMCharacterData – plain text contents …

19. 3. 2015

PHP DOM Implementation

Page 14: Martin Kruliš 19. 3. 2015 by Martin Kruliš (v1.0)1

by Martin Kruliš (v1.0) 14

Document Validation◦ DOMDocument methods for validation

validate(), schemaValidate() Automatic validation when the document is parsed

If validateOnParse == true

DOMXPath Class for XPath Search◦ Operates on DOMDocument◦ Search is performed by query() or evaluate()◦ The result is either basic PHP type (boolean, float, or string) or DOMNodeList object

19. 3. 2015

PHP XML Validation and XPath

Example 2

Page 15: Martin Kruliš 19. 3. 2015 by Martin Kruliš (v1.0)1

by Martin Kruliš (v1.0) 15

XSLTProcessor Class◦ Uses libxslt external library◦ Transformation rules are loaded from a DOM

document or from a Simple XML Element◦ Transformation input is a DOMDocument object◦ The result is yielded

As a new DOMDocument object String with serialized XML/HTML/text document Directly to a file

19. 3. 2015

PHP and XSLT

Example 3

Page 16: Martin Kruliš 19. 3. 2015 by Martin Kruliš (v1.0)1

by Martin Kruliš (v1.0) 16

Simplified DOM API◦ Also loads the document into tree structure◦ All nodes are SimpleXMLElement objects

Tree traversal is implemented by overloading class iterators and magic methods __get(), __set(), …

◦ The structure can be modified and saved◦ Some advanced functions are also available

Integrated XPath query processor Conversions to/from DOM structure

19. 3. 2015

Simple XML in PHP

Example 4

Page 17: Martin Kruliš 19. 3. 2015 by Martin Kruliš (v1.0)1

by Martin Kruliš (v1.0) 17

JavaScript Object Notation (JSON)◦ Lightweight interchange format for structured

data◦ Based on subset of JavaScript language◦ Otherwise language independent

Many parsers exist with frontends for many languages

◦ Intended for replacing XML in simple scenarios

Syntax◦ Two basic structures: collections and lists◦ Supports strings, numbers, bools, and null type◦ Unicode safe

19. 3. 2015

JSON - Revision

Page 18: Martin Kruliš 19. 3. 2015 by Martin Kruliš (v1.0)1

by Martin Kruliš (v1.0) 18

JSON Example[ { "StudentId": 42, "Name": "John Smith" }, { "StudentId": 54, "Name": "Jane Johnson", "Graduated": true }]

19. 3. 2015

JSON – Revision

Ordered list

Named collection

Number (int)

Unicode string

Boolean literal

Page 19: Martin Kruliš 19. 3. 2015 by Martin Kruliš (v1.0)1

by Martin Kruliš (v1.0) 19

JSON Functions◦ json_decode() – JSON string -> object/array struct.◦ json_encode() – serialize any PHP type (except

resource) into JSON string◦ json_last_error(), json_last_error_msg()

Customizing Serialization Process◦ JsonSerializable interface◦ Abstract method jsonSerialize() that converts

object into anything that can be processed by json_encode()

19. 3. 2015

JSON in PHP

Page 20: Martin Kruliš 19. 3. 2015 by Martin Kruliš (v1.0)1

by Martin Kruliš (v1.0) 20

Image Processing in PHP

Martin Kruliš

19. 3. 2015

Page 21: Martin Kruliš 19. 3. 2015 by Martin Kruliš (v1.0)1

by Martin Kruliš (v1.0) 21

PHP: Hypertext Preprocessor◦ Not restricted only to (hyper)text◦ HTTP with MIME can accommodate any content

Compressed packages (zip, tar.gz, …) Application documents (PDF, …) Images

Caveats◦ PHP is text oriented

Binary data are represented as strings◦ PHP is interpreted

Processing binary data might be slow

19. 3. 2015

Non-textual Data

Page 22: Martin Kruliš 19. 3. 2015 by Martin Kruliš (v1.0)1

by Martin Kruliš (v1.0) 22

GD Library◦ The php_gd2 module◦ Supports various formats (JPEG, GIF, PNG, …)

Working with Images◦ Image is a resource$image = imagecreate(400, 300);

◦ Large variety of functions for manipulation◦ Image is not compressed in internal

representation◦ Can be saved or written to output

imagepng(), imagejpg(), imagegif() Corresponding MIME type needs to be set

19. 3. 2015

Images in PHP

Page 23: Martin Kruliš 19. 3. 2015 by Martin Kruliš (v1.0)1

by Martin Kruliš (v1.0) 23

Functions◦ Loading/saving supported formats◦ Basic pixel operations◦ Drawing simple geometric shapes

Lines, circles, polygons, …◦ Text rendering

Built-in fonts, PostScript fonts, TrueType fonts◦ Transformation and resampling◦ Copying, alpha blending

Functions are version specific◦ And time/memory demanding

19. 3. 2015

GD Library

Example 5

Page 24: Martin Kruliš 19. 3. 2015 by Martin Kruliš (v1.0)1

by Martin Kruliš (v1.0) 24

Exchangeable Image File Format◦ Metadata for photographs◦ Extension of JPEG and TIFF containers

PHP API◦ Read only functions◦ exif_imagetype() – returns image type◦ exif_read_data() – reads meta-data from image◦ exif_thumbnail() – returns the image thumbnail

(if present) as binary string

19. 3. 2015

EXIF

Example 6

Page 25: Martin Kruliš 19. 3. 2015 by Martin Kruliš (v1.0)1

by Martin Kruliš (v1.0) 25

Cairo◦ Fast 2D drawing library written in C◦ Supports transformations and Bezier curves

Gmagic◦ A Swiss army knife for image manipulation◦ Large variety of supported formats

ImageMagic◦ Well established library for image manipulation◦ Designed even for more complex operations

19. 3. 2015

Other Libraries

Page 26: Martin Kruliš 19. 3. 2015 by Martin Kruliš (v1.0)1

by Martin Kruliš (v1.0) 26

PHP and Database Management

SystemsMartin Kruliš

19. 3. 2015

Page 27: Martin Kruliš 19. 3. 2015 by Martin Kruliš (v1.0)1

by Martin Kruliš (v1.0) 27

MySQL Revision◦ Original mysql API is deprecated (as of PHP 5.5)◦ MySQL Improved (mysqli) API

Dual object/procedural interface Procedural interface is similar to original (deprecated)

API Advanced connectivity features

Persistent connections, compression, encryption Directly supports transactions

◦ MySQL Native Driver (mysqlnd) extension More direct access to MySQL server Additional features (e.g., asynchronous queries)

19. 3. 2015

MySQL

Page 28: Martin Kruliš 19. 3. 2015 by Martin Kruliš (v1.0)1

by Martin Kruliš (v1.0) 28

MySQLi API◦ mysqli class – the connection to the DBMS

Manage controls, settings, info, stats, … Issue queries and multi-queries Performs transaction control

◦ mysqli_stmt class – SQL statement representation SQL statement preparation and configuration Argument bindings

◦ mysqli_result class – SELECT result wrapper Various methods for accessing data in the result

19. 3. 2015

MySQL

Page 29: Martin Kruliš 19. 3. 2015 by Martin Kruliš (v1.0)1

by Martin Kruliš (v1.0) 29

Extensions◦ mysqlnd_qc

Transparent cache for MySQL queries◦ mysqlnd_memcache

Special extension that utilizes InnoDB Memcache Translates SQL statements to Memcache protocol

◦ mysqlnd_uh Allows user to insert hooks for low-level calls Can be used for monitoring or auditing

◦ mysqlnd_mux Transparent client-side connection multiplexing

19. 3. 2015

MySQL

Page 30: Martin Kruliš 19. 3. 2015 by Martin Kruliš (v1.0)1

by Martin Kruliš (v1.0) 30

PostrgreSQL◦ Quite powerful alternative for MySQL◦ Similar API to MySQL

pg_connect(), pg_query(), …

SQLite◦ SQL engine running directly on filesystem◦ Small project, where regular DBMS is not available

Commercial Giants◦ MSSQL, Oracle, dBase, IBM DB2, …

19. 3. 2015

Other Database Systems

Page 31: Martin Kruliš 19. 3. 2015 by Martin Kruliš (v1.0)1

by Martin Kruliš (v1.0) 31

Abstract Database Layers◦ Solving the problem of portability

If the application refrains from using specific SQL statements

PHP Data Objects (PDO)◦ Generic interface for various RDBMS◦ Extension php_pdo, individual DB systems are in

separate extensions (php_pdo_mysql, …)◦ Simple object oriented API

PDO and PDOStatement classes Minimalistic, no special functions

19. 3. 2015

PDO Interface

Page 32: Martin Kruliš 19. 3. 2015 by Martin Kruliš (v1.0)1

by Martin Kruliš (v1.0) 3219. 3. 2015

Discussion