<body><script type="text/javascript"> function setAttributeOnload(object, attribute, val) { if(window.addEventListener) { window.addEventListener('load', function(){ object[attribute] = val; }, false); } else { window.attachEvent('onload', function(){ object[attribute] = val; }); } } </script> <div id="navbar-iframe-container"></div> <script type="text/javascript" src="https://apis.google.com/js/platform.js"></script> <script type="text/javascript"> gapi.load("gapi.iframes:gapi.iframes.style.bubble", function() { if (gapi.iframes && gapi.iframes.getContext) { gapi.iframes.getContext().openChild({ url: 'https://www.blogger.com/navbar.g?targetBlogID\x3d6568480304078693241\x26blogName\x3dTips+\x27n\x27+Tricks+%26+Currents+Affairs\x26publishMode\x3dPUBLISH_MODE_BLOGSPOT\x26navbarType\x3dBLUE\x26layoutType\x3dCLASSIC\x26searchRoot\x3dhttps://tipsatoz.blogspot.com/search\x26blogLocale\x3den\x26v\x3d2\x26homepageUrl\x3dhttp://tipsatoz.blogspot.com/\x26vt\x3d7125596445736072490', where: document.getElementById("navbar-iframe-container"), id: "navbar-iframe" }); } }); </script>

PHP Tips & Tricks - A Picture Upload

Wednesday, April 23, 2008
One of the harder tasks to writing a PHP application (or any program) is to know how to break down the processes into smaller, more manageable pieces.

Many PHP files start out as simple little scripts, but after time (and many feature requests) they tend to grow into very long, complicated programs. It may even become difficult to determine exactly what the program is doing at any given point. It is at this point where breaking it down will add a bit of clarity to the program flow.

Fortunately, PHP provides a few simple, yet elegant methods for breaking out code into separate pieces. The first one we will concentrate on is PHP's very powerful include() statement.

Let us start with an often requested web application that will allow an end user to upload an image file to the web server and then give them a list of the images they have uploaded in the past.

Starting with first things first, we know that we will need a web form that has a file upload field and a submit button. And we know we will need some php code to actually handle the upload. Here is an example.

<?
//print_r($_POST);

if($_POST["action"] == "Upload Image")
{
unset($imagename);

if(!isset($_FILES) && isset($HTTP_POST_FILES))
$_FILES = $HTTP_POST_FILES;

if(!isset($_FILES['image_file']))
$error["image_file"] = "An image was not found.";


$imagename = basename($_FILES['image_file']['name']);
//echo $imagename;

if(empty($imagename))
$error["imagename"] = "The name of the image was not found.";

if(empty($error))
{
$newimage = "images/" . $imagename;
//echo $newimage;
$result = @move_uploaded_file($_FILES['image_file']['tmp_name'], $newimage);
if(empty($result))
$error["result"] = "There was an error moving the uploaded file.";
}

}

?>


<form method="POST" enctype="multipart/form-data" name="image_upload_form" action="<p><input type="file" name="image_file" size="20"></p>
<p><input type="submit" value="Upload Image" name="action"></p>
</form>

<?
if(is_array($error))
{
while(list($key, $val) = each($error))
{
echo $val;
echo "<br />\n";
}
}
?>


Without going into great detail, here is the basic low-down of the up-load.

The bulk of the php code only get activated if $_POST["submit"] is "Upload Image" and this only occurs if someone has pressed the submit button. The form actually submits back to itself by using the . Inside the HTML of the form, we will print out any error messages that may have occurred. This script also assumes that there exists a directory named "images" that is writable by the web server. You can usually accomplish this by ftping into your website and making a folder named "images" and changing the permissions of the folder to 777 or xxx or execute execute execute.

Now that we have our uploading script, it is time to take a step back and see if there is a way that we can make it more modular. Imagine a scenario where you are working with a team, and a designer who only knows HTML wants to modify the form. This could lead to a dangerous situation where he inadvertently changes some php code when he just wants to change some HTML.

Here is where php's include() comes in handy. include() lets you grab other files and php will automatically insert everything from that file at the exact place you invoke the include() statement. To accomplish this, one only has to type the file name in-between the parentheses of the statement. For example, include("myfile.txt");

If we separate out our one giant php script so that the html part is in another file and leave most of the php in the original script, the HTML designer can go into that file and not have to worry about changing code.

After putting in our include, the main file would appear like this...

<?
//print_r($_POST);

if($_POST["action"] == "Upload Image")
{
unset($imagename);

if(!isset($_FILES) && isset($HTTP_POST_FILES))
$_FILES = $HTTP_POST_FILES;

if(!isset($_FILES['image_file']))
$error["image_file"] = "An image was not found.";


$imagename = basename($_FILES['image_file']['name']);
//echo $imagename;

if(empty($imagename))
$error["imagename"] = "The name of the image was not found.";

if(empty($error))
{
$newimage = "images/" . $imagename;
//echo $newimage;
$result = @move_uploaded_file($_FILES['image_file']['tmp_name'], $newimage);
if(empty($result))
$error["result"] = "There was an error moving the uploaded file.";
}

}

include("upload_form.php");

if(is_array($error))
{
while(list($key, $val) = each($error))
{
echo $val;
echo "<br />\n";
}
}

?>


and our mostly HTML file would be this...


<form method="POST" enctype="multipart/form-data" name="image_upload_form" action="<?$_SERVER[">">
<p><input type="file" name="image_file" size="20"></p>
<p><input type="submit" value="Upload Image" name="action"></p>
</form>


