Tagged: php

Cannot send mail in Mac OS X 10.8 Mountain Lion

- by admin

There is an error while sending mail in Mac OS X Mountain Lion 10.8:

send-mail: fatal: chdir /Library/Server/Mail/Data/spool: No such file or directory

To fix just run in terminal:
sudo mkdir -p /Library/Server/Mail/Data/spool
sudo /usr/sbin/postfix set-permissions
sudo /usr/sbin/postfix start

The solution also fixes an error in 10.8 where the fax service has stopped sending emails after the 10.8 upgrade.

Another Postfix Mac OS X issue solution can be found here: Mac OS, MySQL: No such file or directory (trying to connect via unix:///var/mysql/mysql.sock)

Date format validation in PHP

- by admin

In case of date format checking in PHP just use this function:
function checkDateFormat($date)
  //match the format of the date
  if (preg_match ("/^([0-9]{4})-([0-9]{2})-([0-9]{2})$/", $date, $parts))
    //check weather the date is valid of not
          return true;
         return false;
    return false;

echo checkDateFormat("2008-02-29"); //return true
echo checkDateFormat("2007-02-29"); //return false

Mac OS, MySQL: No such file or directory (trying to connect via unix:///

- by admin

Error on attempt to connect locally to MySQL server DB with PHP on MacOS X Lion 10.7:

No such file or directory (trying to connect via unix:///var/mysql/mysql.sock)

Solution 1:

instead of localhost use
mysql_connect ('', $user, $password);

Solution 2:

In /etc/php.ini change
pdo_mysql.default_socket = /var/mysql/mysql.sock
mysql.default_socket = /var/mysql/mysql.sock
mysqli.default_socket = /var/mysql/mysql.sock

pdo_mysql.default_socket = /tmp/mysql.sock
mysql.default_socket = /tmp/mysql.sock
mysqli.default_socket = /tmp/mysql.sock

Do not forget to restart Apache after update :-)

FYI: If no /etc/php.ini found just copy /etc/php.ini.default to /etc/php.ini

Uncaught exception 'Zend_Mail_Transport_Exception' with message 'Unable

- by admin


Fatal error: Uncaught exception 'Zend_Mail_Transport_Exception' with message 'Unable to send mail. ' in /.........../Zend/Zend/Mail/Transport/Sendmail.php:137

Postfix is running, PHP can connect to Postfix.

We have got In Apache error_log:
postdrop: warning: uid=48: File too large


The default max message size for Postfix is 10240000 bytes. Thus we do:
% /usr/sbin/postconf -e message_size_limit=XXXXXXXXXXX

where XXXXXXXXXX is the new limit.

And then restart Postfix.

FYI: Related issues:

The the default value for a mailbox size according to Postfix is 51200000 bytes. It may be changed like this:
% postconf -e mailbox_size_limit=0

And then restart Postfix.

The settings for all the size related parameters (in main.cf) can be listed like this:
# postconf -d | grep size

Sample output:
berkeley_db_create_buffer_size = 16777216
berkeley_db_read_buffer_size = 131072
body_checks_size_limit = 51200
bounce_size_limit = 50000
header_size_limit = 102400
mailbox_size_limit = 51200000
message_size_limit = 10240000

Voila !

Convert a string into an URL safe address

- by admin

 * Convert a string into a url safe address.
 * @param string $unformatted
 * @return string
public function formatURL($unformatted) {

    $url = strtolower(trim($unformatted));

    //replace accent characters, forien languages
    $search = array('À', 'Á', 'Â', 'Ã', 'Ä', 'Å', 'Æ', 'Ç', 'È', 'É', 'Ê', 'Ë', 'Ì', 'Í', 'Î', 'Ï', 'Ð', 'Ñ', 'Ò', 'Ó', 'Ô', 'Õ', 'Ö', 'Ø', 'Ù', 'Ú', 'Û', 'Ü', 'Ý', 'ß', 'à', 'á', 'â', 'ã', 'ä', 'å', 'æ', 'ç', 'è', 'é', 'ê', 'ë', 'ì', 'í', 'î', 'ï', 'ñ', 'ò', 'ó', 'ô', 'õ', 'ö', 'ø', 'ù', 'ú', 'û', 'ü', 'ý', 'ÿ', 'Ā', 'ā', 'Ă', 'ă', 'Ą', 'ą', 'Ć', 'ć', 'Ĉ', 'ĉ', 'Ċ', 'ċ', 'Č', 'č', 'Ď', 'ď', 'Đ', 'đ', 'Ē', 'ē', 'Ĕ', 'ĕ', 'Ė', 'ė', 'Ę', 'ę', 'Ě', 'ě', 'Ĝ', 'ĝ', 'Ğ', 'ğ', 'Ġ', 'ġ', 'Ģ', 'ģ', 'Ĥ', 'ĥ', 'Ħ', 'ħ', 'Ĩ', 'ĩ', 'Ī', 'ī', 'Ĭ', 'ĭ', 'Į', 'į', 'İ', 'ı', 'IJ', 'ij', 'Ĵ', 'ĵ', 'Ķ', 'ķ', 'Ĺ', 'ĺ', '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I', 'i', 'I', 'i', 'I', 'i', 'I', 'i', 'IJ', 'ij', 'J', 'j', 'K', 'k', 'L', 'l', 'L', 'l', 'L', 'l', 'L', 'l', 'l', 'l', 'N', 'n', 'N', 'n', 'N', 'n', 'n', 'O', 'o', 'O', 'o', 'O', 'o', 'OE', 'oe', 'R', 'r', 'R', 'r', 'R', 'r', 'S', 's', 'S', 's', 'S', 's', 'S', 's', 'T', 't', 'T', 't', 'T', 't', 'U', 'u', 'U', 'u', 'U', 'u', 'U', 'u', 'U', 'u', 'U', 'u', 'W', 'w', 'Y', 'y', 'Y', 'Z', 'z', 'Z', 'z', 'Z', 'z', 's', 'f', 'O', 'o', 'U', 'u', 'A', 'a', 'I', 'i', 'O', 'o', 'U', 'u', 'U', 'u', 'U', 'u', 'U', 'u', 'U', 'u', 'A', 'a', 'AE', 'ae', 'O', 'o');
    $url = str_replace($search, $replace, $url);

    //replace common characters
    $search = array('&', '£', '$');
    $replace = array('and', 'pounds', 'dollars');
    $url= str_replace($search, $replace, $url);

    // remove - for spaces and union characters
    $find = array(' ', '&', '\r\n', '\n', '+', ',', '//');
    $url = str_replace($find, '-', $url);

    //delete and replace rest of special chars
    $find = array('/[^a-z0-9\-<>]/', '/[\-]+/', '/<[^>]*>/');
    $replace = array('', '-', '');
    $uri = preg_replace($find, $replace, $url);

    return $uri;

Voila !

Scanning for Viruses Using PHP and ClamAV

- by admin

Here what I found and like most.

PHP Code for directory scanning:
    // Set the allowed types for reading and upload. 
    $types = array ('jpg', 'jpeg', 'txt'); 
    // Start a variable. 
    $dir_files = array(); 
    // If it is a directory add all the files. 
    if (is_dir ($dir)) { 
        // Open the directory. 
        if ($handle = opendir($dir)) { 
            // Read the file names of all the files available. 
            while (false !== ($file = readdir($handle))) { 
                // Make sure the file is not this directory or its parent and not the .DS_Store file. 
                if ($file != "." && $file != ".." && $file != '.DS_Store') { 
                    // Get the file parts. 
                    $file_parts  = pathinfo($file); 
                    // Make sure the extension is allowed. 
                    if (in_array(strtolower ($file_parts['extension']),$types)) { 
                        // Add the file to the array. 
                        $dir_files[] = array ('original_name'=>$file, 'type'=>$file_parts['extension']); 
            // Close the handle. 
            closedir ($handle); 

    // If any files exist in the upload directory check them for viruses. 
    if (count ($dir_files) > 0) { 
        // Get the dir and prepare it for the command line. 
        $real_path = realpath ($dir); 
        $safe_path = escapeshellarg($real_path); 
        // Set the variables for the cmd. 
        $return = -1; 
        $out =''; 
        $cmd = '/usr/local/clamXav/bin/clamscan ' . $safe_path; 
        // Execute the cmd. 
        exec ($cmd, $out, $return); 
        // If a virus is found loop through each of the files and delete the virus, write the user a message, and add them to a db table. 
        if ($return != 0) { 
            // Loop through the files. 
            foreach ($dir_files as $k=>$v) { 
                // Get the dir and prepare it for the command line. 
                $real_path = realpath ($dir . $v['original_name']); 
                $safe_path = escapeshellarg($real_path); 
                // Reset the values. 
                $return = -1; 
                $out =''; 
                $cmd = '/usr/local/clamXav/bin/clamscan ' . $safe_path; 
                // Execute the command. 
                exec ($cmd, $out, $return); 
                // If the file is clean do nothing. 
                if ($return == 0){} 
                // If the file contains a virus remove it and add a note to the db. 
                else if ($return == 1) { 
                    // Delete the file. 
                    unlink ($dir . $v['original_name']); 
                    // Unset the file from the records. 
                    unset ($dir_files[$k]); 
                    // Notify the user. 
                    $message .= "The file {$v['original_name']} contained a known virus.  It has been deleted from the server.<br />"; 
                    $message_class = 'error'; 
                    // Add the user who uploaded the file to the db and a time and date. 
                    // Query the db to record who uploaded the file, not necessary but fun info to have. 
                else { 
                    // Delete the file. 
                    unlink ($dir . $v['original_name']); 
                    // Unset the file from the records. 
                    unset ($dir_files[$k]); 
                    // Notify the user. 
                    $message .= "The file {$v['original_name']} caused an unknown error and was removed from the server.<br />"; 
                    $message_class = 'minor_error'; 

PHP Get File Extension

- by admin

Just some examples:
$filename = 'sample.gif';

// 1. The "explode/end" approach
$ext = end(explode('.', $filename));

// 2. The "strrchr" approach
$ext = substr(strrchr($filename, '.'), 1);

// 3. The "strrpos" approach
$ext = substr($filename, strrpos($filename, '.') + 1);

// 4. The "preg_replace" approach
$ext = preg_replace('/^.*\.([^.]+)$/D', '$1', $filename);

// 5. The "never use this" approach
// From: http://php.about.com/od/finishedphp1/qt/file_ext_PHP.htm
$exts = split("[/\\.]", $filename);
$n = count($exts)-1;
$ext = $exts[$n];

And the last but not the least:
$path_parts = pathinfo('/www/htdocs/inc/lib.inc.php');
echo $path_parts['dirname'], "\n";
echo $path_parts['basename'], "\n";
echo $path_parts['extension'], "\n";
echo $path_parts['filename'], "\n"; // since PHP 5.2.0


PHP: explode() or split()?

- by admin

Actually explode() isn’t the same as split().

The biggest difference is that explode() takes in parameters a delimiter to split by, while split() takes a regular expression. This means that explode() runs faster. And even more, the PHP documentation says that preg_split() is faster than split(), so really there isn’t much of a reason to use split() at all.

split() is not an alias of explode() while join() is an alias of implode().

And finally:

split() function has been DEPRECATED as of PHP 5.3.0. Relying on this feature is highly discouraged.

« All tags