Skip to main content

By clicking Submit, you agree to the developerWorks terms of use.

The first time you sign into developerWorks, a profile is created for you. Select information in your profile (name, country/region, and company) is displayed to the public and will accompany any content you post. You may update your IBM account at any time.

All information submitted is secure.

  • Close [x]

The first time you sign in to developerWorks, a profile is created for you, so you need to choose a display name. Your display name accompanies the content you post on developerworks.

Please choose a display name between 3-31 characters. Your display name must be unique in the developerWorks community and should not be your email address for privacy reasons.

By clicking Submit, you agree to the developerWorks terms of use.

All information submitted is secure.

  • Close [x]

developerWorks Community:

  • Close [x]

Learning PHP, Part 3: Authentication, objects, exceptions, and streaming

Nicholas Chase is the founder and creator of NoTooMi. In addition to technical writing for large corporations, he has been involved in website development for companies such as Lucent Technologies, Sun Microsystems, Oracle, and the Tampa Bay Buccaneers. He has been a high school physics teacher, a low-level-radioactive waste facility manager, an online science fiction magazine editor, a multimedia engineer, an Oracle instructor, and the chief technology officer of an interactive communications company. He is the author of several books, including XML Primer Plus (Sams 2002).

Summary:  This tutorial is Part 3 of a three-part "Learning PHP" series teaching you how to use PHP through building a simple workflow application. In this tutorial, you will learn about using HTTP authentication, streaming files, and how to create objects and exceptions.

03 Jan 2013 - Nicholas Chase updated content throughout this tutorial to reflect current PHP technology.

View more content in this series

Date:  03 Jan 2013 (Published 12 Jul 2005)
Level:  Intermediate PDF:  A4 and Letter (792 KB | 36 pages)Get Adobe® Reader®

Comments:  

Using streams

Now that you've set up the system\, you're ready to enable the user to actually download the available files. From the very beginning, these files have been stored in a non-web-accessible directory, so a simple link to them is out of the question. Instead, in this section, you will create a function that streams the file from its current location to the browser.

What are streams?

Now, the way in which you actually access a resource, such as a file, depends on where and how it's stored. Accessing a location file is very different from accessing one on a remote server through HTTP or FTP.

Fortunately, PHP provides stream wrappers. You make a call to a resource, wherever it is, and if PHP has an available wrapper, it will figure out just how to make that call.

You can find out which wrappers are available by printing the contents of the array returned by the stream_get_wrappers() function, as in Listing 10.


Listing 10. Displaying available stream wrappers
<?php

print_r(stream_get_wrappers());

?>

The print_r() function is extremely handy for seeing the contents of an array. For example, your system might give you Listing 11.


Listing 11. Available stream wrappers
Array
(
    [0] => php
    [1] => file
    [2] => http
    [3] => ftp
)

This enables you to easily store your files on a remote web server or FTP server as an alternative to storing them as files on the local server. The code you use in this section will still work.

Let's take a look.


Downloading the file

For the user to see a file, the browser has to receive it. It also has to know what the file is in order to display it properly. You can take care of both of these issues. Create a new file called download_file.php and save it in the loggedin directory. Add the code in Listing 12.


Listing 12. Sending the file
<?php

   include ("../scripts.txt");

   $filetype = $_GET['filetype'];
   $filename = $_GET['file'];
   $filepath = UPLOADEDFILES.$filename;

   if($stream = fopen($filepath, "rb")){
      $file_contents = stream_get_contents($stream);
      header("Content-type: ".$filetype);
      print($file_contents);
   }

?>

Despite its power, the process here is actually quite straightforward. First, you open the file for reading and for buffering. What you actually do with the fopen() function is create a resource that represents the file. You can then pass that resource to stream_get_contents(), which reads the entire file out into a single string.

Now that you have the content, you can send it to the browser, but the browser won't know what to do with it and will likely display it as text. That's fine for a text file, but not so good for an image, or even an HTML file. So, rather than just sending it raw, you first send a header to the browser with information on the Content-type of the file, such as image/jpeg.

Finally, you output the contents of the file to the browser. Having received the Content-type header, the browser will know how to treat it.

As far as deciding which file and type to actually use, you're reading these from the $_GET array, so you can add them right to the URL, as in:

http://localhost/loggedin/download_file.php?file=NoTooMiLogo.png&filetype=image/png

Enter this URL (with an appropriate file name and type, of course) into your browser to see the results in Figure 6.


Figure 6. Downloading a file
Screen capture of downloading a file

Adding a link to the file

Because all the information the download page needs can be added to the URL, it's simple to add a link enabling the user to download a file. You create the display of available files using the display_files() function, so you can add the link as in Listing 13.


Listing 13. Adding the link
...
for ($i = 0; $i < count($workflow["fileInfo"]); $i++){
    $thisFile = $workflow["fileInfo"][$i];
    echo "<tr>";
    echo "<td><a href='/loggedin/download_file.php?file="
.$thisFile["fileName"].
            "&filetype=".$thisFile["fileType"]."'>".$thisFile["fileName"]
."</a></td>";
    echo "<td>".$thisFile["submittedBy"]."</td>";
    echo "<td>".$thisFile["size"]."</td>";
    echo "<td>".$thisFile["status"]."<td>";
    echo "</tr>";

}
...

You can see the results in Figure 7.


Figure 7. Linking to the file
Screen capture of linking to the file

Click a link to verify the file.

Next, you'll look at encapsulating this process into an object.

4 of 11 | Previous | Next

Comments



static.content.url=http://www.ibm.com/developerworks/js/artrating/
SITE_ID=1
Zone=Open source, Linux, XML
ArticleID=133669
TutorialTitle=Learning PHP, Part 3: Authentication, objects, exceptions, and streaming
publish-date=01032013
author1-email=ibmquestions@nicholaschase.com
author1-email-cc=