PHP File Handling. Opening a file Fopen(filename,mode) Closing a file Fclose(filename)

Slides:



Advertisements
Similar presentations
PHP Form and File Handling
Advertisements

PHP File Upload ISYS 475.
V 1.0 OE NIK 2013 PHP+SQL 4. File handling basics File-based "database" File-based guestbook 1.
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.
More forms CS Reset Buttons  specify custom text on the button by setting its value attribute CS380 2 Name: Food: Meat? HTML.
More on PHP Coding Lab no. 6 Advance Database Management System.
Chapter 4 Working with Files and Directories PHP Programming.
Faculty of Sciences and Social Sciences HOPE PHP – Working with Input Stewart Blakeway FML 213
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.
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.
Outline Overview about Web Page HTML Form Creation FORM Input INPUT control types GET & POST PHP File Upload PHP Include Files Headers Cookie Sessions.
Uploading Files. Why? By giving a user the option to upload a file you are creating an interactive page You can enable users have a greater web experience.
2010/11 : [1]Building Web Applications using MySQL and PHP (W1)Slide Topic Files.
Web Programming Week 10 Old Dominion University Department of Computer Science CS 418/518 Fall 2010 Martin Klein 11/02/10.
U NIT 4 F ILE U PLOAD. I. C REATE AN U PLOAD -F ILE F ORM - With PHP, it is possible to upload files to the server.To allow users to upload files from.
Kyle MacLachlan.  Used To Format a Date/Time  Syntax: › date(format,timestamp)  format: Required, specifies format  timestamp:Optional, specifies.
04/09/20151 PHP & MySQL 'Slide materials are based on W3Schools PHP tutorial, 'PHP website 'MySQL website.
PHP Tutorials 02 Olarik Surinta Management Information System Faculty of Informatics.
PHP Basics 2 ICS213, 1 / 2011 Dr. Seung Hwan Kang 1.
PHP Tutorial By RAY BALIKI && ROY GHORAYEB. DEFINITION PHP is a powerful server-side scripting language for creating dynamic and interactive websites.
1 HTML and CGI Scripting CSC8304 – Computing Environments for Bioinformatics - Lecture 10.
CSC 2720 Building Web Applications Getting and Setting HTTP Headers (With PHP Examples)
PHP3. PHP File Upload With PHP, it is possible to upload files to the server. Create an Upload-File Form To allow users to upload files from a form can.
ITCS373: Internet Technology Server-Side Programming PHP – Part 2 Dr. Faisal Al-Qaed.
PHP Advance. Agenda Server side Includes File Handling Cookies Sessions Error/Exception handling Database handling with MySQL sending.
Creating Databases Uploading Files. Reading & writing files. Homework: Starting planning ‘original’ project.
Creating Dynamic Web Pages Using PHP and MySQL CS 320.
18. PHP File Operations Opening a File
Website Development with PHP and MySQL Saving Data.
PHP2. PHP Form Handling The PHP $_GET and $_POST variables are used to retrieve information from forms, like user input. Name: Age:
14. Uploading Files to MySQL Database. M. Udin Harun Al Rasyid, S.Kom, Ph.D Desain dan.
File IO and command line input CSE 2451 Rong Shi.
Creating databases for web applications Library. New example: student database. Homework: Complete class example. Catch up on source postings. Do creation.
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.
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.
Forms Collecting Data CSS Class 5. Forms Create a form Add text box Add labels Add check boxes and radio buttons Build a drop-down list Group drop-down.
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.
1 HTML forms (cont.)
12 – PHP Contd. Informatics Department Parahyangan Catholic University.
HTML Forms.
Web Scripting [PHP] CIS166AE Wednesdays 6:00pm – 9:50pm Rob Loy.
GAME203 – C Files stdio.h C standard Input/Output “getchar()”
Company LOGO In the Name of Allah,The Most Gracious, The Most Merciful King Khalid University College of Computer and Information System Web pages Development.
Web Page Designing With Dreamweaver MX\Session 1\1 of 9 Session 3 PHP Advanced.
Dr. Abdullah Almutairi Spring PHP is a server scripting language, and a powerful tool for making dynamic and interactive Web pages. PHP is a widely-used,
FILE UPLOAD.
Media Access online media sharing ECE 3553 Multifarious Systems Cary Fung December 7, 2007.
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.
Generating XML Data from a Database Eugenia Fernandez IUPUI.
Unit 7 How to Upload Files. A very useful aspect of PHP is its ability to manage file uploads to your server. Before you can use PHP to manage your uploads,
PHP. What is PHP? PHP stands for PHP: Hypertext Preprocessor PHP is a server-side scripting language, like ASP PHP scripts are executed on the server.
Web Engineering Lecture-08.
HTTP – An overview.
>> PHP: File Uploads
Chapter 7 Text Input/Output Objectives
Lecture 19: Forms and uploading files
Advanced PHP Lecture by Nutthapat Keawrattanapat
Handling Files In particular, uploading files.
Online Translation Service Capstone Design
Handling Files In particular, uploading files.
Presentation transcript:

