advanced web 2012 lecture 6 sean costain 2012. files sean costain 2012 php allows for the : creation...

20
Advanced Web 2012 Lecture 6 Sean Costain 2012

Upload: elvin-mclaughlin

Post on 13-Dec-2015

217 views

Category:

Documents


0 download

TRANSCRIPT

Page 1: Advanced Web 2012 Lecture 6 Sean Costain 2012. Files Sean Costain 2012 Php allows for the : Creation Reading Appending Deleting Uploading And Closing

Sean Costain 2012

Advanced Web2012

Lecture 6

Page 2: Advanced Web 2012 Lecture 6 Sean Costain 2012. Files Sean Costain 2012 Php allows for the : Creation Reading Appending Deleting Uploading And Closing

Sean Costain 2012

FilesPhp allows for the :• Creation• Reading• Appending• Deleting• Uploading• And ClosingOf files.

Page 3: Advanced Web 2012 Lecture 6 Sean Costain 2012. Files Sean Costain 2012 Php allows for the : Creation Reading Appending Deleting Uploading And Closing

Sean Costain 2012

Create / Open a fileSame command Creates or Opens a file.

Opens or Creates the file

Critical when dealing with files, always, always close the file once you have finished working with it.

Page 4: Advanced Web 2012 Lecture 6 Sean Costain 2012. Files Sean Costain 2012 Php allows for the : Creation Reading Appending Deleting Uploading And Closing

Sean Costain 2012

fopen tagsAs you may have noticed in the last slide, fopen was followed by a ‘w’ there are 6 methods of tagging the fopen command for file manipulation:• ‘r’ : File is read only• ‘w’ : File is open for writing, if the file exists overwrites all data and

prepares to write data at the start of the file.• ‘a’ : Appends data to the end of the file, doesn’t damage any of the current

content.• ‘r+’ : Reads the file and allows data to be written at the start of the file• ‘w+’ : Deletes the contents of the file and allows data to be written to it• ‘a+’ : Reads the file and allows data to be added to the file, at the end of

the file position.

This is the part that gets changed

Page 5: Advanced Web 2012 Lecture 6 Sean Costain 2012. Files Sean Costain 2012 Php allows for the : Creation Reading Appending Deleting Uploading And Closing

Sean Costain 2012

fclose

Technically, we don’t have to close the files, but it is good programming practice to always close them. PHP is smart enough to do it after finishing the execution, but if you program a close, you won’t cause any ‘accidental’ memory issues.

Closing the fileProcess

Page 6: Advanced Web 2012 Lecture 6 Sean Costain 2012. Files Sean Costain 2012 Php allows for the : Creation Reading Appending Deleting Uploading And Closing

Sean Costain 2012

Writing to a fileThere are a couple of ways to write to a file, the first is just a straight write, which will decimate any content in a file. The second is to append data to the end of a file, there by, not overwriting any previous data. And finally, there is the Read/Write option, which appends data to the start of the file.

First things first, the file must be opened

Open for writing, deletes any data

Open for writing, appends data to the end of the file

Open for writing, writes data to the start of the file

Page 7: Advanced Web 2012 Lecture 6 Sean Costain 2012. Files Sean Costain 2012 Php allows for the : Creation Reading Appending Deleting Uploading And Closing

Sean Costain 2012

Writing Cont.

Creates the file testFile.txt

Simulates user entry

Writes the data into the file

This creates a blank file and writes data to it.

Page 8: Advanced Web 2012 Lecture 6 Sean Costain 2012. Files Sean Costain 2012 Php allows for the : Creation Reading Appending Deleting Uploading And Closing

Sean Costain 2012

Writing Cont.

This creates a blank file and writes data to it.

The test file looks like this

Page 9: Advanced Web 2012 Lecture 6 Sean Costain 2012. Files Sean Costain 2012 Php allows for the : Creation Reading Appending Deleting Uploading And Closing

Sean Costain 2012

Writing Cont.

Opens the file testFile.txt

Simulates user entry

Writes the data into the file

This appends data to the end of a pre-existing file.

Page 10: Advanced Web 2012 Lecture 6 Sean Costain 2012. Files Sean Costain 2012 Php allows for the : Creation Reading Appending Deleting Uploading And Closing

Sean Costain 2012

Writing Cont.

