Sunday, October 28, 2007

Store images on disk or database?

Should you store uploaded images on disk or database?



I store my uploaded images on disk, and not in database. Why? One thing is that most hosting providers limit the mysql database. So if you have a large number of images, the database won't be enough for you. In database I keep the path and the filename, and the file is kept on the disk.
Also, having the images stored on the disk, you can access them very easy with ftp clients for example. Showing the image in the browser, does not affect database server's performance, having the image stored on the disk.

Thursday, October 18, 2007

Get number of characters in Javascript

How to count number of characters in a string in Javascript

length javascript's function returns the length (number of characters) of a given string.

For example:



var mystring = 'test string';

alert( mystring.length ) ;


This will alert a window with the number of characters of mystring variable.

If you want to get number of characters in a textfield using javascript, that you need to call the same function, length this way:


alert( document.yourform.yourfieldname.value.length );

Tuesday, October 16, 2007

PHP MySQL Calendar Class

PHP MySQL Calendar Class



PEAR has this great calendar class . There is no need for you to waste your time by building a calendar class, this is a great one. Documentation for this class can be found here.

Some working examples of this calendar class written in php and mysql can be found here. As you can see it is really easy to work with this class, and it is well documented. You can easily extend it and use it for your own needs.

Javascript open centered popup window

How to open a centered pop-up window in Javascript


Here is a Javascript function that allows you to open a centered window, no matter what resolution the visitor uses.

function OpenCenteredWindow( url, width, height )
{
var left = Math.floor( (screen.width - width) / 2);
var top = Math.floor( (screen.height - height) / 2);
var winParms = "top=" + top + ",left=" + left + ",height=" + height + ",width=" + width;
winParms += ",toolbar=no,status=no,scrollbars=yes,menubar=no,resizable=no,location=no,directories=no";
var win = window.open(url, '', winParms);
if (parseInt(navigator.appVersion) >= 4) { win.window.focus(); }
return win;
}

There are three parameters:
  • url - the address you want to open in the new window
  • width - the width of the new popup window
  • height - the height of the new centered window
Also you can set what parameters the new window will have: toolbar, status, scrollbars, menubar, resizable, location, directories.

Change upload file size apache

How to change the PHP maximum upload file size



The default maximum upload size for files is 2MB. If you have access to .htaccess files, you can change the maximum upload size from there. Here is what you should write in .htaccess files for apache:

php_value max_execution_time 1200
php_value memory_limit 30M
php_value post_max_size 10M
php_value upload_max_filesize 10M

In the first line you increase maximum execution time from 30 seconds (default) to 1200 seconds. This way if the file is large, you won't get a timeout browser error.
In the second line you increase memory limit to 30MB.
The third line increases post maximum size to 10MB.
And the last line increases upload maximum filesize to 10MB.

Friday, October 12, 2007

Upload and image manipulation class

So you want to upload your files, but also validate extensions, size. You can use PEAR's HTTP_Upload class for that.

And then if you want to only upload images, and create thumbnails, resize images or crop the uploaded images, you can use the above upload class and this great Thumbnailer Class

This two classes make a powerful tool for uploading ,validating, resizing, cropping, rotating images or pictures.

PEAR MDB2 get array rows

This tip is for those who use PEAR MDB2 as your database abstraction layer. There are two nice methods, that are not in documentation, queryRow($query) and queryAll($query).

queryRow($query) - returns an associative array for one record
queryAll($query) - return an associative array for all found records

So for those who like to use arrays (like myself) you don't have to use fetchRow() anymore.

Documentation for this class can be found here.

Tuesday, October 9, 2007

Permanent redirect to www domain

You have a domain anydomain.com, and you want a permanent redirect (301) to www.anydomain.com. You can do this using a .htaccess file. Here it is:

RewriteEngine on
RewriteCond %{HTTP_HOST}!^www.anydomain.com
RewriteRule ^(.*)$ http://www.anydomain.com/$1 [R=301,L]

So if someone types anydomain.com, he will be redirected to www.anydomain.com. easy.

Javascript change element visibility

Want to change an element visibility from javascript. Here is an example.

document.getElementById("your_element_id").style.visibility = "hidden";
document.getElementById("your_element_id").style.visibility = "visible";

As you can see the visibility for an element can be hidden or visible. Make sure you have set an ID for your element.

Monday, October 8, 2007

MySQL get date from string

Let's say you have a string, like '18-Sep-2007' and you want to format it in date format %Y-%m-%d. You can use STR_TO_DATE function. First parameter is your string, and the second parameter is the format of your string, in this case %d-%b-%Y. You can see the format values here:
http://dev.mysql.com/doc/refman/5.0/en/date-and-time-functions.html#function_date-format

So STR_TO_DATE('18-Sep-2007','%d-%b-%Y') = 2007-09-18

You can use this result to compare dates, to order dates, or other date operations.

Saturday, October 6, 2007

PHP useful classes

Here is a list of PHP tools or classes I use to develop my projects.

PHP MySQL Blog

Hello,

Here I will post tips and tricks about PHP and MySQL. Everything I will find useful you will find here, so you won't have to search lots of pages on the internet. Also if you have any question please post a message, and I will try to help you.