PHP: File Upload Script (HTML Form + PHP Handler Class)

by Yang Yang on January 15, 2009

in Free PHP Classes & Library,PHP Tips & Tutorials

It’s sometimes cumbersome to handle uploaded files – checking if it is really uploaded, moving and renaming. Why not writing all these chores into a class and make our own file upload script?

First we are going to create a simple class to handle uploaded files and move them to some place we designate for convenient access.

The PHP Class (PHP5, Uploaded.class.php)
<?php
class Uploaded {
	private $field_name = '';
	// $field_name is the name of the input in uploading form
	public function __construct($field_name) {
		$this -> field_name = $field_name;
	}
	// $path is the path to the directory where
	// the uploaded file you want stored.
	// $primary_name is the primary part of the
	// file name you want the new name of the uploaded file to be
	// such as 'song' with 'song.mp3'. The extensions part of the
	// new name will be determined from that of the uploaded one.
	public function getFileName($path, $primary_name = '') {
		if (empty($path)) {
			return false;
		}
		if (empty($primary_name)) {
			// Use microtime() as temporary file name if no $primary_name is given
			$primary_name = microtime();
		}
		if (is_uploaded_file($_FILES[$this -> field_name]['tmp_name'])) {
			$client_name = basename($_FILES[$this -> field_name]['name']);
			$ext = substr($client_name, strrpos($client_name, '.'));
			$server_name = $primary_name.$ext;
			if (file_exists($path.$server_name)) {
				// deleting any existing files with the same name here
				// so that the old file can be updated this way.
				unlink($path.$server_name);
			}
			if (move_uploaded_file($_FILES[$this -> field_name]['tmp_name'], $path.$server_name)) {
				// The new name of the uploaded file will be returned
				// for access and retrieval of it.
				return $server_name;
			}
		}
		return false;
	}
}
?>

To adapt this class to PHP4, read this.

The HTML
<form enctype="multipart/form-data" method="post" action="upload.php">
	<input type="file" name="photo" />
	<button type="submit" name="submit">Submit</button>
</form>

Fair enough, now we know browsers are going to send upload.php a file accessible in $_FILES via ‘photo’.

The PHP (upload.php)

Now we will use the Uploaded class (Uploaded.class.php) to handle the file sent from the HTML uploading form above.

<?php
require_once('Uploaded.class.php');
if (isset($_POST['submit'])) {
	$myPhoto = new Uploaded('photo');
	$photoFileName = $myPhoto -> getFileName('uploaded/photos/', 'new_name');
}
?>

The uploaded file will now be all ready and accessible in uploaded/photos/new_name.ext (ext is whatever file extension the original file has) with $photoFileName containing its new name.

Related Posts

{ 4 comments… read them below or add one }

Mozgaf March 1, 2010 at 12:51 am

I have 3 questions;
do we edit the Uploaded.class.php file in any way to suite our server?

where do i place the php files in the root folder?

is the Uploaded.class.php how the php file starts is nt there a <?php at start?
thanks in advance.

Reply

Yang Yang March 1, 2010 at 11:47 am

I should have wrapped the class with < ?php ... ?>

Just copy and paste the class and save it as the file Uploaded.class.php

Puting it at the root folder would be fine, but make sure you give the correct path to it in upload.php – in the example / code I give, Uploaded.class.php and upload.php are in the same directory.

Reply

Shozib May 25, 2010 at 12:59 am

Hm very nice

Reply

Cheyne July 2, 2010 at 5:20 am

Haha this would have been useful for my music mod. Had to scour the planet to find a decent one. For the output page…how would you wrap it in a header and footer.

Reply

Leave a Comment

{ 2 trackbacks }

Previous post:

Next post: