Programming

Counting Rows in textarea

I wrote a snippet which counts the number of rows of text in an HTML textarea object. Its very useful if you need to display data outside of the textarea box.

I’ve got an example of a form and the PHP function it calls.

Filename: t.php



Filename: t_handler.php

$textarea = $_POST['ta'];

$check = explode("\n", $textarea);
$lines = count($check);

echo "Lines: $lines";

Preventing File Locking in E-mail Attachment

I’m trying to attach a file to an e-mail that I’m sending using the SmtpClient class (from System.Net.Mail) using C# for ASP.NET. Everything works correctly, but the problem is that the file that was uploaded gets locked in the folder by Windows and I can’t delete it afterwards.

This is the code I used to attach a file to an e-mail:

sHtmlConfirmation = "D:\\ftpsite\\from_web\\" + sFolder + "\\" + sFolder + ".htm";
Attachment data = new Attachment (sHtmlConfirmation);
Email.Attachments.Add (data);

I needed a solution to prevent Windows from locking a file that it uses, so I turned to the MSDN Forums for help. James Curran posted a working solution. He suggested employing the using statement to dispose the MailMessage object after the mail has been sent.

The code which fixes the problem is:

using (MailMessage Email = new MailMessage ())
{
// setup message details here
}

After the mail was sent, I was able to move the folder where the attached file was used.

Passing asp:FileUpload Data for Processing on Another Page

I’ve been trying to write code for a user to select a file for downloading using the FileUpload control (in ASP.NET), and then passing that information to another page that has the code to download the file from the user. After some searching, I found a Visual Basic solution that used DirectCast.

DirectCast is a type conversion operation in Visual Basic .NET which is based on inheritance or implementation. Its better to use this than CType when converting objects.

When converting VB code to C#, there is no DirectCast operation available, but standard casting can be used instead. In the following example, I’m trying to pass the contents of a FileUPload control in an ASP.NET page to another web page for processing. Its passed to the other page through a Session variable:


Session ["FileUpload"] = FileUpload1;
Response.Redirect ("Upload_2.aspx");

I found code to Visual Basic code to use the FileUpload object, but it uses DirectCast. The following shows how the code was converted to the equivalent in C#.

Visual Basic

Dim objFileUpload As FileUpload = DirectCast(Session("FileUpload"),FileUpload)

C#

FileUpload objFileUpload = new FileUpload ();
objFileUpload = (FileUpload) Session ["FileUpload"];

Then I use the code below to download the file, which is typical code for this control. There is no error checking in this example.

if (objFileUpload.HasFile)
objFileUpload.SaveAs ("D:\\uploads\\" + objFileUpload.FileName);

Creating a Safety Net for WordPress Plugin Integrations

Installing a WordPress plugin can be somewhat stressful for those who are unfamiliar with web design or programming. There are many plugins which require you to go into the PHP source code of a WordPress module and then add the function to call the plugin. Normally, the instructions just tell you to enter the function name. This works well until you deactivate or uninstall the plugin. You need to remove the PHP code you added to your blog or you’ll get an error message, or worse, the page may fail to render!

One way to get around this is to use function_exists () with the plugin. This is a PHP function which will return a true value if the function that its trying to run exists. This may seem confusing, but the following code will make it easier to understand.

if (function_exists ('wp_plugin'))
wp_plugin ();

This checks if the function to call the plugin (which is named wp_plugin) is active (or installed). If it is, then it will run the function wp_plugin (). If the function could not be found, then no command will be run and the next statement after wp_plugin () will be executed.

You need to ensure that you enter ‘wp_plugin’ and not ‘wp_plugin ()’ in function_exists for it to work properly.

If you wrap the plugins that you install this way, you can easily turn on and off the plugin and still have a fully functional blog.

Zend Core: PHP Makes It Into Prime Time

PHP has been steadily growing in usage since the start of the millennium. It has overtaken Perl as the primary programming language for web applications and will likely dominate this area for many years to come. The language is becoming increasingly common in the corporate world in spite of the initial suspicion many had over it being a free and open source application.

One of the advantages of PHP is the ease in learning it. Enough can be picked up in a few days to create database driven web sites. The only real issue is that PHP can be difficult to install and setup, and also in installing a database server to go with it. This is easier with Linux because the typical installation includes PHP and a database, such as MySQL. If your using Windows, PHP and a database server needs to be installed manually.

Zend has provided a quick way to setup a PHP development environment with Zend Core. This is a collection of applications that installs and configures PHP. It can also install MySQL and add support to several other databases (DB2, Informix, Oracle, and SQL Server).

image

In Windows, PHP can be setup to work with IIS or Apache can be installed instead. Working with MySQL on the command line can be a challenge, so phpMyAdmin can be setup as well. For programmers willing to learn a robust PHP library, Zend Framework is available.

Zend Core can be installed in Windows, 32/64-bit Linux, and Macintosh OS X. This is the simplest way to create a PHP development environment which is not just for novices, but for experienced users as well. There isn’t any tinkering to make PHP or the applications it uses work properly. The advantage of using Zend Core is that it will update PHP and any of the applications that it installs. You no longer have to visit the MySQL site, for instance, to check if a new version of the database is available.

Zend Core is a free download, but requires registration with Zend first.