Preview PDF in PHP

PHP          Trackback

If you want to create preview image of some pdf you have on your site, all you need is web server that has installed Imagick. If you are lucky enough to have that you can use this code:

$preview = new imagick('somepdf.pdf[0]'); /* read first page of PDF */
setImageFormat('jpeg'); /* set preview format to jpeg */
header('Content-Type: image/jpeg'); /* display preview */
echo $preview;


PHP Fatal error: Allowed memory size of xxx bytes exhausted

PHP          Trackback

Familiar with this error code? I was working on some PHP code that is doing bunch of data analysis and I got that error. There is one line of code that can help you to fix this problem. Just put this line at the top of the script:

ini_set('memory_limit', '16M');

This will set memory limit to 16MB. If this is not helping, try to increase memory limit even more.
If you have access to php.ini and you would like to increase memory limit for all your scripts you could add this line to the server’s php.ini file:

memory_limit = 16M

If you see this message when you are running your script that is frequently executed, I would recommend you to check your code first, because most common case when you are going to see this message is whan your code is poorly written and is needlessly eating too much memory.


Export MySQL To Excel better way

PHP,SQL          Trackback

I showed you how to export your MySQL tables to MS Excel file, but that wasn’t real excel files we were creating. Actually you were getting CSV and HTML files which excel can read. These were textual files, and now I am gonna show you how to create binary excel file: [SOURCE CODE]


Simple PHP breadcrumb navigation

PHP,Tutorials          Trackback

I was working on one application this morning and I needed breadcrumb links on the top. First thought that came to me was: ‘recursion’. That’s scary word, for memory eating functions, but at the same time that is easiest way to create breadcrumbs navigation. All you have to do is to create MySQL table, with these fields: (category_id, parent_id, category_name)
Top categories must have parent_id set to 0. And here goes that simple recursive printbreadcrumb function:

function printbreadcrumb($category_id) {
	$sql = "SELECT category_id, parent_id, category_name
				FROM categories
				WHERE category_id=$category_id";
	$result	= mysql_query($sql) or die('something is wrong here');
	$row = mysql_fetch_array($result);
	if ($row['parent_id']) { // if this node has parent
		printbreadcrumb($row['parent_id']); // make recursive call to this function
	echo $row['categories_name'].' > '; // print nodes one by one

You are calling this function like this: printbreadcrumb($category_id)

Some would say that this is slow script, because you’ll have same number of sql calls, as you have levels. What you can do is to change this function to work with arrays and to load that complete table in one array.


show PHP errors

htaccess,PHP          Trackback

Did you ever work on some php script, and all you get when you point your browser to that script was blank page? Just emptiness. You would expect error message at least. Probably your server is configured not to display and log errors.

Try to add these two lines to your .htaccess file:

php_flag display_errors on
php_value error_reporting 7

That can solve your problem