FILE UPLOAD.

Slides:



Advertisements
Similar presentations
JQuery MessageBoard. Lets use jQuery and AJAX in combination with a database to update and retrieve information without refreshing the page. Here we will.
Advertisements

PHP File Upload ISYS 475.
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.
Video, audio, embed, iframe, HTML Form
FILE UPLOAD.
CSC 2720 Building Web Applications PHP File Upload.
A simple PHP application We are going to develop a simple PHP application with a Web interface. The user enters two numbers and the application returns.
HTML Form Processing Learning Web Design – Chapter 9, pp Squirrel Book – Chapter 11, pp
How the web works: HTTP and CGI explained
1 Web Search Interfaces. 2 Web Search Interface Web search engines of course need a web-based interface. Search page must accept a query string and submit.
HTTP Overview Vijayan Sugumaran School of Business Administration Oakland University.
CGI Programming: Part 1. What is CGI? CGI = Common Gateway Interface Provides a standardized way for web browsers to: –Call programs on a server. –Pass.
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.
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.
File uploading in PHP Basharat Mahmood, Department of Computer Science,CIIT,Islamabad, Pakistan. 1.
Forms and PHP Dr. Charles Severance
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.
Advance Database Management Systems Lab no. 5 PHP Web Pages.
1 Chapter 6 – Creating Web Forms and Validating User Input spring into PHP 5 by Steven Holzner Slides were developed by Jack Davis College of Information.
ACM Web Development Workshop - PHP By Luis Torres.
1Computer Sciences Department Princess Nourah bint Abdulrahman University.
Web Application Architecture and Communication. Displaying a Web page in a Browser
1 HTML and CGI Scripting CSC8304 – Computing Environments for Bioinformatics - Lecture 10.
Server-side Scripting Powering the webs favourite services.
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 Basic Perl CGI Programming. 2 Issues How and when your program is invoked. Generating Response –HTTP Headers –HTML (or whatever document type you want)
HTML, PHP, and MySQL: Putting It All Together. Making a Form Input tags Types: “text” “radio” “checkboxes” “submit”
CSC 2720 Building Web Applications Server-side Scripting with PHP.
HTML Form and PHP IST Review of Previous Class HTML table and PHP array Winner is chosen randomly using rand() function.
14. Uploading Files to MySQL Database. M. Udin Harun Al Rasyid, S.Kom, Ph.D Desain dan.
Form Data Encoding GET – URL encoded POST – URL encoded
Google Application Engine Introduction Jim Eng with thanks to Charles Severance
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.
WEB SERVER Mark Kimmet Shana Blair. The Project Web Server Application  Receives request for web pages or images from a client browser via the internet.
Jan 2001C.Watters1 World Wide Web and E-Commerce Client Side Processing.
Web Technologies Lecture 3 Web forms. HTML5 forms A component of a webpage that has form controls – Text fields – Buttons – Checkboxes – Range controls.
Web Server Design Week 13 Old Dominion University Department of Computer Science CS 495/595 Spring 2010 Martin Klein 4/7/10.
ICM – API Server & Forms Gary Ratcliffe.
Netprog CGI and Forms1 CGI and Forms A detailed look at HTML forms.
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.
Overview of Servlets and JSP
CSC 2720 Building Web Applications File Upload using Apache Commons – FileUpload.
Forms and PHP Dr. Charles Severance
DATABASES.
HTML FORM AND PHP IST 210: Organization of Data IST210 1.
COOKIES AND SESSIONS.
LOGIN FORMS.
Uploading in PHP CPTE 212 2/24/2015 John Beckett.
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.
Web Programming Week 1 Old Dominion University Department of Computer Science CS 418/518 Fall 2007 Michael L. Nelson 8/27/07.
Introduction to Web Site Development Department of Computer Science California State University, Los Angeles Lecture 8: Forms and Controls.
CACHING TO IMPROVE PERFORMANCE
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 File Handling. Opening a file Fopen(filename,mode) Closing a file Fclose(filename)
Web Engineering Lecture-08.
Allowing File Uploads.
Storing Images Connect to the server using the correct username and password. $conn = mysql_connect(“yourserver”, “joeuser”, “yourpass”); Create the database.
File Upload in ASP.NET Ivaylo Kenov Technical Assistant
Intro to PHP & Variables
Tutorial (4): HTTP Copyright © The McGraw-Hill Companies, Inc. Permission required for reproduction or display.
Cookies BIS1523 – Lecture 23.
Handling Files In particular, uploading files.
Cookies and sessions Saturday, February 23, 2019Saturday, February 23,
Handling Files In particular, uploading files.
Mr. Justin “JET” Turner CSCI 3000 – Fall 2016 Section DA MW 4:05-5:20
Allowing File Uploads.
Presentation transcript:

FILE UPLOAD

Overview of file upload File upload is where a file is copied from the client to the server – Useful for uploading Images PDFs Videos Audio Pretty much anything that can't be copied-and-pasted into a TEXTAREA

Process of file upload Browser Server Storage location Fill form with input type=file Encode & upload Access file Store file some place safe, such as on the file sys or in a db

Setting up the form correctly <form method="post" action="filetest.php" enctype="multipart/form-data"> Choose file:

