Author Archives: Christoffer Wadensten

(PHP) simple picture/file upload script

Here is a simple tutorial script that uploads a file to a folder on the server. In this example it is assumed that the file is an image and it will be outputted on screen in an img-tag. But the file uploading mechanics works for any file type.

<?php
if(isset($_POST['submit']))
{
    $temp_name = $_FILES["file"]["tmp_name"]; // get the temporary filename/path on the server
    $name = $_FILES["file"]["name"]; // get the filename of the actual file
 
    // print the array (for reference)
    print_r($_FILES);
         
    // Create uploads folder if it doesn't exist.
    if (!file_exists("uploads")) {
        mkdir("uploads", 0755);
        chmod("uploads", 0755); // Set read and write permissions of folder, needed on some servers
    }
         
    // Move file from temp to uploads folder
    move_uploaded_file($temp_name, "uploads/$name");
    chmod("uploads/$name", 0644); // Set read and write permissions if file
 
}
?> 
     
<form action="" method="post" enctype="multipart/form-data">
    <input type="file" name="file" id="file" />
    <input type="submit" name="submit" value="submit"/>
</form>

(Android) Delay method call

This is how you could delay a method call for a given amount of time. I’ve used this to have an alert dialog pop up a few second after starting an activity.

This will fire myMethod() after 3000 miliseconds:

				Handler handler = new Handler();
				handler.postDelayed(new Runnable() {
					@Override
					public void run() {
						myMethod();			
					}}, 3000);

(Android) Check if Google Play Store is installed on device

Here’s a quick way to check if Google Play Store is installed. I use this to keep activities that use in-app billing from crashing on devices that doesn’t have Google Play Store. Works fine in the emulator as well. If the method returns false, I’ll just give the user an alert telling them they need Google Play for in-app billing. If it returns true the in-app billing system gets set up.

Here, implemented in a static method. If you call this from an activity you go:

boolean myBoolean = googlePlayStoreInstalled(this);

and if you’re in a fragment or so just go

boolean myBoolean = googlePlayStoreInstalled(getActivity());

I keep this in a utility class.

 
	public static boolean googlePlayStoreInstalled(Activity activity) {

		PackageManager packageManager = activity.getApplication().getPackageManager();
		List<PackageInfo> packages = packageManager.getInstalledPackages(PackageManager.GET_UNINSTALLED_PACKAGES);

		for (PackageInfo packageInfo : packages)
			if (packageInfo.packageName.equals(GooglePlayStorePackageNameOld) || packageInfo.packageName.equals(GooglePlayStorePackageNameNew))
				return true;

		return false;
	}

(Android) Simple confirm dialog

	
private void confirmDialog() {		
		AlertDialog.Builder builder = new AlertDialog.Builder(context);
				
		builder
		.setMessage("Are you sure?")
		.setPositiveButton("Yes",  new DialogInterface.OnClickListener() {
			@Override
			public void onClick(DialogInterface dialog, int id) {
				// Yes-code
			}
		})
		.setNegativeButton("No", new DialogInterface.OnClickListener() {
			@Override
			public void onClick(DialogInterface dialog,int id) {
				dialog.cancel();
			}
		})
		.show();
	}

YouTube Tutorials

My all time favorite tutorial maker Derek Banas has an extensive catalogue of programming tutorials on his Youtube channel. I’ve used those a lot, especially in my studies. His series on design patterns are golden!

I really like the tempo in them. Very hands-on and very organised.

He has a bunch of videos that covers entire techniques in a single video. Perfect if you’re a somewhat experienced developer that need to get up to speed in something.

Here’s and example: