I don’t always browse the web, but when I do, I prefer Google Chrome (Side-note: If you don’t get the reference, spend more time on reddit.com). Unfortunately, some people still like using IE. Some code I wrote that worked great on Chrome and Firefox didn’t cooperate with IE. These headers seemed to work consistently whereas others (like leaving out the Pragma header) would result in IE throwing a ‘Cannot load this site’ error when the Save dialog would open. The StackOverflow forums have some other references in-case these don’t work for you.
header("Pragma: public");
header("Content-Type: application/octet-stream");
header("Content-Disposition: attachment; filename=some_filename.csv");
echo $content;
die();
Share on Facebook
Tags: PHP
function numeric_only($string)
{
//function to strip characters other than numerals from a string
$numString = ereg_replace("[^[:digit:]]", "", $string);
return $numString;
}
Share on Facebook
Tags: PHP
PHP has a built-in function called mysql_real_escape_string() for use in MySQL queries but MSSQL has been left in the lurch for this one. I found this handy function on the StackOverflow forums that works like a charm (renamed for more uniform convention).
function mssql_escape_string($data) {
if ( !isset($data) or empty($data) ) return '';
if ( is_numeric($data) ) return $data;
$non_displayables = array(
'/%0[0-8bcef]/', // url encoded 00-08, 11, 12, 14, 15
'/%1[0-9a-f]/', // url encoded 16-31
'/[\x00-\x08]/', // 00-08
'/\x0b/', // 11
'/\x0c/', // 12
'/[\x0e-\x1f]/' // 14-31
);
foreach ( $non_displayables as $regex )
$data = preg_replace( $regex, '', $data );
$data = str_replace("'", "''", $data );
return $data;
}
Share on Facebook
Hopefully this saves someone some time. I spent hours working on a resolution before I came across it.
Background: Installed Win2008 + IIS7 + PHP 5.3 using the FastCGI method (used the Windows installer to configure IIS7).
Symptoms: PHP worked fine in my Default Site but when attempting to execute any scripts in Virtual Sites or Virtual Directories, every single PHP file would yield a 404 error message.
Solution: The problem ended up being that the ‘open_basedir’ config variable from the php config file is applied to ANYWHERE that PHP is run…regardless of what the actual home folder should be for a site (PHP picks up the basedir during installation and statically enters it in your php.ini file). The default ‘basedir’ will be whatever the root of your ‘Default Web Site’ in IIS is (usually c:\inetpub\wwwroot\). If you comment out this line (enter a semicolon before the line) and restart IIS, it will solve the problem. If you’re running PHP as a FastCGI module, you’ll actually have to rename (or copy) the php.ini file to a file named ‘php-cgi-fcgi.ini’ in the same folder as your php-cgi.exe executable. Be sure to restart IIS!
Share on Facebook