Receiving the file on the server side <?php $dir = "mydirectory"; if ($_SERVER['REQUEST_METHOD'] == "POST") { $errorinfo = $_FILES["myfile"]["error"]; $filename = $_FILES["myfile"]["name"]; $tmpfile = $_FILES["myfile"]["tmp_name"]; $filesize = $_FILES["myfile"]["size"]; $filetype = $_FILES["myfile"]["type"]; if ($filetype == "image/jpeg" && $filesize < ) { move_uploaded_file($tmpfile, "$dir/". $filename); chmod("$dir/". $filename, 0644); } else echo "Only jpegs under 1MB are invited to this party."; } $jpgs = glob("$dir/*.*"); sort($jpgs); echo " "; foreach($jpgs as $jpg) { echo " ". htmlspecialchars(substr($jpg, strlen($dir)+1)). " "; } echo " "; ?>

Checking that the file is legit Be sure to validate the inputs – Otherwise, people could be uploading stuff that you really don't want on your server Such as.exe,.dll or.so files containing viruses Or enormous files that waste your server space and maybe your bandwidth – Or they could be uploading bad data that will break your web app when you use data later doesn't look too good

Storing a file in the database A file is an array of bytes, so storing it in the database is very similar to storing a string But you need to declare the column as a blob – – mediumblob is usually good (around 16MB)

Lengthy example Part 1 – Storing files away if ($_SERVER['REQUEST_METHOD'] == "POST") { $errorinfo = $_FILES["myfile"]["error"]; $filename = $_FILES["myfile"]["name"]; $tmpfile = $_FILES["myfile"]["tmp_name"]; $filesize = $_FILES["myfile"]["size"]; $filetype = $_FILES["myfile"]["type"]; $mysqli->query('create table if not exists myuploads(fid integer not null auto_increment, filename varchar(256), filedata mediumblob, primary key(fid))'); if ($filetype == "image/jpeg" && $filesize < ) { $filedata = file_get_contents($tmpfile); $query = $mysqli->prepare("insert into myuploads(filename, filedata) values (?,?)"); $empty = NULL; $query->bind_param("sb", $filename, $empty); $query->send_long_data(1, $filedata); $query->execute(); } else { echo "Only jpegs under 1MB are invited to this party."; }

Lengthy example Part 2 – Listing the files if (!isset($_GET['id'])) { print(' Choose file: '); if ($result = $mysqli->query("select fid, filename from myuploads")) { echo " img {max-height: 1in;} "; echo " Your files... "; while ($obj = $result->fetch_object()) echo " <a target=n href='filetest.php?id=". htmlspecialchars($obj->fid). "'><img src='filetest.php?id=". htmlspecialchars($obj->fid). "'>". " ". htmlspecialchars($obj->filename). " "; echo " "; $result->close(); } } else { $fid = $_GET['id']; if ($fid <= 0) echo ""; else if (!preg_match('/^[0-9]+$/', $fid)) echo "Invalid fid"; else { header('Content-type: image/jpeg'); if ($result = $mysqli->query("select filedata from myuploads where fid = $fid")) { if ($obj = $result->fetch_object()) echo $obj->filedata; $result->close(); }

Let's dig into what is really happening File upload differs from a typical http POST in the way that data sent to data are encoded – Differences in the "content type" – Differences in how the content is represented And also when the server sends data back – Differences in the content type

Example of a simple GET request GET /list.php?category=apple HTTP/1.1 Host: User-Agent: Safari/4.0

Example of a simple POST operation POST /login.php HTTP/1.1 Host: User-Agent: Safari/4.0 Content-Length: 26 Content-Type: application/x-www-form-urlencoded usernm=cs&password=mypass

Example of a simple POST file upload POST /filehandler.php HTTP/1.0 Host: User-Agent: Safari/4.0 Content-Type: multipart/form-data; boundary=BbC15x --BbC15x Content-Disposition: form-data; name="someregularparameter" OSU --BbC15x Content-Disposition: form-data; name="files" Content-Type: multipart/mixed; boundary=CcD15y --CcD15y Content-Disposition: file; filename="somefile.jpeg" Content-Type: image/jpeg dGhlIHRlbnVyZSBzeXN0ZW0gaXMgcmVhbGx5IGphY2tlZA== --CcD15y Content-Disposition: file; filename="anotherfile.gif" Content-Type: image/gif Content-Transfer-Encoding: base64 dGVhY2hpbmcgaXMgdW5kZXJyYXRlZA== --CcD15y-- --BbC15x--

Content type (MIME type) tells how to interpret data As some sort of text – text/plain, text/html, text/javascript, text/css As some sort of image – image/jpeg, image/gif, image/png As some sort of multi-part package – multipart/form-data; boundary=BbC15x For others, see

Detailed breakdown of file upload Browser Web server Storage location Fill form with input type=file Multipart encode; upload Read content type; Decode upload; Store files to temp Your PHP program Pass data to your PHP Store data to some safe place

Detailed breakdown of sending data back Browser Web server Storage location Click a link GET with parameter Read parameters Your PHP program Pass data to your PHP Retrieve requested data Pass content type & data Interpret data Show to user

Making file upload look slick For a great "How-To" topic, search the web for one of the many slick AJAX file upload libraries – HINT: The file can be sent via AJAX; full page refresh isn't needed!