PHP Basics / Advanced Output pt. 2
Subtitles of the Movie
There may be times when you don't want your output going directly to the browser. There are times in your application when you want to redirect people to a different page. If you've already started output to the browser, you can't send new headers; otherwise you'll cause the program to break. If you have a lot of output occurring and you're not sure yet if you want it to go to the browser, you can send it to a buffer in PHP and then when you're ready for it to output, you can just flush the buffer. Using buffers in PHP is easy. You just type output buffer or ob underscore start. This starts the buffer so any output goes to the buffer and not to the browser. The string, even though it uses the echo command, is going to the buffer and if I type in ob end flush, it will flush the buffer to the browser. Notice it came out to the browser as we expected. If I didn't want it to go to the browser, I could just dump the buffer or clean it. In that case, I type in ob end clean. Using bufferings can be very powerful in helping you control what and when output goes to the browser. There's one other item about advanced outputs I want to show you and that's outputting to a file. We can output to a file by using the command file put contents. File put contents will take whatever contents you pass to it and place it in a new file in the same directory as the script that's currently running. When we call the file put contents function, we must pass it a name for this new file and then the contents we want to be outputted. In this case will be string. When we look at our directory, since we know that this script is running in the folder 0204, the web server, which is doing the processing, must have write permissions to this folder. If it doesn't, then if we try to do a file put contents, we're going to get an error. Permission denied. It's very important then that your web server has access or write access to the folder in which you want to file put contents. In this case, I'm going to do a get info on the folder and look at the permission details. My web server runs as a group www, but rather than changing the group permissions, in this case, for demonstration, I'm just going to allow others or everyone write permission to the folder. This is very important that you do not do this on a production machine. This is only for demonstration purposes. Giving others or everyone write permission to a web folder will allow anybody publicly on the Internet to maliciously access that folder. Now that I've given the write permissions to the folder, I can go ahead and refresh my script and we can see that my new file was created. I'm going to delete that file and do that again. PHP works very fast in reading and writing to files. This finishes our discussion on advanced output techniques in PHP.
Tutorial Information
| Course: | FileMaker 9 & PHP Foundations |
| Author: | Lance Hallberg |
| SKU: | 33786 |
| ISBN: | 1-933736-99-2 |
| Release Date: | 2007-08-22 |
| Duration: | 9.5 hrs / 107 lessons |
| Work Files: |
Yes |
| Captions: | For Online University members only |
| Compatibility: |
Vista/XP/2000, OS X, Linux QuickTime 7, Flash 8 |
VTC Sign up & Benefits
- Unlimited Access
- 98,729 Video Tutorials (23,265 free)
- Video Available as Flash or QuickTime
- Over 1026 Courses
- $30 for One Month Access
- Multi-User Discounts Available
United States 