Sunday, February 25, 2007

Flash-PHP file downloads counter

If you want to keep a record and show how many times has a file been downloaded, you can use Flash, php and a simple txt file to hold the number of downloads.

When I finished my Firefox Screensaver, I showed it first at the spreadfirefox community, of course, without any way to count the downloads.
When I finally had the time to show it here on my blog, I was looking for a way to count the number of downloads but had the limitation that on this blog, as far as I know, is only possible to write "front-end" scripts and, as you know, without a server is not possible to have any downloads counter as there is no way to store any data.

However, using flash as front-end I can pull any data from anywhere behind the scenes and show it back on the same flash.

Then, if in case you want to create a file downloads counter, just follow these steps:

First of all, we will need 4 files,
- the file we want to be downloaded, usually a compressed .zip file
- theCounter.txt file which will store the downloads number
- the flash file to show the number of downloads and to allow people to download the file
- a php file which behind the scenes will manage the downloads counter and point to the right link to the file to be downloaded.

Let's start with the flash file:

- Open a new flash document
- Put on the stage all the graphics you need and also a dinamic text field which will show the number of downloads with an instance name of "downloads" as shown in the picture:



- Select the background image and press F8 to make it a movieclip, and put it as instance name "downloads_btn"



- On a different layer called "actions", write the following actionscript:
// --- Function to load the stored number ---
function loadingVars():Void {
var my_lv:LoadVars = new LoadVars();
my_lv.onLoad = function(success:Boolean) {
if (success) {
downloads.text = "( "+my_lv.downloads+" downloads )";
} else {
trace("Error loading/parsing LoadVars.");
}
};
my_lv.load("theCounter.txt?num="+random(999));
}
// --- Calling the function ---
loadingVars();
// --- Button actions ---
downloads_btn.onPress = function() {
getURL("downloads.php");
};
downloads_btn.onRelease = function() {
loadingVars();
};
What is happening here is, first we are loading the variable stored in theCounter.txt file and to avoid the cache memory and load a fresh value everytime, we add a ?num=random(999) that gives us a unique value when calling the same file, that way the browser thinks you are loading a different file...
When pressing the button, we are calling a php which is the one in charge of the download and counter system.
On releasing the button, we load the variable again and as the file we are offering for download has been requested another time, the value of the variable loaded is bigger by one.
Save the file and publish it to have a swf file.

Now let's see the php:

- Open dreamweaver or any other php editor; even notepad can do the job.
- Write the following code:
I'm sorry this is an image but as I said before, blogger doesn't like back-end scripts and stripped it out when I wrote it here...

We are first opening theCounter.txt file, checking what it has inside and from it, the value of "donwloads" is stored in a variable called "count", then add one to this value, write the variable and the new value of it back to the txt file and closes it. Next we give the real location of the zip file and redirect the browser to it.

The .txt file has only one line inside:
downloads=X
where X is the current number of downloads, so the first time it should be 0 (zero).
That's it!
just upload everything to a server with php support and embed your swf where convenient.
For example, I embeded my swf on this post:



Try it, click on the button, download and install the screensaver, if you don't like you can always uninstall it :)

salut!

Saturday, February 24, 2007

Submit to StumbleUpon flash button

I've just uploaded a new WebWidget to Widgipedia: "Submit to StumbleUpon flash button"
Integrate your site with Stumbleupon; Adding this widget allows people to submit a story from your blog or any other personal website to Stumbleupon for others to discover.
StumbleUpon is a great way to find websites that are really interesting for you as they have been submitted to the database into different categories, you subscribe only to the categories you like. It's so addictive that my del.icio.us account is growing thanks to the cool stuff I discover when I "Stumble".

If you already have an account with them and have a blog, why not adding this widget:



Tested already by submitting this page to StumbleUpon!
Get it from Widgipedia.

Salut!

Friday, February 23, 2007

LFPUG February 07 meeting


lfpug0207-01, originally uploaded by Ernesto Quezada.