PHP File Handling

Opening a file Fopen(filename,mode) Closing a file Fclose(filename)

ModesDescription rRead only. Starts at the beginning of the file r+Read/Write. Starts at the beginning of the file wWrite only. Opens and clears the contents of file; or creates a new file if it doesn't exist w+Read/Write. Opens and clears the contents of file; or creates a new file if it doesn't exist aAppend. Opens and writes to the end of the file or creates a new file if it doesn't exist a+Read/Append. Preserves file content by writing to the end of the file xWrite only. Creates a new file. Returns FALSE and an error if file already exists x+Read/Write. Creates a new file. Returns FALSE and an error if file already exists

Check End-of-file The feof() function checks if the "end-of-file" (EOF) has been reached. The feof() function is useful for looping through data of unknown length. Note: You cannot read from files opened in w, a, and x mode!

Reading a File Line by Line The fgets() function is used to read a single line from a file. Note: After a call to this function the file pointer has moved to the next line. $file = fopen("welcome.txt", "r") or exit("Unable to open file!"); //Output a line of the file until the end is reached while(!feof($file)) { echo fgets($file). " "; } fclose($file);

Reading a File Character by Character The fgetc() function is used to read a single character from a file. Note: After a call to this function the file pointer moves to the next character. echo fgetc($file);

PHP File Upload Create an Upload-File Form To allow users to upload files from a form can be very useful. enctype="multipart/form-data” The enctype attribute of the tag specifies which content-type to use when submitting the form. "multipart/form-data" is used when a form requires binary data, like the contents of a file, to be uploaded The type="file" attribute of the tag specifies that the input should be processed as a file. For example, when viewed in a browser, there will be a browse-button next to the input field

Create The Upload Script if ($_FILES["file"]["error"] > 0) { echo "Error: ". $_FILES["file"]["error"]. " "; } else { echo "Upload: ". $_FILES["file"]["name"]. " "; echo "Type: ". $_FILES["file"]["type"]. " "; echo "Size: ". ($_FILES["file"]["size"] / 1024). " Kb "; echo "Stored in: ". $_FILES["file"]["tmp_name"]; }

Global PHP $_FILES Array $_FILES["file"]["name"] - the name of the uploaded file $_FILES["file"]["type"] - the type of the uploaded file $_FILES["file"]["size"] - the size in bytes of the uploaded file $_FILES["file"]["tmp_name"] - the name of the temporary copy of the file stored on the server $_FILES["file"]["error"] - the error code resulting from the file upload

Restrictions on Upload In this script we add some restrictions to the file upload. The user may only upload.gif or.jpeg files and the file size must be under 20 kb: if ((($_FILES["file"]["type"] == "image/gif") || ($_FILES["file"]["type"] == "image/jpeg") || ($_FILES["file"]["type"] == "image/pjpeg")) && ($_FILES["file"]["size"] < 20000))

Saving the Uploaded File if (file_exists("C:\Users\ARSIL\Downloads/". $_FILES["file"]["name"])) { echo $_FILES["file"]["name"]. " already exists. "; } else { move_uploaded_file($_FILES["file"]["tmp_name"], "C:\Users\ARSIL\Downloads/". $_FILES["file"]["name"]); echo "Stored in: ". "C:\Users\ARSIL\Downloads/". $_FILES["file"]["name"]; }

Category MIME type Description DOCUMENTS application/msword Microsoft Word document application/pdf PDF document text/plain Plain text text/rtfRich text format IMAGES image/gifGIF format image/jpeg JPEG format (includes.jpg files) image/pjpegJPEG format (nonstandard MIME type used by Internet Explorer) image/png PNG format image/tiff TIFF format

Working on PDF file format $file="C:\Users\ARSIL\Downloads/{$_FILES['file'] ['name']}"; header('Content-type: application/pdf'); header('Content-Disposition: inline'); readfile($file);