The next step is to have something that actually shows what images are in the directory. We can write a quick script that will iteratively go through images directory and echo out what is there like this...

<?
$handle = @opendir("images");

if(!empty($handle))
{
while(false !== ($file = readdir($handle)))
{
if(is_file("images/" . $file))
echo '<img src="images/' . $file . '" /><br /><br />';
}
}

closedir($handle);
?>


Again, here is where the power of include comes in handy. We can provide a direct link to our list_images.php, but we can also just include it in our original upload.php This saves us from having to write it twice!

So at the bottom of our upload.php we can just include the include like this...

<?
//print_r($_POST);

if($_POST["action"] == "Upload Image")
{
unset($imagename);

if(!isset($_FILES) && isset($HTTP_POST_FILES))
$_FILES = $HTTP_POST_FILES;

if(!isset($_FILES['image_file']))
$error["image_file"] = "An image was not found.";


$imagename = basename($_FILES['image_file']['name']);
//echo $imagename;

if(empty($imagename))
$error["imagename"] = "The name of the image was not found.";

if(empty($error))
{
$newimage = "images/" . $imagename;
//echo $newimage;
$result = @move_uploaded_file($_FILES['image_file']['tmp_name'], $newimage);
if(empty($result))
$error["result"] = "There was an error moving the uploaded file.";
}

}

include("upload_form.php");

if(is_array($error))
{
while(list($key, $val) = each($error))
{
echo $val;
echo "<br />\n";
}
}

include("list_images.php");

?>

10 Effective Ways to Get More Blog Subscribers

Thursday, April 17, 2008

The question I seem to be getting over and over these days is…

How did you get 6,000 subscribers in 10 months?

The answer is simple—I value subscribers more than any other measure of blog success, such as page views or raw traffic. Subscribers are the life blood of a successful blog in my opinion, and frankly, I wish I had more of them. :)

OK, that may be a bit vague.

So here are 10 specific strategies you can begin to implement today and start getting more blog subscribers right away.

1. Make it easy and obvious


As I’ve said before in more detail, make your subscription options prominent, offer an email alternative to RSS, and ask for the subscription, preferably at the bottom of each post.

2. Be laser focused

Make sure that you are primarily focusing on a particular topic, and the more specialized that topic is, the better you’ll do. It’s also key to step back and evaluate whether there are enough prospective readers in your chosen niche. It’s better to be brutally honest with yourself than to toil away and end up disappointed.

3. Offer a bribe

Relax, it’s nothing illegal. It’s an ethical bribe, in the form of a free ebook, report, e-course or audio series. Typically this only works with email subscriptions tied to autoresponders, because you want to condition delivery of the bonus on subscription.

But here’s a nifty way to do it with RSS:

If you have a WordPress blog, use the free Feedvertising plugin to link to the download page for your free gift. Since Feedvertising links only show up in the feed (and not in the post), only feed subscribers will see the link and have access to the bonus.

4. Use viral ebooks

This is a spin on the ethical bribe strategy, but instead you let other people give away your PDF ebook or even bundle it for sale with other products. The PDF in turn promotes your blog. Check out this post to see how I bundled my free Viral Copy report with a book that spent several days at the top of the Amazon bestseller list.

5. Dedicated subscription landing page

Create a page that is dedicated to nothing more than obtaining a subscription, and drive traffic to it from your blog, AdWords, or really any other source you want. You can even put it on a unique URL, and add in the ethical bribe strategy to increase signups. For more information on doing this with AdWords, read this article, and then this one.

6. Become a guest blogger

Contributing content to someone else’s blog may seem crazy, but it’s a solid strategy to gain exposure for your own blog and build your subscriber base. Just make it very clear to the blog owner that you require a very brief byline at the end of the post, with a link back to your site. And make sure it’s original content, not something recycled off of your blog.

7. Start a podcast

Start a related podcast on your subject matter, and get it into iTunes and listed in the various podcasting directories. Mention your blog in every episode and the benefits of subscribing, and try to land some interviews with prominent players in your niche. Not only will you be opening up a new promotional channel, you’re also creating bonus content that can be reused as part of your ethical bribe campaign for new subscribers.

8. Post in forums

A tried and true technique since the earliest days of the Internet is to be a helpful, proactive participant in forums that are important in your niche. People will notice that you are offering yourself up to others, and will be more inclined to see what else you have to offer with your blog.

9. Networking

This is perhaps the most overlooked strategy for gaining traffic and subscribers. Don’t badger other bloggers for links, because it rarely works anymore. Find a way to help them with something, and then eventually work that initial graciousness into a business relationship and even friendship. There are real people behind these blogs, and they respond to good will just like people do offline.

10. Cross-promotional deals

Here’s another cool way to make use of the Feedvertising plugin for WordPress.

Find a blogger that publishes related, but non-competitive content. Work out a deal where you both promote each other in your RSS feeds, using Feedvertising. If one blog has way more subscribers than the other, work out a ratio deal. Since Feedvertising allows you to create up to six rotating links, the smaller blog would promote the other blog continuously, while the larger blog would reserve one slot for the smaller blog, and use the other slots for other cross-promotion deals, affiliate links, or sponsor ads.

So there you have it, with one additional word of caution.

All of the above presupposes that you are producing the best content you can. If you honestly cannot say that you are doing your best work content-wise, start there. But afterwards, using some or all of the above will definitely increase your subscriber count.