This appends the data to the end of the existing file.

The test file looks like this

Page 11: Advanced Web 2012 Lecture 6 Sean Costain 2012. Files Sean Costain 2012 Php allows for the : Creation Reading Appending Deleting Uploading And Closing

Sean Costain 2012

Writing Cont.

Opens the file testFile.txt

Simulates user entry

Writes the data into the file

This appends data to the start of a pre-existing file.

Page 12: Advanced Web 2012 Lecture 6 Sean Costain 2012. Files Sean Costain 2012 Php allows for the : Creation Reading Appending Deleting Uploading And Closing

Sean Costain 2012

Writing Cont.

This appends the data to the start of the existing file.

The test file looks like this

Page 13: Advanced Web 2012 Lecture 6 Sean Costain 2012. Files Sean Costain 2012 Php allows for the : Creation Reading Appending Deleting Uploading And Closing

Sean Costain 2012

Reading the FileReading the file is straight forward, you open it, stream the data into a variable and then echo that data to the screen. There are two methods, fread and fgets. Fread grabs the file and doesn’t perform any processing on it, fgets, on the other hand, recognizes items like the \n for newline.

freads

fgets

Page 14: Advanced Web 2012 Lecture 6 Sean Costain 2012. Files Sean Costain 2012 Php allows for the : Creation Reading Appending Deleting Uploading And Closing

Sean Costain 2012

Deleting a fileWhen deleting a file, you need to ensure that it is closed first. You can have an fclose listed on the file before you run the delete, but if the file isn’t already open you will get a warning message. This message can be hidden by using the code error_reporting(0);

Always make sure it is the correct file you delete

Delete the file using unlink

Page 15: Advanced Web 2012 Lecture 6 Sean Costain 2012. Files Sean Costain 2012 Php allows for the : Creation Reading Appending Deleting Uploading And Closing

Sean Costain 2012

Uploading FilesUploading a file is one part of the process, you need to be able to save it somewhere.

The standard process is that any uploaded files get placed into a temporary folder to be deleted later on, so when you do upload a file into the temporary location, it is useful to move it to a more secure location.

This other location can be either in a database; think BLOB or as the file itself. Where you can store the data needed to reference it in a database.

Page 16: Advanced Web 2012 Lecture 6 Sean Costain 2012. Files Sean Costain 2012 Php allows for the : Creation Reading Appending Deleting Uploading And Closing

Sean Costain 2012

Uploading Files Cont.Self feeding form

The enctype attribute of the <form> 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 uploadedThe type="file" attribute of the <input> tag specifies that the input should be processed as a file.

Page 17: Advanced Web 2012 Lecture 6 Sean Costain 2012. Files Sean Costain 2012 Php allows for the : Creation Reading Appending Deleting Uploading And Closing

Sean Costain 2012

Uploading Files Cont.

Moves the file from the temporary location to the one you have set up.

The first parameter is the form's input name and the second index can be either "name", "type", "size", "tmp_name" or "error". Like this:• $_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

Page 18: Advanced Web 2012 Lecture 6 Sean Costain 2012. Files Sean Costain 2012 Php allows for the : Creation Reading Appending Deleting Uploading And Closing

Sean Costain 2012

Uploading Files Cont.

This is the information you store in the database along with the raw data as a blob type.

For your assessment, if you are uploading image files, think of storing it as files with the link data being stored in the database.

Page 19: Advanced Web 2012 Lecture 6 Sean Costain 2012. Files Sean Costain 2012 Php allows for the : Creation Reading Appending Deleting Uploading And Closing

Sean Costain 2012

Login for Websites

You’ve all seen them, login forms. So what’s the process?

Gather the information and then compare it to the data stored in the database

Page 20: Advanced Web 2012 Lecture 6 Sean Costain 2012. Files Sean Costain 2012 Php allows for the : Creation Reading Appending Deleting Uploading And Closing

Sean Costain 2012

Login Cont.So once you have confirmed it is real, then what?

The easiest way to ensure that a user is logged in and stays logged in, is to use SESSION variables as shown last week. These variables last only as long as the user has the browser open and the user hasn’t reset the session id by destroying the session.

A simple IF..THEN on each ‘secure’ page can then check if the session is valid, if it is, then the user can look at the content of the page, otherwise, they can be redirected back to the login page.