Skip to content

Angie's Techno Stuff

Programming notes

To download a pdf file is very simple, and can be done using a few lines of code such as:

header('Content-disposition: attachment; filename=file1.pdf');
header('Content-type: application/pdf');
readfile('file1.pdf');

This code can be put into a php file, e.g. download.php and used in the webpage as follows:

<a href="download.php">File to download</a>

Recently I needed to download other file types and developed the following code. I use a file object (class) which is instantiated when the fileManager is called.
I found that the files I downloaded were getting corrupted, and fixed the problem with ob_clean() and flush()

Here’s my PHP code for downloading files:

First create a file class:

<?php
/*
 File: File.class.php
 Description: File Management Object to Download Document 
 Author: AS June 2012
 */
class File {

    // File Name 
    private $file;

    //Mime/Type 
    private $type;

    
    // file object Constructor 
    public function File($f_name) {
	    $this->file = $f_name;
        $this->type = $this->setMimeType($f_name);
        $this->download_file();
        return;
    }

	//set mime type
	private function setMimeType($filename){
		$extension = strtolower(substr(strrchr($filename, '.'), 1));
		switch($extension) {
			case "pdf": $type="application/pdf"; break;
			case "pptx": $type="application/vnd.openxmlformats-officedocument.presentationml.presentation"; break;
			case "doc": $type="application/msword"; break;
			case "docx": $type="application/vnd.openxmlformats-officedocument.wordprocessingml.document"; break;
			case "xlsm": $type="application/vnd.ms-excel.sheet.macroEnabled.12"; break;
			
			default: $type=0;
		}
	}
   
     // download the $file 
     private function download_file() {
		 
	   header("Content-Disposition: attachment; filename=".$this->file);
           header("Content-type: ".$this->type);
		
	   // next 2 lines of code required to prevent file corruption
	   ob_clean();
	   flush();

           readfile($this->file);
	   exit;
    }
}
?> 

Create another php file including the following code and call it fileManager.php. It will create the file objects and download them.


require_once( "File.class.php" );
//get values for variables  
$fileName = isset( $_GET["file"] ) ? $_GET["file"] : "";


//download file 
if(is_file($fileName)){
	$download = new File($fileName );
}

Then on web page include a link to your file for downloading e.g.

<a href="fileManager.php?file=filename.pdf">File to download</a>

Don’t forget to upload your file for downloading to the website.

Useful link for other mime types

Tags: , ,

Deploying a .war file to Apache Tomcat