Alias Cummins presenting at the London Flash Platform Users Group meeting last night.

We had two great presentations yesterday night; the first one, Alias showed us some cool stuff he has done experimenting with filters, bitmaps, and some other classes. He explained us how all this special filters and stuff comes from the same root: Perlin noise. In the end he showed us some code that looks simple but complex at the same time, using Maths and other crazy stuff.

The second presentation was in hands of Dan Thomas on Apollo. After explaining us some things about the legendary Apollo soon to be released (hopefuly), there was a debate about how good is Apollo and why should anyone choose it instead of for example JAVA? I think Emmanuel raised a good point when saying we are following too much Adobes press that even we are doing much more marketing than them. Very true!
Anyway, for desktop applications, I preffer to continue using MDM Zinc, a much more mature tool to develop desktop RIAs.

And the best part of the meeting, for me of course, I was AGAIN the lucky winner of a book!
This time is Richard Leggett's FLASH APPLICATIONS FOR MOBILE DEVICES that of course I started reading already.

Thanks Richard for the personalized autograph!

Salut!

Sunday, February 18, 2007

Game: Blitzkrieg - Lightening War



I was following a tutorial on Creating a Vertical Shooter that I wrote a while back (more than a year ago)
- creating-vertical-shooter-part-I
- creating-vertical-shooter-part-II
- creating-vertical-shooter-part-III
When I wrote it, I made Pawel and the Flying Dragons; this time I made a new game:
Blitzkrieg - Lightening War

The actionscript I used is basically the same, just changed the graphics and added a couple of things like a shotgun and an enemys bomb. This is a probe that any script can be re-used and it has to be made thinking about it, some basic rules in Object Oriented Programming (Polymorphism and Inheritance).

The scoretable is almost empty so, go for it ;)

PD: Don't ask me what's that of a tank shooting anti-aircraft, hey! at least this time there are no flying dragons...

Sunday, February 11, 2007

Take back the web & the desktop!

Firefox ScreenSaver





To help to spread the SFx community and Mozilla Firefox at the same time, I've done a ScreenSaver that shows the Firefox downloads in a different way.

The idea behind is to reach 1 Billion downloads (now is a bit more of a quarter of billion) and show it as a full Firefox logo.
So far if you leave the screensaver running, it shows just 1/3 of the logo as only 1/3 of a billion has been downloaded as per today (11/Feb/2007) and this third of billion is slowly uncovered by squares (the data collected is transformed into squares, each square represents 200,000 downloads)...

well, better if you check it by yourself :)




salut!

Saturday, February 10, 2007

Flash Counter Widget goes coloured



The Flash Web Counter
widget has found a wide acceptance in the Widgetbox users community.
Shown in the top rated list as the highest rated is a big honour for me and from the 10 widgets I have uploaded to Widgetbox, is by far the one with more subscriptions.
One of its goodness is the small control panel where you can reset the counter to any convenient number and from today its possible as well change the counters background colour!

There is a number of files involved in the process for this widget to work:
- The flash counter itself
- An individual text file for each counter to store number of visits and selected colour (one text file per user)
- A php file that gets the current value of the counter and adds one more visit when required
- The Flash Control Panel
- A php file to get and set the values of both colour and number of visits.

All these 5 files have been updated today in order to have the option of changing the counters background colour. I know it would have been better to have a MySQL database instead of the hundreds of single text files I have currently but my hosting plan is very basic...

Anyway, don't be shy and leave your comments-feedback here ;)

Thursday, February 01, 2007

Widget: Thunderbird Contact Form

I just uploaded a new web widget on Widgetbox.
from its page:
A simple contact form that promotes Mozilla Thunderbird. Let your visitors contact you giving them the option of sending you messages or get your contact details, ie mobile phone number and email address.
If you want to contact me, test it from here but as well I invite you to go and test it from Widgetbox


Available from:
http://www.widgetbox.com/widget/thunderbird-contact-form
Yes, I use Mozilla Thunderbird and this is my tribute to it.

Salut