Learning PHP

Soldato
Joined
25 Sep 2003
Posts
3,725
Location
Manchester
I need to learn some php for a small project I'm doing and was wondering the best way to learn it is? Also, are there any Dreamweaver type editors that would do code hinting to help me out a bit while I'm getting to grips with it?

What I'm trying to do is put together something where a user uploads images, those images are auto renamed to be called image#### (where#### is an auto number incremented by one from the last file on the server to keep the images in order). The images once uploaded and renamed are added to an xml file. Is using php going to be the best way to do this, and how hard will it be for someone who used to do a bit of VB about 10 years ago? :)

Stage 2 of this project involves randomly shuffling the xml contents so all the images randomize.

Thanks for any tips.
 
Last edited:
Just grab something like XAMPP and it'll get a webserver set-up for you (including PHP support). Once done, create a *.php file in the htdocs folder and go to http://127.0.0.1/*.php and you're half way there :p.

You can use notepad++ for PHP autocompletion (using this). There's also PDT for Eclipse if you want to manage a whole project.

For actually learning PHP, there's loads of tutorials online. YouTube is a great source as well (I work better from Video at least).
 
Last edited:
Just grab something like XAMPP and it'll get a webserver set-up for you (including PHP support). Once done, create a *.php file in the htdocs folder and go to http://127.0.0.1/*.php and you're half way there :p.

You can use notepad++ for PHP autocompletion (using this). There's also PDT for Eclipse if you want to manage a whole project.

For actually learning PHP, there's loads of tutorials online. YouTube is a great source as well (I work better from Video at least).

Wow, thanks Pho - I've got XAMPP set up so am on my way! I've just been playing with an upload script which works great, shame I have no idea what all the code actually really means. :(

I'll browse through YouTube and see what I can learn.
 
What I'd personally do for your upload script is to make use of a database as well. XAMPP comes with MySQL which is all you'd need. The database can keep track of the images' file names and can automatically assign them a unique ID which is easier than manually doing it.

If you don't want to use a database, I got bored and wrote a sample script to get the contents of a directory and output as basic XML:

PHP:
<?php
// An array to store the list of files found
$uploaded_files = array();

// Get the files in the uploaded folder
// results are stored in $uploaded_files
get_file_contents( "uploaded", $uploaded_files, 1 );

// Convert the list of files into XML and output to screen
echo output_xml ( "files", "file", $uploaded_files );


//
// Gets the contents of a folder and stores it in an array
// $folder - the folder to scan
// &$arr - the array to save the results in
// $shuffle - shuffle file name order?
//
function get_file_contents( $folder, &$arr, $shuffle ) {
	// Open the $folder directory
	if ($handle = opendir($folder)) {
		// For each file in the folder
		while (false !== ($file = readdir($handle))) {
			// . and .. and special files to go to the parent folder etc
			// we don't want these -  != is NOT EQUALS
			if ($file != "." && $file != "..") {
				// Add the current file to the $arr array
				array_push($arr, $file);
			}
		}
		
		if ( $shuffle )
			shuffle( $arr );
	}
}


//
// Gets the contents of a folder and stores it in an array
// $parent -  the parent XML tag
// $child -  the child XML tag
// $arr - the array to parse
// structure:
//
function output_xml ( $parent, $child, $arr ) {
	// Set XML header
	$xml = "<?xml version=\"1.0\" encoding=\"utf-8\" ?>\n";
	
	// Add parent tag
	$xml .= "<$parent>\n";
	
	// For each element in the array
	foreach ( $arr as $data ) {
		// Add child tag
		$xml .= "<$child>$data</$child>\n";
	}
	
	// Close parent tag
	$xml .= "</$parent>";
	
	return $xml;
}
?>
 
Back
Top Bottom