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

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.

8 thoughts on “PHP: File Upload Script (HTML Form + PHP Handler Class)”

  1. Pingback: A Basic PHP Contact Form Script

  2. 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.

    1. I should have wrapped the class with

      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.

  3. Pingback: PHP Security Checklist for Websites and Web Applications – Bottom Line for Every Good PHP Developers

  4. 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.

  5. Pingback: Resize and crop images - SitePoint Forums

  6. hey buddy its not working i made 3 files and put all those at same place and made folder photos in uploaded
    file 1st UPLOADED.CLASS.PHP
    file 2nd UPLOAD.PHP
    file 3rd index.html
    and made folder uploaded and in this folder i made another folder photos
    and permissions are also same as u prescribed
    please help in this this how can u make working upload website its messy to login to cpanel all the time to upload a file waiting for your reply

Comments are closed.

Scroll to Top