From the Ask Apache blog, there's a quick tip that makes it simple to give your site's visitors the option to either download or view a PDF file no matter what their browser default is.
One of the most annoying things on the Internet for me is when I click on a link to an Adobe PDF file. For me this is annoying to the extreme because the PDF file is openened directly in your browser because of the Adobe PDF Plugin that almost all browsers have installed. [...] For me an ideal solution would be to offer me choices.
His solution uses mod_rewrite in an .htaccess file to push the visitor to a PHP file. This file grabs the filename they want and pulls in the content, pushing it back out the other side with the "attachment" header that forces a request box on the browser.
Felix Geisendorfer has a quick tip for CakePHPers out there today - an update to the framework that might cause a "gotcha" moment in your code:
Just got bitten by this one when updating to the latest version of CakePHP. If you use code [checking to see if the return from a save() is true] in your app, you're in for a surprise. Because as of revision 5895 Model::save() now returns Model::data on success if its not empty.
He notes that most developers don't seem to do it this way, but it tripped him up enough to where he wanted to share it with the CakePHP community so they'd know. Check out the comments on the post for other issues that might be caused by the change.
Even more good news for mysqlnd users out there - according to this new post on the Internet Super Hero blog, some of the tuning options it enables can help you save 40% of the memory you were using before.
mysqlnd saves memory. It consumes half as much memory as libmysql. This is what we have been convinced of. This is what we taught you. Then I tried to test it and made Andrey get nervous for a few hours'¦ Meanwhile he is fine again and we can announce: mysqlnd saves memory, not only in theory, we tested it - we can proof it, can we?
The proof comes in the form of a few "tricks" and some of the settings that the software can use to optimize buffer sizes. Benchmarks (and the code to run them) are also included to show what the differences are between mysqlnd and libmysql.
In a new post to his blog today, Felix Geisendorfer shares a method to make error handling in the CakePHP model functionality a bit more "beautiful".
Now I've written actions like the one above in the past as well. It's just that I've not had many MySql errors since I've switched to CakePHP. The Model class usually handles all the DB operations flawlessly and it's probably been over a year that I've written a custom MySql statement in my code somewhere. However, even CakePHP or, what's more likely, the database can fail or deny operations.
He shows some code snippets of what he sees as less beautiful versions of database insertion code, methods that either don't check the response/errors or make a messy job of it. His solution works by returning the result into a switch statement to check the results and handling it there (versus a series of ifs). There's a class you'll need to make it work (Common), but he shows how to use it and creates a simple "add" example with it and a new Controller to handle the request.
DevShed steps into the final part of their series looking at Command objects in PHP5 with this new tutorial just posted - "Using Self-Saving Objects with Command Objects in PHP 5".
I'm going to extend the creation and use of command objects to use the pattern in a more helpful fashion. Essentially, I'm going to explain how to utilize command classes for defining and handling self-saving objects, that is objects capable of storing a copy of themselves on a database, text file, etc.
They start by defining the core modules and classes for the creation of the objects. They build the other functionality on top of that, creating the save and load mechanisms. The wrap things up and add a few more small bits of functionality before giving a real-life example of the pattern and code in action.
On his blog today, Ammar Ibrahimrelays a story of how the Mail component from the eZ component frameowrk "saved the day" for a project he was working on.
A few days ago I was asked to develop a simple script for a solidarity campaign. The idea is that people send their photos as attachements to some email. The script would download all images attached and insert a record for that in the database.
I got introduced to eZ components during my last visit to Norway to attend the eZ systems conference. I decided to give it a shot, and oh boy it's just amazing, probably the cleanest and simplest API ever.
The Mail attachment provided all that he needed, and he includes the script (about 50 lines long) that he used to fetch the emails. He did have one issue with the ability to fetch a single email with the tool, but he managed to hack around it (script included as well).
In their continuing series on the Zend Framework, the IBM developerWorks site has posted part three today, a closer look at working with feeds in the framework.
This "Understanding the Zend Framework" series chronicles the building of an online feed reader, Chomp, while explaining the major aspects of using the recently introduced open source PHP Zend Framework.
In parts 1 and 2, we discuss the goals behind the Zend Framework and show you how to use it to create the beginnings of our online feed reader, creating a form and adding information to a database while getting to know the MVC pattern. In this tutorial, you will see how to use the Zend Framework to implement the online feed reader portion of the application.
Obviously, you'll need to have kept up with the previous articles in the series (one, two) to get a grasp on this one, but those are quick reads so you can get caught up quickly. They first build up a new schema with tables for the RSS data being pulled in, then start creating the view and controller to get the ball rolling. They also introduce the Zend_InputFilter class for testing and removing unwanted content. The code in the controller allows the creation of new feeds, the subscribe to a feed, update a users information, and authenticate the user.
The FeedController does the work of grabbing the contents of the remote feeds and parsing them down to be used locally. They also include functionality to "bookmark" entries and save them for later.
On Arul Pradad's blog today, there's a quick post talking about a simple way to save the XML coming from a Flash file with PHP.
I was trying to create a XML file on the server, using PHP. The XML string was being sent from Flash, by adding the same as a property to a LoadVars obj. ( Since I had to send few other data items as well - other than the XML string, I couldn't use Flash's XML.sendAndLoad method.
He shows the code that was giving him troubles and traces the path he took to discover the ultimate problem with the XML - the quotes in the file were being escaped, resulting in PHP's inability to parse it correctly. A simple call to stripslashes solved that, and the app works just fine.
DevShed continues their "serializing objects" series today with part two of the series, highlighting the use of the sleep and wakeup functionality of PHP to help with the serialization.
After refreshing the concepts that I deployed in the first part of this series, it's time to focus on the topics that I'll cover in this article, so you'll know what to expect before you continue reading. In this second part, I'll explain how to use objects in conjunction with the "__sleep()" and "__wakeup() magic functions respectively, in order to get the most out of them.
They start with a look at defining self-saving objects with their ObjectSaver class developed earlier. Building on that reminder, they integrate the "__sleep()" and "__wakeup()" functionality to handle calls immediately before and immediately after the handling of the object. They then use this new functionality to create persistent objects, capable of maintaining values across page requests.
On the blog at HackersCenter.com, there's a new post looking a a few simple ways to help optimize your PHP code. Sure, most of the everyday scripts developers write don't benefit a whole lot from shaving off those extra milliseconds in execution time, but it's always a good practice to get into when you get into those larger scripts.
Php optimization is the skill of fine tuning an application to extract maximum performance with minimum server load. Many of my clients in the past have wondered why I spend extra hours just to get the script execution time to be cut down by 1 second.
However if you consider an average large scale application, it would receive about 1000 unique visitors an hour (approximation based on average statistics for my clients websites) and an average of about 8 page views per user. This would mean that your server would face 8000 requests for that page every hour.
The author follows this up with a few more stats before getting on with the heart of the article - the tips and tricks. They demonstrate a sample function to help test the speed of the script before looking at different topics of optimization - outputting an array, regular expressions, using references, and file inclusion. For each, there's a "slow way" and a "fast way" - I wonder which is the better choice...