Tuesday, August 25, 2009

File_Get_Contents replacement (CURL)

Some servers have file_get_contents function disabled. But don't worry, most of them have curl library installed. Here is a replacement function for file_get_contents, using CURL library. Easy and working:

  1. function file_get_contents_curl($url) {
  2. $ch = curl_init();
  3. curl_setopt($ch, CURLOPT_HEADER, 0);
  4. curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
  5. curl_setopt($ch, CURLOPT_URL, $url);
  6. $data = curl_exec($ch);
  7. curl_close($ch);
  8. return $data;
  9. }



Using this function is easy, just like file_get_contents function :

$contents = file_get_contents_curl( 'http://...');

Tuesday, August 4, 2009

PHPMailer UTF-8 messages

Sending UTF-8 body messages with PHPMailer is pretty easy. You just have to set charset

Here is an example: $mail->CharSet = 'UTF-8';

So now you can send utf-8 mail messages using PHPMailer. Good luck.

Wednesday, June 24, 2009

How to validate a hex color in PHP

Validating a hex color code in PHP is easy.

Rules:

Hex colors must start with '#' and have 6 characters.
Hex colors can contain the following letters: [A-F]
Hex colors can contain the following digits: [0-9]

So using pregmatch, the hex validation code is:

if(preg_match('/^#[a-f0-9]{6}$/i', $color)) //hex color is valid
{

}

Tuesday, June 23, 2009

PHP Upload and Image Manipulation Class

I've been looking for a simple to use class for uploading and manipulating images. And I have found it.

Here it is: http://www.verot.net/php_class_upload_overview.htm

You can upload files, validate mime types, set language for errors. It's really easy to use. It's free for non-commercial applications.

You can now easily upload and manipulate images (resize, watermark, etc) with this PHP class.

Sunday, May 10, 2009

PHP 301 redirect

301 redirect means "moved permanently"

Every website has to have SEO onpage. That means sometimes you need 301 redirects, when you want to replace some url with a new url. How do you get 301 redirect in PHP? Using header function.

Here is a simple example:

header ('HTTP/1.1 301 Moved Permanently');
header ('Location: '.$new_location);
exit();

This code must be used in the old file, that you want to remove from search engines. $new_location is the new path where the script is being redirected. exit() must be used to stop the script.
Remember to not display anything (any HTML or other text sent to browser) before calling header() function. Or if you do, you can use output buffering to clear the output.

Monday, May 4, 2009

PHP include path

How to set include path



Include path separator differs from one operating system to another. For Windows path separator is ; and for unix separator is :. So how do you make your code create the right include path regardless of your OS?

First, you detect server's OS:

if (stristr(PHP_OS, 'WIN'))
{
$separator = ';';
}
else {
$separator = ':';
}

Then you set your classes path, for example:
$yourpath = './classes';

And then you add this path to include path:

ini_set('include_path', ini_get('include_path') . $separator . $yourpath);

This way, either your server is Windows or a Unix system, it will always include your path using the right separator character, and you don't have to change the code everytime you switch to another OS.

Thursday, April 23, 2009

Get file extension in PHP

Here are some simple ways to retrieve extension of a file using PHP. You need this when you try to validate file uploads, for example.

So here is one simple way:

$extension = end(explode(".", $filename ));

Let's say we have: $filename = 'text.jpg' then the result will be jpg.

Another way of getting file extension is using pathinfo() function:
$fileinfo = pathinfo($filename);
$extension = $fileinfo['extension'];

And the third way:
$extension = substr(strrchr($filename,'.'),1);

I use the first method, works just fine. You can use what method you want to determine a file extension.