18. PHP File Operations Opening a File

Slides:



Advertisements
Similar presentations
PHP Form and File Handling
Advertisements

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 Chapter 5 – Handling HTML Controls in Web Pages spring into PHP 5 by Steven Holzner Slides were developed by Jack Davis College of Information Science.
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.
COMP234 Perl Printing Special Quotes File Handling.
Alford Academy Business Education and Computing1 Advanced Higher Computing Based on Heriot-Watt University Scholar Materials File Handling.
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.
Reading and Writing Files Keeping Data. Why do we use files? ä For permanently storing data. ä For dealing with information too large to fit in memory.
IS 1181 IS 118 Introduction to Development Tools Week 2.
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.
Form Handling, Validation and Functions. Form Handling Forms are a graphical user interfaces (GUIs) that enables the interaction between users and servers.
2010/11 : [1]Building Web Applications using MySQL and PHP (W1)Slide Topic Files.
August Chapter 1 - Essential PHP spring into PHP 5 by Steven Holzner Slides were developed by Jack Davis College of Information Science and Technology.
PHP Tutorials 02 Olarik Surinta Management Information System Faculty of Informatics.
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.
ITM 352 © Port,KazmanFile I/O - 1 File I/O in PHP Persistent Data.
1 PHP and MySQL. 2 Topics  Querying Data with PHP  User-Driven Querying  Writing Data with PHP and MySQL PHP and MySQL.
PMS /134/182 HEX 0886B6 PMS /39/80 HEX 5E2750 PMS /168/180 HEX 00A8B4 PMS /190/40 HEX 66CC33 By Adrian Gardener Date 9 July 2012.
Files COP3275 – PROGRAMMING USING C DIEGO J. RIVERA-GUTIERREZ.
PHP1-1 PHP Lecture 2 Xingquan (Hill) Zhu
Mr. Justin “JET” Turner CSCI 3000 – Fall 2015 CRN Section A – TR 9:30-10:45 CRN – Section B – TR 5:30-6:45.
Website Development with PHP and MySQL Saving Data.
1 Chapter 9 – Cookies, Sessions, FTP, and More spring into PHP 5 by Steven Holzner Slides were developed by Jack Davis College of Information Science.
PHP2. PHP Form Handling The PHP $_GET and $_POST variables are used to retrieve information from forms, like user input. Name: Age:
_______________________________________________________________________________________________________________ PHP Bible, 2 nd Edition1  Wiley and the.
CSC 2720 Building Web Applications Server-side Scripting with PHP.
14. Uploading Files to MySQL Database. M. Udin Harun Al Rasyid, S.Kom, Ph.D Desain dan.
Introduction to Programming Using C Files. 2 Contents Files Working with files Sequential files Records.
Java server pages. A JSP file basically contains HTML, but with embedded JSP tags with snippets of Java code inside them. A JSP file basically contains.
An Introduction to Programming with C++ Sixth Edition Chapter 14 Sequential Access Files.
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.
1 File Handling. 2 Storage seen so far All variables stored in memory Problem: the contents of memory are wiped out when the computer is powered off Example:
Liang, Introduction to Programming with C++, Second Edition, (c) 2010 Pearson Education, Inc. All rights reserved Chapter 13 File Input and.
Open Source Server Side Scripting ECA 236 Open Source Server Side Scripting Files & Directories.
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.
CST336, Spring 2015 Week 8: PHP File Upload. PHP provides specific functions to handle binary data such as uploading a file into the server, storing it.
Fall 2002CS 150: Intro. to Computing1 Streams and File I/O (That is, Input/Output) OR How you read data from files and write data to files.
8 Chapter Eight Server-side Scripts. 8 Chapter Objectives Create dynamic Web pages that retrieve and display database data using Active Server Pages Process.
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.
Since you’ll need a place for the user to enter a search query. Every form must have these basic components: – The submission type defined with the method.
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])
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. FILE * u In C, we use a FILE * data type to access files. u FILE * is defined in /usr/include/stdio.h u An example: #include int main() { 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.
FILES AND EXCEPTIONS Topics Introduction to File Input and Output Using Loops to Process Files Processing Records Exceptions.
FILE I/O: Low-level 1. The Big Picture 2 Low-Level, cont. Some files are mixed format that are not readable by high- level functions such as xlsread()
PHP File Handling. Opening a file Fopen(filename,mode) Closing a file Fclose(filename)
Silberschatz and Galvin  C Programming Language Kingdom of Saudi Arabia Ministry of Higher Education Al-Majma’ah University College of Education.
Chapter 22 – part a Stream refer to any source of input or any destination for output. Many small programs, obtain all their input from one stream usually.
DBW - PHP DBW2017.
* Lecture # 7 Instructor: Rida Noor Department of Computer Science
Introduction to Scripting
Arrays and files BIS1523 – Lecture 15.
Intro to PHP & Variables
Files I/O, Streams, I/O Redirection, Reading with fscanf
Topics Introduction to File Input and Output
Beginning C Lecture 11 Lecturer: Dr. Zhao Qinpei
File I/O in C Lecture 7 Narrator: Lecture 7: File I/O in C.
File Input and Output.
Topics Introduction to File Input and Output
Lecture 6: Processing Forms with PHP
Topics Introduction to File Input and Output
PHP-II.
Professor Jodi Neely-Ritz University of Florida
Presentation transcript:

