Web Programming 02 – PHP File and Directory Handling Aryo Pinandito, ST, M.MT.

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

PHP Form and File Handling
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.
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.
CS 241 Section Week #5 2/23/12. 2 Topics This Section MP4 overview Function Pointers Pthreads File I/O.
PHP Workshop ‹#› File Handling with PHP. PHP Workshop ‹#› Files and PHP File Handling –Data Storage Though slower than a database –Manipulating uploaded.
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.
File System Interface CSCI 444/544 Operating Systems Fall 2008.
COMP An Introduction to Computer Programming : University of the West Indies COMP6015 An Introduction to Computer Programming Lecture 06.
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
2010/11 : [1]Building Web Applications using MySQL and PHP (W1)Slide Topic Files.
Intermediate PHP (2) File Input/Output & User Defined Functions.
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
INF1204 – Week 4 FILE. Text file input/output overview Printing on the screen The simplest way to produce output is using the print statement where you.
PHP2. PHP Form Handling The PHP $_GET and $_POST variables are used to retrieve information from forms, like user input. Name: Age:
Variables and ConstantstMyn1 Variables and Constants PHP stands for: ”PHP: Hypertext Preprocessor”, and it is a server-side programming language. Special.
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.
Digital Computer Concept and Practice Copyright ©2012 by Jaejin Lee C Language Part 4.
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
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.
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.
Outline if...else...elseif Statements Switch Loops Functions Arrays Forms.
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,
24-2 Perform File I/O using file pointers FILE * data-type Opening and closing files Character Input and Output String Input and Output Related Chapter:
Files Tutor: You will need ….
12 – PHP Contd. Informatics Department Parahyangan Catholic University.
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()”
ITFN 2601 Introduction to Operating Systems Lecture 22 Files & Directories.
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.
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.
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
Silberschatz and Galvin  C Programming Language Kingdom of Saudi Arabia Ministry of Higher Education Al-Majma’ah University College of Education.
CGS 3066: Web Programming and Design Spring 2016
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.
Chapter 7 Files and Exceptions
<?php require("header.htm"); ?>
Beginning C Lecture 11 Lecturer: Dr. Zhao Qinpei
Cmdlets “Command-lets”
Files Handling In today’s lesson we will look at:
Presentation transcript:

Web Programming 02 – PHP File and Directory Handling Aryo Pinandito, ST, M.MT

Files and PHP  File Handling  Data Storage  Though slower than a database  Manipulating uploaded files  From forms  Creating Files for download

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.

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.

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); ?>

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..

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.

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.

Data Reading Example <?php $handle = fopen('people.txt', 'r'); while (!feof($handle)) { echo fgets($handle, 1024); echo ' '; } fclose($handle); ?>

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.

Writing Data  To write data to a file use: fwrite($handle,$data)  Write $data to the file.

Data Writing Example <?php $handle = fopen('people.txt', 'a'); fwrite($handle, “\nFred:Male”); fclose($handle); ?>

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! 

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'

Directory Example <?php $handle = opendir('./'); while(false !== ($file=readdir($handle))) { echo "$file "; } closedir($handle); ?>

Other Directory Operations  Get current directory getcwd()  Change Directory chdir('dirname');  Create directory mkdir('dirname');  Delete directory (MUST be empty) rmdir('dirname');  And more! 

Review File Handling in PHP  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.

Questions?