Jun 03

How to write PHP to a file

I wanted to pass along this tasty bit of PHP to add to your tackle box that might help some of you out there. This trick runs a PHP file and creates a static file (HTML, XML, whatever), drastically cutting back on server resources like PHP processing and database calls. This should speed up your site and prevent lame crashes as your site grows.

You’ll only need to roll your sleeves up half way, because this is pretty easy.

STEP 1

Create three files:
updater.php
magic.php
index.html (make sure index.html is writable… CHMOD)

STEP 2

We’re going to call the PHP file we want to copy “magic.php”. You can put anything you want in here, just as you would any other PHP file. Maybe you want to create a dynamic XML file or do some intense database crunching and output those results to a simple HTML file. The thing to remember is that the PHP code itself is not going to be copied to the static file, but what the PHP code outputs, or would show to the browser will be copied. We’ll keep it simple for this example. — let’s add some PHP code:

<? echo "<h1>I am awesome</h1>"; ?>

All this file is going to do is output some simple HTML:

<h1>I am awesome</h1>

Rather than calling the PHP every time the page is requested, we can have the PHP run periodically and output to a static file. This makes the most sense when you are making multiple database calls or have dynamic content that does not need to be updated regularly.

STEP 3

Now open up updater.php and paste in the following code:

<?php

ob_start();

// Run the PHP file that you want to copy. This could be a MySQL query
// or any page that might be server intensive.
include ("magic.php");
$content=ob_get_contents();
ob_clean();

// Define what file to want to write to. This could be an HTML file, XML doc, or anything, really.
// Make sure to update CHMOD to allow write access.
$myNewPage = "index.html";

// Now actually run the PHP file and write the new file
$processFile = fopen($myNewPage, "w");
fwrite($processFile, $content);
fclose($processFile); 

?>

This code’s job is to open up magic.php, do whatever magic.php says to do and copy the results to index.html. SIMPLE! In our case the PHP is saying write “I am awesome” in a header 1 tag to index.html.

You can manually call updater.php through the web browser, http://yoursite.com/updater.php, or set up a cron job to do the dirty work whenever you want. Here’s more info about cron jobs and crontab.

RECAP

I find the updater.php script to be incredibly helpful in speeding up page load times by cutting back on database calls. I also find the script incredibly useful in creating dynamic XML documents. Rather than processing a whole bunch of PHP and database calls every time the XML file is requested, I can have that done behind the scenes. Then when the XML file is called, it returns a lightweight and quick file with no worry of timing out!

Keep Reading 0 Comments   |   Posted by Ian Corbin
May 07

Pris Doobie Down Down Music Video

I was pleasantly surprised when I found an old video I animated circa 2001 / 2002 for a Seattle Powerpop band called Pris. I remember I only had a couple days to draw the video in Flash and I was a total noob, design student at the time. I have had enough time away from it that I really enjoy the idea of the video, and I think It would be a lot of fun to do something like this now (seven years later) and see how it turns out. The video got a lot of solid play in Canada, so this was a welcome “career boost” at the time. The video is now being pushed by the Powerpop Academy in Japan.

Keep Reading 0 Comments   |   Posted by Ian Corbin
May 04

It’s Almost Webby Time

The 13th annual Webby Award winners are announced tomorrow, and dude… I hope I win. Just take a look at the highlight reel from last year’s show! If this doesn’t look like a once in a lifetime experience for a random web dude, then I don’t know what does.

The 12th Annual Webby Celebration Highlight Reel from Webby Awards on Vimeo.

Keep Reading 0 Comments   |   Posted by Ian Corbin
Apr 22

Shady Side, Maryland on the Chesapeake Bay

Chesapeake Bay

Chesapeake Bay

A Shipwrecked Boat

A Shipwrecked Boat

Keep Reading 0 Comments   |   Posted by Ian Corbin
Apr 15

2009 Webby Awards Nominate CelebrityTweet!

CelebrityTweet gets a Webby nod

CelebrityTweet gets some Webby love.

I was surprised to hear that my little side project, CelebrityTweet has been chosen as an official nominee in the 2009 Webby Awards Celebrity/Fan category. All I can really say is, “WOW!” It’s quite an honor to have your work recognized alongside some of the greatest agencies and corporations in the industry.

There are five nominations in each Webby category and two chances to bring home a webby. The first way is to have the Webby academy choose your site as an award recipient. The other way is to be chosen by the people as the People’s Voice award winner. To me, both are equally beautiful.

If you’d like to vote for CelebrityTweet in the Celebrity/Fan category (second from the top) please visit, http://pv.webbyawards.com.

I have some really stiff competition in the Celebrity/Fan category; E! Online, Yahoo! OMG, Muhammad Ali and Nickelodeon’s Yo Gabba Gabba. As you can see, I am the little guy here! BUT it is an honor to be hanging with these dudes. Here is the full list of nominees.

I need to talk Muhammad Ali into Twittering…

xo

Ian

Keep Reading 0 Comments   |   Posted by Ian Corbin
Previous Page 1 of 2
A photograph of Ian Corbin
  • Latest Tweet
    • Found this at Target... Uhh... I think I'll pass, LOL. But had to take a picture. http://twitpic.com/95d9f 11 hours ago
  • flickring
    Playstation 3Summerslam 2007WWE Saturday Nights Main Event MSG NYCRoyal Rumble 2008 MSG NYCThe WrestlerFrom the Merch BoothNinja on His Way to WorkPantsed MannequinWarp Speed 29th St
  • FriendFeed

    Found this at Target... Uhh... I think I'll pass, LOL. But had to take a picture. http://twitpic.com/95d9f

    Friday 7:47

    If you're a coffee junkie like me, you will see the beauty. http://bit.ly/jm8d8

    Thursday 20:26

    Working on CelebrityTweet improvements. It's getting late though and I'm almost out of gas! Maybe I'll push some updates out this weekend...

    Thursday 20:23

    Bookmarked a link: Best Practices For Effective Design Of "About me"-Pages | Design Showcase | Smashing Magazine

    Wednesday 15:08

    @GlobalGrind Bad. Thriller. Beat It.

    Wednesday 12:10

    My FriendFeed profile …

  • Cloudiness
    App Store CelebrityTweet chesapeake bay CHMOD CRON iPhone maryland Music NYC PHP trip Twitter Video Webbys Winter Work XML
  • Sushiroll
    • CelebrityTweet
    • Twypos
    • Wiiff - Wii Friends Forever
  • Recent Posts
    • How to write PHP to a file
    • Pris Doobie Down Down Music Video
    • It’s Almost Webby Time
    • Shady Side, Maryland on the Chesapeake Bay
    • 2009 Webby Awards Nominate CelebrityTweet!
  • Translator
    English flagItalian flagKorean flagChinese (Simplified) flagChinese (Traditional) flagPortuguese flagGerman flagFrench flagSpanish flagJapanese flag
    Arabic flagRussian flagGreek flagDutch flagBulgarian flagCzech flagCroat flagDanish flagFinnish flagHindi flag
    Polish flagRumanian flagSwedish flagNorwegian flagCatalan flagFilipino flagHebrew flagIndonesian flagLatvian flagLithuanian flag
    Serbian flagSlovak flagSlovenian flagUkrainian flagVietnamese flag     
    By N2H
  • Archives
    • June 2009
    • May 2009
    • April 2009
    • March 2009
    • February 2009

© Copyright Ian Corbin | Digital Journal, Digital Shipwrecks. Steal this content.