Tagged: file

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)

Find (search) and replace text from command line in multiple files (Linu

- by admin

Another (and more easy) way to change text in multiple files is to use grep:
grep -lr -e 'oldtext' * | xargs sed -i 's/oldtext/newtext/g'

or to use PERL:
perl -p -i -e ’s/oldtext/newtext/g’ *

 

Find (search) and replace text from command line in multiple files (Linu

- by admin

Just after I posted this article the second more easy solution has been found. Here it is:

Find (search) and replace text from command line in multiple files (Linux) #2

When you are working on the Linux command line and you come across a large file or a large number of files in which you need to replace a certain text with another, finding and pasting over each instance of the text can be a bit time consuming. Well, worry no more. Linux has just the solution for you. Here’s a way to find and replace a string of text in one or more files automatically.

For the purpose of this exercise we will use a Linux command line tool called “sed”.  ”sed” is a very powerful and versatile tool, and a lot can be written about its capabilities. We are using a very limited aspect of “sed” here. I would definitely recommend that you read up a little more on “sed” if you find this aspect of it interesting.

We are going to use the following syntax to find and replace a string of text in a file:
# sed -i 's/[orginal_text]/[new_text]/' filename.txt

Say you have a file called “database.txt” with numerous instances of the IP address of your database server in it. You have just switched to a new database server and need to update it with the new server’s IP address. The old IP address is 192.168.1.16 and the new one is 192.168.1.22. Here’s how you go about it:
# cat database.txt
LOCAL_DATABASE = 192.168.1.16
LOCAL_DIR = /home/calvin/
PROD_DB = 192.168.1.16

# sed -i 's/192.168.1.16/192.168.1.22/g' database.txt
# cat database.txt
LOCAL_DATABASE = 192.168.1.22
LOCAL_DIR = /home/calvin/
PROD_DB = 192.168.1.22

Now open the file “database.inc” and check to see if the new IP address has taken place of your old one. Here’s the breakup of the above command. First you call the “sed” command. Then you pass it the parameter “-s” which stands for “in place of”. Now we use a little bit of regular expressions, commonly known as “regex”  for the next bit. The “s” in the quoted string stands for “substitute”, and the “g” at the end stands for “global”. Between them they result in a “global substitution of the the string of text you place in between them.

You can optionally skip the “g” at the end. This means that the substitution will not be global, which practically translates to the substitution of only the first instance of the string in a line. So if you had a line with multiple instances of the text you are trying to replace, here’s what will happen
# cat database.txt
LOCAL_DATABASE = 192.168.1.16
LOCAL_DIR = /home/calvin/
PROD_DB = 192.168.1.16, 192.168.1.16

# sed -i 's/192.168.1.16/192.168.1.22/' database.txt
# cat database.txt
LOCAL_DATABASE = 192.168.1.22
LOCAL_DIR = /home/calvin/
PROD_DB = 192.168.1.22, 192.168.1.16

Here comes the real magic. Now, say you want to change a string of text not just in a single file, but in the entire directory you are in. There are a number of text files in which you need to find and replace the “wine” with “champagne”.
# find . -maxdepth 1 -name "*.txt" -type f -exec sed -i 's/wine/champagne/' {} \

We use the find command to get a list of all the text files in the current directory. That’s the “find . -maxdepth 1 -name “*.txt” -type f” part. “find . maxdepth 1″ tell the computer to look in the current directory and go no deeper than the current directory. The ‘-name  ”*.txt”‘ part tells find to only list files with the extension of “.txt”. Then the “-type f” section specifies that “find” should only pick exactly matching files. Finally the “-exec” part tells “find” to execute the command that follows, which, in this case, is the “sed” command to replace the text – “sed -i ‘s/wine/champagne/’ {} \”.

I realize that the above command seems complicated. However, once you use it a little bit you will realize that it is probably worth noting it down and using it. Now try changing a string of text in multiple levels of directories.

Uncaught exception 'Zend_Mail_Transport_Exception' with message 'Unable

- by admin

Error:

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

FIX:

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 !

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

output:
/www/htdocs/inc
lib.inc.php
php
lib.inc

« All tags