Presentation is loading. Please wait.

Presentation is loading. Please wait.

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

Similar presentations


Presentation on theme: "Advanced Web 2012 Lecture 6 Sean Costain 2012. Files Sean Costain 2012 Php allows for the : Creation Reading Appending Deleting Uploading And Closing."— Presentation transcript:

1 Advanced Web 2012 Lecture 6 Sean Costain 2012

2 Files Sean Costain 2012 Php allows for the : Creation Reading Appending Deleting Uploading And Closing Of files.

3 Create / Open a file Sean Costain 2012 Same 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.

4 fopen tags Sean Costain 2012 As 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

5 fclose Sean Costain 2012 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

6 Writing to a file Sean Costain 2012 There 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

7 Writing Cont. Sean Costain 2012 Creates the file testFile.txt Simulates user entry Writes the data into the file This creates a blank file and writes data to it.

8 Writing Cont. Sean Costain 2012 This creates a blank file and writes data to it. The test file looks like this

9 Writing Cont. Sean Costain 2012 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.

10 Writing Cont. Sean Costain 2012 This appends the data to the end of the existing file. The test file looks like this

11 Writing Cont. Sean Costain 2012 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.

12 Writing Cont. Sean Costain 2012 This appends the data to the start of the existing file. The test file looks like this

13 Reading the File Sean Costain 2012 Reading 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

14 Deleting a file Sean Costain 2012 When 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

15 Uploading Files Sean Costain 2012 Uploading 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.

16 Uploading Files Cont. Sean Costain 2012 Self feeding form 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.

17 Uploading Files Cont. Sean Costain 2012 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

18 Uploading Files Cont. Sean Costain 2012 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.

19 Login for Websites Sean Costain 2012 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

20 Login Cont. Sean Costain 2012 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.


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

Similar presentations


Ads by Google