PHP Workshop ‹#› File Handling with PHP. PHP Workshop ‹#› Files and PHP File Handling –Data Storage Though slower than a database –Manipulating uploaded.

Slides:



Advertisements
Similar presentations
» PHP arrays are lists of values stored in key-value pairs. » Uses of arrays: Many built-in PHP environment variables. Database functions use arrays.
Advertisements

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.
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.
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.
V 1.0 OE NIK 2013 PHP+SQL 4. File handling basics File-based "database" File-based guestbook 1.
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.
Objectives Handling magic quotes Understand file permissions
Chapter 4 Working with Files and Directories PHP Programming.
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.
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 6 Working with Files and Directories PHP Programming with MySQL.
1 CSC 1401 S1 Computer Programming I Hamid Harroud School of Science and Engineering, Akhawayn University
Chapter 4 File Systems Files Directories Tanenbaum, Modern Operating Systems 3 e, (c) 2008 Prentice-Hall, Inc. All rights reserved
File/Directory manipulation. Opening a File To read from a file, must use a file handle. –by convention, all caps open a file (for reading): –open FILE,
2010/11 : [1]Building Web Applications using MySQL and PHP (W1)Slide Topic Files.
Intermediate PHP (2) File Input/Output & User Defined Functions.
Web Programming 02 – PHP File and Directory Handling Aryo Pinandito, ST, M.MT.
BBK P1 Module2010/11 : [‹#›] File Handling with PHP.
Lecture 06 – Reading and Writing Text Files.  At the end of this lecture, students should be able to:  Read text files  Write text files  Example.
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,
PHP Programming with MySQL Slide 6-1 CHAPTER 6 Working with Files and Directories.
18. PHP File Operations Opening a File
File Systems CSCI What is a file? A file is information that is stored on disks or other external media.
PHP2. PHP Form Handling The PHP $_GET and $_POST variables are used to retrieve information from forms, like user input. Name: Age:
Introduction to Programming Using C Files. 2 Contents Files Working with files Sequential files Records.
PHP Working with Files. Perancangan dan Pemrograman Web: PHP (wcw) Opening a File fopen (path_to_file, file_mode) File Modes File Mode Description ropen.
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.
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.
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.
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.
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.
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.
Advanced Web 2012 Lecture 6 Sean Costain Files Sean Costain 2012 Php allows for the : Creation Reading Appending Deleting Uploading And Closing.
Using Text Files in Excel File I/O Methods. Working With Text Files A file can be accessed in any of three ways: –Sequential access: By far the most common.
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,
Files Tutor: You will need ….
12 – PHP Contd. Informatics Department Parahyangan Catholic University.
Perl Variables: Array Web Programming1. Review: Perl Variables Scalar ► e.g. $var1 = “Mary”; $var2= 1; ► holds number, character, string Array ► e.g.
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.
GAME203 – C Files stdio.h C standard Input/Output “getchar()”
FILES. open() The open() function takes a filename and path as input and returns a file object. file object = open(file_name [, access_mode][, buffering])
ITFN 2601 Introduction to Operating Systems Lecture 22 Files & Directories.
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.
15 – PHP(5) Informatics Department Parahyangan Catholic University.
Files and Streams. Objectives Learn about the classes that support file input/output Understand the concept of abstraction and how it related to the 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
FILES AND EXCEPTIONS Topics Introduction to File Input and Output Using Loops to Process Files Processing Records Exceptions.
Silberschatz and Galvin  C Programming Language Kingdom of Saudi Arabia Ministry of Higher Education Al-Majma’ah University College of Education.
File Handling with PHP.
Objectives In this chapter, you will:
Python’s input and output
APACHE & PHP (except Accessing PHP from SQL
CHAPTER 5 WORKING WITH FILES AND DIRECTORIES PHP PROGRAMMING WITH MYSQL 2ND EDITION MODIFIED BY ANITA PHILIPP –SPRING 2012.
Topics Introduction to File Input and Output
Chapter 7 Files and Exceptions
C Programming Lecture-15 File I/O
<?php require("header.htm"); ?>
File System B. Ramamurthy B.Ramamurthy 11/27/2018.
File IO and Strings CIS 40 – Introduction to Programming in Python
Cmdlets “Command-lets”
Files Handling In today’s lesson we will look at:
Topics Introduction to File Input and Output
Topics Introduction to File Input and Output
Presentation transcript:

PHP Workshop ‹#› File Handling with PHP

PHP Workshop ‹#› Files and PHP File Handling –Data Storage Though slower than a database –Manipulating uploaded files From forms –Creating Files for download

PHP Workshop ‹#› Open/Close a File A file is opened with fopen() as a “stream”, and PHP returns a ‘handle’ to the file that can be used to reference the open file in other functions. Each file is opened in a particular mode. A file is closed with fclose() or when your script ends.

PHP Workshop ‹#› File Open Modes ‘r’ Open for reading only. Start at beginning of file. ‘r+’ Open for reading and writing. Start at beginning of file. ‘w’ Open for writing only. Remove all previous content, if file doesn’t exist, create it. ‘a’ Open writing, but start at END of current content. ‘a+’ Open for reading and writing, start at END and create file if necessary.

PHP Workshop ‹#› File Open/Close Example <?php // open file to read $toread = fopen(‘some/file.ext’,’r’); // open (possibly new) file to write $towrite = fopen(‘some/file.ext’,’w’); // close both files fclose($toread); fclose($towrite); ?>

PHP Workshop ‹#› Now what..? If you open a file to read, you can use more in-built PHP functions to read data.. If you open the file to write, you can use more in-built PHP functions to write..

PHP Workshop ‹#› Reading Data There are two main functions to read data: fgets($handle,$bytes) –Reads up to $bytes of data, stops at newline or end of file (EOF) fread($handle,$bytes) –Reads up to $bytes of data, stops at EOF.

PHP Workshop ‹#› Reading Data We need to be aware of the End Of File (EOF) point.. feof($handle) –Whether the file has reached the EOF point. Returns true if have reached EOF.

PHP Workshop ‹#› Data Reading Example $handle = fopen('people.txt', 'r'); while (!feof($handle)) { echo fgets($handle, 1024); echo ' '; } fclose($handle);

PHP Workshop ‹#› Data Reading Example $handle = fopen('people.txt', 'r'); while (!feof($handle)) { echo fgets($handle, 1024); echo ' '; } fclose($handle); Open the file and assign the resource to $handle $handle = fopen('people.txt', 'r');

PHP Workshop ‹#› Data Reading Example $handle = fopen('people.txt', 'r'); while (!feof($handle)) { echo fgets($handle, 1024); echo ' '; } fclose($handle); While NOT at the end of the file, pointed to by $handle, get and echo the data line by line while (!feof($handle)) { echo fgets($handle, 1024); echo ' '; }

PHP Workshop ‹#› Data Reading Example $handle = fopen('people.txt', 'r'); while (!feof($handle)) { echo fgets($handle, 1024); echo ' '; } fclose($handle); Close the file fclose($handle);

PHP Workshop ‹#› File Open shortcuts.. There are two ‘shortcut’ functions that don’t require a file to be opened: $lines = file($filename) –Reads entire file into an array with each line a separate entry in the array. $str = file_get_contents($filename) –Reads entire file into a single string.

PHP Workshop ‹#› Writing Data To write data to a file use: fwrite($handle,$data) –Write $data to the file.

PHP Workshop ‹#› Data Writing Example $handle = fopen('people.txt', 'a'); fwrite($handle, “\nFred:Male”); fclose($handle);

PHP Workshop ‹#› Data Writing Example $handle = fopen('people.txt', 'a'); fwrite($handle, '\nFred:Male'); fclose($handle); $handle = fopen('people.txt', 'a'); Open file to append data (mode 'a') fwrite($handle, “\nFred:Male”); Write new data (with line break after previous data)

PHP Workshop ‹#› Other File Operations Delete file –unlink('filename'); Rename (file or directory) –rename('old name', 'new name'); Copy file –copy('source', 'destination'); And many, many more! –

PHP Workshop ‹#› Dealing With Directories Open a directory –$handle = opendir('dirname'); $handle 'points' to the directory Read contents of directory –readdir($handle) Returns name of next file in directory Files are sorted as on filesystem Close a directory –closedir($handle) Closes directory 'stream'

PHP Workshop ‹#› Directory Example $handle = opendir('./'); while(false !== ($file=readdir($handle))) { echo "$file "; } closedir($handle);

PHP Workshop ‹#› Directory Example $handle = opendir('./'); while(false !== ($file=readdir($handle))) { echo "$file "; } closedir($handle); Open current directory $handle = opendir('./');

PHP Workshop ‹#› Directory Example $handle = opendir('./'); while(false !== ($file=readdir($handle))) { echo "$file "; } closedir($handle); Whilst readdir() returns a name, loop through directory contents, echoing results while(false !== ($file=readdir($handle))) { echo "$file "; }

PHP Workshop ‹#› Directory Example $handle = opendir('./'); while(false !== ($file=readdir($handle))) { echo "$file "; } closedir($handle); Close the directory stream closedir($handle);

PHP Workshop ‹#› Other Directory Operations Get current directory –getcwd() Change Directory –chdir('dirname'); Create directory –mkdir('dirname'); Delete directory (MUST be empty) –rmdir('dirname'); And more! –

PHP Workshop ‹#› Review Can open and close files. Can read a file line by line or all at one go. Can write to files. Can open and cycle through the files in a directory.