This snippet comes in handy and has saved me hours of menial coding tasks.  Let’s say you have a customer that you do a lot of work for and you want them to access certain files.  In my case, I produce and edit a lot of TV commercials for clients.  I never hard code their “download portal”, because it’s a waste of time, simply upload your files via ftp to a folder and put this snippet in your index.php file along with other messaging to your customers.

This procedural code can be inserted anywhere to automatically generate a clickable list of files for your customers.

Here’s the PHP snippet that will save you lots of time later:


<?php
$s=scandir(".");

foreach ($s as $file){
// replace ".mp" with the file extension you want to allow through this filter -- this loop will ignore any other file extension.
if (stristr($file,".mp")){
$file_title=str_replace("_"," ",$file);  // This displays a nice "Title Without Those Ugly Underscores"
echo "<a href='$root$file' style='margin:5px'>$file_title</a><br/><br/>";
}
}
?>

 

You might also want to force files of a certain extension to be download only by default, this is often done by adding this line to the .htaccess file either site wide or in the individual folders you wish:

Add to .htaccess to force download of file extensions instead of the browser possibly trying to open the file and displaying it inside its window.  Just simply add the file extension to the list below, much like I added “mp4” at the end of the list.  Keep the same pattern by separating each file extension with the pipe (|) delimiter.

Also keep in mind the module mod_headers needs to be loaded in Apache2, this is a common module that is usually enabled by default by most web hosts, but do not assume.


&lt;filesmatch "\.(?&lt;span="" pre=""&gt;i:doc|odf|pdf|cer|txt|mp4)$"&gt;   Header set Content-Disposition attachment &lt;/FilesMatch&gt;

Sometimes, there are just weird things with line breaks — it’s really hard to tell what type of line break is embedded in certain data.  Sometimes, text entered from a form with line breaks throws the output off, especially if your script needs to break the text or often the data feed down by line (also referred to as a row).

If you can capture the text in the $text variable, this will output it and find those line break offenders.  Throw Mac keyboard input into the mix, and you can spend hours trying to find these things so your script reads and breaks up the data properly.


echo str_replace(array('\r\n', '\r', '\n', '\t'), array('\\r\\n', '\\r', '\\n', '\\t'), $text);

Adding this to your template file or functions.php in some cases will allow your PHP scripts to access variables passed in the URL query strings.  You know, that ugly text after the Question mark in the website address:  “http://tools.belchamber.us?variable=value&variable2=value”.  Most of you probably already figured out just looking at these type of URLs that some values are being passed from one page to another.  The variables extracted this way are called “Get variables” and aren’t very secure.

Remember to avoid passing any sensitive or personal data inside a URL — anyone can view it on the address bar, or worse yet – in your browser history!


function add_query_vars_filter( $vars ){

$vars= array("var1","var2","var3");

return $vars;

}

add_filter( 'query_vars', 'add_query_vars_filter' );


Moving from procedural with mysql_connect, etc to PDO objects should be an easy transition.  It’s so much faster and reliable, too.  The “mysql” library in PHP will soon be deprecated, so you hold-outs won’t have a choice.  It’s actually less code to write and get whole queries into resulting arrays much faster.  Trust me!

Here’s how you instance a new PDO object and connect to your MySQL database through PDO:


<?php

/*** mysql hostname ***/
$hostname = 'localhost';

/*** mysql username ***/
$username = 'username';

/*** mysql password ***/
$password = 'password';

try {
$dbh = new PDO("mysql:host=$hostname;dbname=mysql", $username, $password);
/*** echo a message saying we have connected ***/
echo 'Connected to database';
}
catch(PDOException $e)
{
echo $e->getMessage();
}
?>