18. PHP File Operations Opening a File Use fopen() to open a file 2 required parameters 1st parameter is the full pathname of the file Can use the PHP built-in variable $_SERVER['DOCUMENT_ROOT'] Points to the base of the document root (www for Apache2) We store the value in a scalar variable: $DOCUMENT_ROOT = $_SERVER['DOCUMENT_ROOT'];

As there are 3 ways to access form data, there are 3 ways to access predefined server variables The above way is preferred Also old-fashioned way, using $DOCUMENT_ROOT directly And the deprecated, longer form $HTTP_SERVER_VARS['DOCUMENT_ROOT'] Which forms we can use depends on our server setup

So 1st parameter to fopen() can be something like $_SERVER[DOCUMENT_ROOT].'/../phpData/orders.txt' Can also use an absolute path, e.g., 'C:/wamp/phpData/orders.txt' Use forward slashes (/) even though they’re executed on Windows On Windows, can also use backslashes (\) But must be doubled (backslash is the escape character) "C:\\wamp\\phpData\\orders.txt"

2nd parameter to fopen() is the file mode It’s a string telling the system how to handle access requests 'r': read only, beginning at the start of the file 'w': write only, beginning at the start of the file If file exists, system deletes its contents Otherwise, system tries to create it 'a': appending new content after existing content If file doesn’t exist, system tries to create it

Variants of these modes where the character is followed by a + All indicate read/write 'r+': read/write, starting at beginning of file Preserve content if the content is only read Overwrite content with writes 'w+': read/write, System clears the content, or creates a new file if specified file doesn’t exist 'a+': read/append, preserving content by writing to end of file 'x': “cautious” write Causes fopen() to fail and return false if the file exists 'x+': cautious read/write

Windows, unlike Unix, distinguishes between binary and text files For portability, should also specify binary content, appending 'b' to the mode For the example given later, append orders to an order file, so use 'ab': fopen($_SERVER[DOCUMENT_ROOT].'/../phpData/orders.txt', 'ab')

Used with all further operations on the file To close this file, use If fopen() succeeds, returns a “resource”, or file pointer, which we store in a variable: $fp = fopen($_SERVER[DOCUMENT_ROOT]. '/../phpData/orders.txt', 'ab'); Used with all further operations on the file To close this file, use fclose( $fp ); Returns true if it succeeds and false otherwise Very unlikely that anything’s amiss with closing a file So we don’t test for this case