Using the deployment section of tomcat Manager App (http://localhost:8080/manager/html  (User:admin, p/w admin)),  deploy by putting / in context path (for root), leave xml config file URL blank and put file path in third box e.g. C:Program files/…. (wherever you copy the .war file to).

e.g. enter the following for project called “DodgeyBros” into the deploy section :-
• Context path : /DodgeyBros
• XML config url: left blank
• WAR directory: C:\temp\DodgeyBros.war

Configuring Apache Tomcat on the server
Change default port from 8080 to 80. Do this by editing code in the server.xml file found in C:/Program Files/Apache Software Foundation/Tomcat 7.0/conf/server.xml to the following:

<Connector port=”80″ protocol=”HTTP/1.1″

connectionTimeout=”20000″

redirectPort=”8443″ />

Replacing the Default Root Directory
In the webapps folder, rename the ROOT folder to ROOT_default , (or delete it).
Put the .war file for your application in a temp folder on C drive e.g. C:/temp/DodgeyBros.war
Rename this file to ROOT.war
Deploy with Tomcat Manager as before using:
• Context path : /ROOT
• XML config URL : blank
• WAR directory: C:\temp\ROOT.war

To run a file in MySQL client:

mysql> SOURCE input_filename;
The file must be located on the client host where you are running mysql
E.g. If mysql is in C:\mysql directory and the script file is myfile.sql in C:\scripts directory, both of the following will work:
mysql> SOURCE C:\scripts\myfile.sql;
mysql> SOURCE ..\scripts\myfile.sql;

Other useful commands:

SHOW DATABASES;
USE database_name;
SHOW TABLES;
EXPLAIN table_name;
SELECT * FROM table_name;
EXIT;

Tags:

For my project I decided to use Subversion for version control and needed to have the repository of files hosted somewhere on the web so that I could share files with my team member. For this I have chosen Unfuddle.com because it meets the following criteria: it’s free and allows for 2 users. It also didn’t seem to have any bad reviews although I got a bit worried when the confirmation page had a problem loading onto my Firefox browser. Nevertheless, I got a confirmation email and haven’t had any problems since.

Here is a useful link for making Subversion hosting comparisons:   http://www.svnhostingcomparison.com/

Create a Repository:

First set up a free account with unfuddle.com.

In Unfuddle select “New Repository” from the Repositories tab to create a repository. Give it a title and abbreviation etc. as shown below:

Creating a new Subversion Repository in Unfuddle.com

Select Type: “Subversion” and then “Create Repository”. Keep a note of the URL for use in Netbeans later.

Download Subversion:

I downloaded Subversion 1.6.17  from http://sourceforge.net/projects/win32svn/  and found the installation quite straightforward.

I also downloaded and installed the Subversion client, TortoiseSVN,  from http://sourceforge.net/projects/tortoisesvn/.

TortoiseSVN makes it easy to use Subversion from Windows Explorer by right-clicking and selecting “TortoiseSVN”.

Context menu obtained when right-clicking in Windows Explorer

The following tutorial is good for explaining how to use it with a repository on your PC:  http://www.shokhirev.com/nikolai/programs/SVN/svn.html. However, I have now discovered that Netbeans has very good support for Subversion itself.

In Netbeans:

(I’m using Netbeans IDE 6.9.1)

The following tutorials are very helpful:

The Netbeans IDE Tutorial:  http://netbeans.org/kb/docs/ide/subversion.html

Subversion and Netbeans a Quick Start Guide by James Selvakumar: http://solitarygeek.com/java/subversion-and-netbeans-a-quick-start-guide

First I followed the instructions for “Specifying the Path to the Subversion Executable” in the Netbeans IDE Tutorial. The path being C:Program Files\Subversion\bin. The instructions are very clear and easy to follow.

Next I followed the instructions to Synchronise my local Netbeans files with the Repository, using the Netbeans IDE Tutorial section on “Importing Files Into A Repository”, identifying the Repository URL (provided by unfuddle.com) and entering the Username and Password details.

Right-clicking on the project gives the Subversion menu as shown here:

Netbeans subversion menu

To find out about the cool ways that Subversion works in Netbeans see James’ tutorial. It helped to read that first and then look back at the Netbeans IDE tutorial for more detail.

Enjoy!

Tags: , , , ,

I was a bit bamboozled when I looked at creating a Stored Procedure in PHPMyAdmin today, but it’s easy when you know how!

You simply need to select the SQL option and type in your procedure remembering to set the Delimiter to // in the box underneath.

Here’s a stored procedure to try:

               CREATE PROCEDURE sp_number_example_records()

                    BEGIN   SELECT ‘Number of records: ‘, count(*) from example;

               END//

where “example” is the table name.

On clicking “GO” the stored procedure is created and appears as a “routine” below the list of tables on the main “structure” page for the database.

Use the following SQL to call the Stored Procedure from your code:

CALL sp_number_example_records();

Here’s a link to some info on how to do this in Java.

Tags:

I get very confused by all the jargon used in IT, so this snippet is to explain some of it.

  • Java EE: the Enterprise Edition used for web stuff as described in my last post
  • J2EE: the old name for the above
  • Java SE: the Standard Edition for desktop applications
  • JDK : Java Development Kit – what you need to develop programs
  • JVM: Java Virtual Machine – the platform-independent execution environment that converts Java byte code into machine code and executes it. (See Webopedia for more info.)
  • JRE: Java Runtime Environment – consists of the JVM plus Java platform core classes, and supporting Java platform libraries. It is what you need to run a Java program.

Tags: , , , ,

This first post is about my confusion yesterday when I was considering whether it was really true that I needed to upload Java EE onto the server I am kitting out for a website.  I have done some development using Netbeans on my computer at home and thought I’d look to see what version of Java I’d been using. All I could find was which JDK version was installed. I looked for stuff on the web to get some clues, but there was nothing that specifically said I needed Java EE. In fact it seemed that Java EE was  aimed at big business. I then went and read my class notes and Murach and couldn’t find anything to help me there either. So, feeling rather foolish, I asked my teacher to clarify. This is what he replied:

“You will need J2EE to develop JSP/Servlets et al but you will not need to use all of the frameworks that ship with it. Do the default install for Java EE and you will have all that you need. Java SE is for desktop apps and doesn’t support servlets”

There,  I have it in plain English, thanks!

So now I recollect that that is what  we were taught. I just don’t remember installing  Java EE on my computer.

Tags: ,