Sunday, July 15, 2012

Save Your PHP in UTF-8 Format Without BOM

BOM or byte order mark is to indicate the byte order of your file. If your PHP file contains non-English characters, UTF-8 formatted PHP file will display as question mark(?) if the PHP script is run on Linux/Apache web service platform. However, no problem is found on Windows/IIS web servers. You can run the following PHP codes to check whether your PHP got BOM:


<?php

$handle = fopen('yourfilename.php','r');
$bom = fread($handle, 3);

if (empty($bom)) {
 echo("Error");
}
else if ($bom === chr(0xef).chr(0xbb).chr(0xbf)) {
 // UTF8 Byte Order Mark present
 echo("BOM");
}
else {
 echo("No BOM");
}

?>


If you see BOM in your UTF-8 PHP file, you'll need to convert to non-BOM format. You can easily fix it by editing using Notepad++. After opening your BOM PHP file, go to the "Encoding" menu and choose "Encode in UTF-8 without BOM". Then save the file.



Your PHP can now output non-English texts without any problem!

P.S.: (update on 18th of July 2012) You need to upload to the server as ASCII formatted file. I missed out this important info earlier. Sorry.

Read More »

Adding PHPExcel PEAR Library in Shared Hosting Environment

You need to have the PEAR library installed in your shared host/server before this. If you haven't done so, you can check out my earlier post on how to do this.

The Steps I used:
  1. You can download the PHPExcel here. Choose the PEAR version. As of now, the current version is 1.77 and the file size for the .tgz file is 11.7MB.

  2. Extract the .tgz file. You'll see a PHPExcel.php and a PHPExcel folder. Like other PEAR add-ons, you just need to copy the PHP file and the folder to your PEAR root directory. You can find out the root directory during the installation of PEAR. You can also refer to my earlier post on this.

  3. Upload the above mentioned PHP file and its folder to your shared server.

  4. Test your PEAR PHPExcel library using the following codes:


    <?php

    set_include_path('./RELATIVE_PATH_TO_YOUR/PEAR/' . PATH_SEPARATOR . get_include_path());
    require('PHPExcel.php');
    require_once('PHPExcel/IOFactory.php');

    $excel = new PHPExcel();
    $excel->setActiveSheetIndex(0); // select a worksheet
    $excel->getActiveSheet()->setTitle('Products'); // name the sheet
    $excel->getActiveSheet()->setCellValue('A1', 'Orange');
    $excel->getActiveSheet()->setCellValue('A2', '1.30');
    $excel->getActiveSheet()->setCellValue('B1', 'Apple');
    $excel->getActiveSheet()->setCellValue('B2', '2.10');
    $excel->getActiveSheet()->setCellValue('C1', 'Total');
    $excel->getActiveSheet()->setCellValue('C2', '=SUM(A2:B2)');


    // Save document in the Excel 2007 format
    $excelWriter = PHPExcel_IOFactory::createWriter($excel, 'Excel2007');
    $excelWriter->save('Products.xlsx');
    echo("Products.xlsx created!");

    ?>


  5. Open the "Products.xlsx" with Excel to see if the xlsx file is fine as planned.


Note: If your PHP file above (to test the PHPExcel library), contains non-English character, you'll need to save it in non-BOM format preferably saved using Notepad++. Here's my post to address this fix.

As for the RELATIVE_PATH_TO_YOUR in red above, RELATIVE is the key. If you PHP file is in a subdirectory and your PEAR's root is in another subdirectory, you need to find a path to refer to PEAR. For example, if your testPHPExcel.php is in the /test directory and your PEAR is in /PEAR directory, you'll need this relative path such as ./../PEAR/.

You are done here! Enjoy!
Read More »

Install PEAR for Shared Hosting

PEAR is undeniably a good source of PHP code library that will make programmers' life a lot easier. Installing PEAR on Windows/IIS platform is easy especially when you have admin access to your PC/server.

When it comes installing PEAR in a shared hosting environment, admin access is null and there are workarounds that need to be addressed in order to use the PEAR library in the restricted platform.

Here are the steps I use:

  1. Go to http://pear.php.net/go-pear
    Copy the content and save it as go-pear.php or other names that you prefer

  2. Upload the go-pear.php to your server and launch the PHP codes by browsing the php such as logging on to http://yourdomain/yourpath/go-pear.php

  3. During setup, you need to find out the path of your PHP executable. You can easily find out by running a PHP like this:


    <?php
    echo `which php`;
    // or echo PHP_BIN if this doesn't work;
    ?>


  4. After the installation is done, you can now remove the go-pear.php for security reason. You will also be given the absolute path to your PEAR here.



    Note: To use PEAR without any problems you need to add your PEAR Installation path (/xxxxxx/xxxxxxxx/xxxxxxx/......./PEAR) to your include_path.


  5. Test your PEAR. You can try the following simple PHP code:



    <?php

    set_include_path('./PATH_TO_YOUR/PEAR/' . PATH_SEPARATOR
    . get_include_path());

    require_once 'PEAR.php';
    var_dump(class_exists('PEAR'));
    ?>

    If it returns a bool(true), you are all set. Enjoy! :)

    Note: You need to make sure the include path is there every time you need PEAR. This is for shared hosting environment. If you have admin access, you can easily skip this by adjusting the PHP.ini file.
Read More »