Monitor

Lesson 9: File handling in php

 Signed in as:    
      Sign out

 

Links

Your home page

Advanced Websites
   Layout and design
   Backgrounds
   Page framing
   Server Side Includes
   Introduction to CGI
   Introduction to php
   php: 'if', emails
   php: string handling and date / time
   php: file handling
   Responsive Web Design
   Processing images
   php: databases
   php: logins and passwords
   Tracking hits
   Navigation I
   Navigation II

For our first look at file handling using php, we use the example of logging access to your pages. Topics include:

 

 

Note: I strongly suggest you view all our videos in full screen mode: click the icon in the lower right corner of the video area (it shows four arrows pointing to the corners of the screen). When you are done, the Esc ("escape") key returns you to regular mode.

 


Lesson 9: File handling
Part 1: Creating log files

Click here for the video transcript

 

Hands on exercise

This exercise is focused on setting up to log access to your pages. We'll add logging capabilty to both your AddTraveler.php and Feedback.php

First, create a Logs directory on your personal computer and on your server. (If you need it, here's a brief video demonstrating this process.)

Now add code to your Feedback.php to log users submitting suggestions:

 

Now, add code to your AddTraveler.php, to log users submitting applications to join your trip:

 

Upload your updated Feedback.php and AddTraveler.php files to your cgis directory

Test your updated Feedback.php by entering some suggestions and test your updated AddTraveler.php by entering info for new fictious travelers.

Download your Travelers and Suggestions log files to your PC and open the files to spot errors; fix any problems you may find.

 

Session 2

How to read files on your server. Topics include:

 

 


Lesson 9: File handling in php
Part 2: reading files on your server

Click here for the video transcript.

 

Hands on exercise

This exercise is focused on reading data from a file on your server, similar to the work in the lab.

However, here we will add a new field in your trip application form (signUpForm.shtml) that asks the traveler to specify what country's passport they will be using for the trip.

Then in your addTraveler.php CGI, you will search a server-side file, Countries, to ensure the passport is for a country that Wales will allow in. If so, simply continue; if not, issue a message that the passport country is not allowed, so do one of: check for spelling error; discontinue their application; or if they have another passport, try that.

Details:

First, add a new input control in your signUpForm.shtml:

location: after the picture file request, before the "how did you hear about this" control

descriptive text: "What country passport are you traveling on?"

then the input control: type=text, name=passport, size=20, maxlength=40

save the updated file and upload it to your server.
 

Next add code to your AddTraveler.php to look up the passport value in the Countries file:

The logic for this process is very similar to that in the lecture, and I'm confident you can figure this out yourself. Choose any one of the five general logic structures from the lecture; try all five if you're comfortable with that.

Note: I found it's very important to trim the value returned from fgets() here

After each CGI code update, save and upload the revised code to your cgis directory, and test.

Your tests should try some deliberately mis-spelled names. Note the list does not necessarily reflect any current geo-political situation; in a real world application it would be updated based on changing alliances, sanctions, and / or other political considerations.

Entries in the Countries list are all lower case and in ascending alphabetical sequence.

 

Challenge / exercise stretch:

I thought it might be helpful to the user to provide a little more information if the passport country was not successful. So this option will display the country before and after the submitted country, alphabetically. This way the user can possibly spot their error. So you'll modify cgis/AddTraveler.php.

So the idea is to save the previous and current values from Countries, and if the submitted passport country ($passport) < current value ($work), then display the previous and current entries as part of the routine where the passport test fails.

One possible approach: create a variable, $previous, initialized to 'before_start_of_list'; then in your checking loop, first test for equality of $work and $passport - if equal, go to your passport match routine; but if $passport < $work, go to the routine for handling passport test failure; if you fall through both of these if's, then you have $passport > $work, so save $work into $previous in preparation for the next iteration. In your passport failed routine create a small reply page and, echo something like:

     "<p>The nearest countries to yours are $previous and $current</p>";

then give the user the option to use the back button and retry or to close their window and stop their trip application.

In your passport match routine, simply continue the work of the CGI (that is, fall through to your routine creating the user file name and supplying defaults for missing values).

As before, save your updated code, upload it to your server, and test that this works.

 

 

Session 3

How to use form data to build a personal web page. Topics include:

 

 


Lesson 9: File handling in php
Part 3: creating web pages from form data

Click here for the video transcript.

 

Hands on exercise

This exercise is a complex project, with the goal of creating a model file and getting your AddTraveler.php to create a new file for a trip applicant. The pieces:

Details:

Create a sample result file

First, copy img_0598.jpg from AdvancedWebFiles/images into your website's /images directory, and rename it to "PatriciaAragon.jpg".

Next, create a page that looks like you want your final applicant pages to look like. Use your tripsite.shtml file as a model: open tripsite.shtml and do a "Save as" using PatriciaAragon.shtml for the name. Now, make the following changes ...

Upload your PatriciaAragon.shtml file to your website, and check that it looks good to you when you display it. Feel free to tweak it so you're happy with the effect.

Create your model traveler file

Next, use your sample page as the starting point for creating your ModelTraveler.txt file. Include special text where you intend to use form values. (You can use my approach of "**" followed by some text or devise your own methodology.)

Finally, modify your AddTraveler.php to build the new travelers page from form info. Be sure to include tests for duplicate existing files (both the picture and the applicant page). Your resulting code might have these pieces:

Test like crazy!

 

 

Session 4

Loose ends and miscellaneous points

 

 


Lesson 9: File handling in php
Part 4: loose ends

Click here for the video transcript.

 

Hands on exercise

This exercise focuses on the travelers page. The pieces:

 

 

Supplemental materials

    A cumulative summary of the contents of the course so far: Click here for the cumulative technical summary

 

 

 Lesson navigation 

 

 

Email us if you would like additional information. We would love to see your comments and recommendations for our site.