Category Archives: PHP

Increase PHP’s File Upload Settings with .htaccess

From time to time the need comes up to increase the maximum file upload size for a website. For reference, what I’m referring to is uploading media files (pictures, PDFs, data files, etc.) through an HTML web form.

Typically PHP comes configured to allow a 2 Meg upload size. Some web hosting companies allow you to make these changes directly in the PHP.ini file. In that case you open your PHP.ini file and simply look for “upload_max_filesize”, “post_max_size”, and “memory_limit” and make the appropriate changes.

If no PHP.ini file is available through your hosting platform an alternative method for adjusting this would be to add the following three lines to your .htaccess file.

php_value  upload_max_filesize  8M
php_value  post_max_size  16M
php_value  memory_limit  24M 

Some notes:

Keep in mind that extra large files will take longer to upload so you’ll want to make sure that your scripts do not time out when uploading. This will be a trial-and-error process based most likely on your client’s Internet connection speeds.

URL Trailing Slashes and Canonical URLs

I noticed recently in my Google Webmaster Tools account that Google seems to be honing in on house cleaning for webmasters. Maybe it’s the recent Google updates that have come down the pike in the past twelve months and maybe it’s just my imagination but nonetheless it’s a good habit for webmasters to practice.

The one thing that lead my suspicion of this was the notes on a few of my sites that Google has begun to put into “HTML Improvements area is alerts over trailing slashes in the URL string. For reference, the following URLs are not the same to a search engine:

  1. http://www.mysite.com/page-name
  2. http://www.mysite.com/page-name/
  3. http://www.mysite.com/page-name/index.html

Because I generally don’t link to any index.html files directly inside of directories, the first two in the list were of particular concern to me and not the third.

The solution is to put the following three lines into your .htaccess file:

RewriteCond %{REQUEST_URI} !\.[^./]+$
RewriteCond %{REQUEST_URI} !(.*)/$
RewriteRule ^(.*)$ http://www.mysite.com/$1/ [R=301,L]

That will automatically enter the trailing slash to the url if it is not already present. Remember to keep those three lines together in the .htaccess file.

Running PHP Script Inside of .HTM and .HTML Files

If your site is running on a LAMP (Linux, Apache, MySQL, PHP) platform it is possible to make the server execute PHP scripts inside of a .HTM or .HTML page.  Pages with the HTML file extensions (.htm or .html) typically, when requested from the server are just sent straight to the browser by the server. With those extensions the web server assumes that there is nothing more to do than that. With Apache’s .htaccess file, however, you can alter this.

Why would you want to do that? It is common for webmasters to come across situations where a site consists entirely of static HTML files and the job does not allow for changing those extensions. Typically this is for search engine reasons.

The quick work-around for this is to add a line or to into the .htaccess file in the site’s root directory that will tell the server to look through each .html or .htm files for PHP scripting prior to sending them to the requesting browser/user. The exact lines of .htaccess code will depending upon the settings of the server that you’re running on but here are the most common.

AddHandler php-script .htm .html

or

AddType application/x-httpd-php .php .htm .html

Each simply tells the server to parse HTML pages as if they were PHP pages and generally, one of those two lines will accomplish the goal.

Managing an Ambiguous Brand

[quotes_right]One famous example of search ambiguity is Apple – are searchers looking for the corporation that sells computers, iPhones, iPods, and iPads, or are they searching for the fruit?[/quotes_right]

This article on Search Engine Watch covers the details of managing an ambiguous brand, but it also contains some nice points that apply to any brand. What happens when a searcher is quickly glancing down through the search results pages? How do you attract clicks that are relevant to your site’s content?

Nine tips for managing an ambiguous brand … Read full article.

CSV Export Using PHP’s Output Buffer

Creating the CSV file without using adding/deleting a file on the server’s file system. This involves the use of the PHP output buffer. In the case of a CSV file, they are being created by the PHP script on the fly, adding the data that you want to export on a row-by-row basis. For this we need to:

1) create a file handle
2) fputcsv each row into the file
3) send the entire thing to the browser to be downloaded

Here is the code:

<?php
header("Content-type: text/csv");
header("Cache-Control: no-store, no-cache");
header('Content-Disposition: attachment; filename="filename.csv"');

$outstream = fopen("php://output",'w');

foreach( $myArray as $row ) {
	fputcsv($outstream, $row, ',', '"');
}

fclose($outstream);
?>

$myArray is an array of records just the same as you would find in a database (the same thing that will appear in the new, downloaded CSV file). Notice that there is no file being created on the server.

Helpful tip:

Just relying on the extension doesn’t work, even in Windows. The magic is in the third header setting, “Content-Disposition,” which informs the browser to download as a separate file
~ from ToSweetToBeSour.com