If fopen() fails, $fp is set to false—test for that If fopen() fails, PHP adds warnings to the HTML But should handle failure gracefully Use the error suppression operator, @: $fp = @ fopen($_SERVER[DOCUMENT_ROOT].'/../phpData/orders.txt', 'ab'); If fopen() fails, $fp is set to false—test for that Can write this as @ $fp = fopen($_SERVER[DOCUMENT_ROOT].'/../phpData/orders.txt', Preferred since @ stands out

If fopen() fails, exit from the script after outputting a paragraph and closing tags: if ( ! $fp ) { echo "<p>Your order cannot be processed now.</p>\n" ."</body>\n<html>"; exit; } Can exploit short-circuit evaluation of or so that exit() with a message is executed if fopen() fails and so returns false: @ $fp = fopen($_SERVER[DOCUMENT_ROOT].'/../phpData/orders.txt', 'ab'); or exit("<p>Your order cannot be processed now.</p>" ."</body></html>");

Writing to a File Most common way to write to a file: fwrite( File, Data, Length ) File is a file pointer Data is string to be written to end of file Length is max number of bytes to write 1st 2 parameters required, 3rd optional If Length is supplied, Data is written to file until its end or Length bytes output Usually include 3rd parameter when writing in binary mode Helps avoid cross-platform compatibility issues

To output $outstring to $fp, can use Can use built-in function strlen() to get length (in bytes) of a string To output $outstring to $fp, can use fwrite( $fp, $outstring, strlen( $outstring ) ); Function fputs() is an alias of fwrite()

An alternative to fwrite() is file_put_contents( Path , Data ) Path is full pathname of the file Data is as before Invoking this function is equivalent to invoking $fp = fopen( FileName, 'w' ); fwrite( $fp, Data ); fclose( $fp ); To get effect of 'a' instead of 'w', include as a 3rd parameter constant FILE_APPEND This returns number of bytes written to file or false if it fails

Example Take 1st example from last section of previous part Have the form include text box for client’s name For each order, client’s name date the order was placed number of each item ordered amount due (including tax) are written as a line to a file orders.txt For security reasons, it’s in a folder not under document root: $DOCUMENT_ROOT/../phpData/orders.txt

Recall: Name of the text box for number of quarts of milk is milkqty Name of the text box for number of bottles of beer is beerqty Corresponding program variables are $milkqty and $beerqty Name of the new text box is name Corresponding program variable is $name Add in 1st block of code the statement $name = $_POST['name']; No change to HTML output by the PHP script Just add required file processing at the end

Format of the line written to file: It must terminate it with a \n Generally separate fields with tabs, \t Normally doesn’t occur in input Using a special field separator lets us split the data back into separate variables See later, arrays and string processing The output string: $name."\t" .$date."\t" .$milkqty." quarts of milk\t" .$beerqty." bottles of beer\t\$" .$totalamount."\n";

Listing of the new code: $outstring = $name."\t" .$date."\t" .$milkqty." quarts of milk\t" .$beerqty." bottles of beer\t\$" .$totalamount."\n"; @ $fp = fopen($_SERVER[DOCUMENT_ROOT].'/../phpData/orders.txt', 'ab'); if ( ! $fp ) { echo "<p>Your order cannot be processed now.</p>\n" ."</body>\n<html>"; exit; } fwrite( $fp, $outstring, strlen($outstring) ); fclose( $fp );

<p>Order written.</p> To confirm that order has been recorded, add a paragraph just before end of the body. <p>Order written.</p> If we exit because the file can’t be opened, this won’t be part of the HTML After a couple of orders, orders.txt file is Al 17:49, 21st October 1 quarts of milk 6 bottles of beer $20.08 Ed 17:50, 21st October 6 quarts of milk 1 bottles of beer $24.20

Reading from a File Open same file again, now for reading Invoke fopen() with mode 'rb': @ $fp = fopen($_SERVER[DOCUMENT_ROOT]. '/../phpData/orders.txt', 'rb'); Trying to open a non-existent file not a problem for writing System should create the file But is a problem if the system can’t create the file E.g., don’t have the required permissions

File has one order per line Want to look at the orders one by one Use function specifically designed for reading a line at a time: fgets( File, Bytes ) Reads from the file associated with file pointer File until it encounters a newline \n, reaches the end of file (EOF), or has read Bytes – 1 bytes

System maintains a current position pointer into a file where next read or write operation will happen A read or write advances this pointer fgets() advances the current position pointer to the position after the last character read (or to EOF) Returns the string read or, if it fails, false

To use fgets(), put it in a loop that repeats as long as we haven’t reached EOF feof( File ) returns true if the file associated with file pointer File is at EOF To output contents of the orders file a line at a time in an HTML document, use a loop like while ( ! feof( $fp ) ) { $order = fgets( $fp, 999 ); echo $order.'<br>'; }

Next slide is a listing of entire PHP file for producing an HTML document listing the orders For file contents shown above, the browser rendering is:

Continued <?php $DOCUMENT_ROOT = $_SERVER['DOCUMENT_ROOT']; ?> <html> <head> <title>Al's Grocery - Customer Orders</title> </head> <body> <h1>Al's Grocery</h1> <h2>Customer Orders</h2> @ $fp = fopen($_SERVER[DOCUMENT_ROOT]. '/../phpData/orders.txt', 'rb'); if ( ! $fp ) { echo "<p>No pending orders.</p>\n</body>\n</html>"; exit; } Continued

while ( ! feof( $fp ) ) { $order = fgets( $fp, 999 ); echo $order.'<br>'; } fclose( $fp ); ?> </body> </html>

Reading and writing a character at a time not covered Rather than reading the file a line at a time, can read it all at once Reading and writing a character at a time not covered Reading arrays and fields within lines—later Function readfile( Path ) opens file with path Path, echoes its content to the HTML document, closes the file Returns number of bytes read or, if it fails, false.

could replace all the above code (except error-handling) with If we don’t care about running lines together when the HTML document is rendered, could replace all the above code (except error-handling) with readfile( "$_SERVER[DOCUMENT_ROOT].'/../phpData/orders.txt' );

Other functions for reading entire file at once fpassthru( File ) takes a file pointer File (unlike with readfile(), must have already opened the file) dumps to standard output the file’s content from current position to the end, closes the file Returns true if successful and otherwise false

Function file_get_contents( Path ) is like readfile() But returns file contents as a string

PHP read functions are generally binary-safe Treat their input as a raw stream of data with no specific format Should work with all 256 possible character values In contrast, most functions that recognize special characters (e.g., escape codes) or expect null-terminated strings aren’t binary safe Binary safe functions required when working with data of unknown format E.g., arbitrary files and encrypted data

Other File Functions Function file_exists( Path ) given the path for a file, returns true if the file exists and false if not E.g., used to avoid trying to open non-existent file

Really a string function Takes a string argument nl2br() Really a string function Takes a string argument Returns a string with \n characters replaced by <br> tags Can replace all code producing HTML that lists the orders with echo nl2br( file_get_contents( $_SERVER[DOCUMENT_ROOT].'/../phpData/orders.txt' ) ); Each order is on a separate, as in the original

To delete a file: unlink( Path ) where Path is the path to the file Returns true if successful, false otherwise Fails if, e.g., wrong permissions or file doesn’t exist PHP has several functions to position the current position pointer in a file Allows random access Not covered

Uploading Files Value "file" for the type attribute of an input form element is for uploading files to the server Rendered, it provides a ‘Browse’ button When clicked, opens a window to browse the files on the client’s system to select the file to upload This input type requires that the POST method be used and requires some accompanying information in the form

The accompanying info in the form: The form element must have the attribute-value pair enctype="multipart/form-data" Before the input element with type="file", need <input type="hidden" name="MAX_FILE_SIZE" value="1000000"> The name value is mandatory The value value (in bytes) can be anything reasonable

Example HTML Document Rendered as <html> <head> <title>Upload New Files</title> </head> <body> <h1>Upload new news files</h1> <form enctype="multipart/form-data" action="upload.php" method="post"> <input type="hidden" name="MAX_FILE_SIZE" value="1000000"> Upload this file: <input name="userfile" type="file"> <input type="submit" value="Send File"> </form> </body> </html> Rendered as

PHP to Handle a File Upload The PHP script fielding this request accesses the global $_FILES Contains all the info on the uploaded file Assume the value of the name attribute of the file input element is "userfile" (as above), we have $_FILES['userfile']['name'] the original name of the file on the client machine $_FILES['userfile']['type'] the mime type of the file (e.g., "text/plain" for a .txt file) $_FILES['userfile']['size'] the size, in bytes, of the uploaded file $_FILES['userfile']['tmp_name'] temporary filename for where uploaded file stored on the server $_FILES['userfile']['error'] the error code associated with this file upload.

By default, uploaded files are stored in the server's default temporary directory unless another location given with the upload_tmp_dir directive in php.ini The server’s default directory can be changed by setting environment variable TMPDIR The default temporary folder for my WampServer is C:\wamp\tmp\ File upload.php (below) echoes the info in $_FILES['userfile'] Main thing it does: copy the uploaded file from the temporary folder to its intended location Intended folder is C:\uploads\ We retain the name of the file itself

"C:" can be omitted—my document root is on that disk The PHP code sets variable $upfile to the full pathname for the file in its intended location $upfile = '/uploads/'.$_FILES['userfile']['name']; "C:" can be omitted—my document root is on that disk To move the file from the temporary folder to the new location, use move_uploaded_file( filename, destination ) Copies uploaded file with name filename to destination, a full pathname In our example, filename is $_FILES['userfile']['tmp_name'] Before moving the file, checks that the file named filename is a valid upload file (uploaded via POST) Returns True on successfully moving the file

File upload.php <html> <head> <title>Uploading ...</title> </head> <body> <h3>Uploading the file</h3> <?php // Put the file where we'd like it $upfile = '/uploads/'.$_FILES['userfile']['name']; if ( ! move_uploaded_file( $_FILES['userfile']['tmp_name'], $upfile ) ) { echo 'Problem: Could not move file to desitination folder'; exit; } echo "<p>"; echo "Temporary name: {$_FILES['userfile']['tmp_name']}<br />"; echo "Name: {$_FILES['userfile']['name']}<br />"; echo "Size: {$_FILES['userfile']['size']}<br />"; echo "Type: {$_FILES['userfile']['type']}<br />"; echo "Error: {$_FILES['userfile']['error']}<br />"; echo "</p>"; ?> </body> </html>

For more information on uploading files with PHP, see the PHP Manual, “Handling file uploads”: http://us3.php.net/manual/en/features.file-upload.php