1 / 20

Advanced Web 2012 Lecture 6

Advanced Web 2012 Lecture 6. Files. Php allows for the : Creation Reading Appending Deleting Uploading And Closing Of files. Create / Open a file. Same command Creates or Opens a file. Opens or Creates the file.

iniko
Download Presentation

Advanced Web 2012 Lecture 6

An Image/Link below is provided (as is) to download presentation Download Policy: Content on the Website is provided to you AS IS for your information and personal use and may not be sold / licensed / shared on other websites without getting consent from its author. Content is provided to you AS IS for your information and personal use only. Download presentation by click this link. While downloading, if for some reason you are not able to download a presentation, the publisher may have deleted the file from their server. During download, if you can't get a presentation, the file might be deleted by the publisher.

E N D

Presentation Transcript


  1. Advanced Web2012Lecture 6 Sean Costain 2012

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

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

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

  5. fclose Closing the fileProcess 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. Sean Costain 2012

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

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

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

  9. Writing Cont. This appends data to the end of a pre-existing file. Opens the file testFile.txt Writes the data into the file Simulates user entry Sean Costain 2012

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

  11. Writing Cont. This appends data to the start of a pre-existing file. Opens the file testFile.txt Writes the data into the file Simulates user entry Sean Costain 2012

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

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

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

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

  16. 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 uploaded The type="file" attribute of the <input> tag specifies that the input should be processed as a file. Sean Costain 2012

  17. Uploading Files Cont. • 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 Moves the file from the temporary location to the one you have set up. Sean Costain 2012

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

  19. 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 Sean Costain 2012

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

More Related