Home > General, Open Source, Tools > Simple File Upload Script in PHP

Simple File Upload Script in PHP

Here goes my file upload component in PHP. It just uploads the file into the specified directory.Other thing i have added as of now is it’ll upload the files into the folder on the current month and checks for the file exists already or not, if it already exists then it’ll prefix the time in milliseconds in the file name.
	<?php

		/*
		*		Copyright 2010 - Kumarasamy
		*		kumarasamy@techmaddy.com
		*/

		class DocumentHandler {

			var $uploadDir;// Directory where the documents to be uploaded

			function prepateForUpload() {
				$this->uploadDir = '/var/www/uploads';
				$this->uploadDir = $this->uploadDir . "/" . date("Y_m");

				//Check the month folder is available, if not then create it.
				if(!is_dir($this->uploadDir))
					mkdir($this->uploadDir);

			} //End of setDetails

			function copyFile($_FILES,$fileCntrlName) {
				$resultArray = array();
				$uploadfile = $this->uploadDir . "/" . basename($_FILES[$fileCntrlName]['name']);

				//If the file exists already in the folder, append the millisecond in the file name.
				if(is_file($uploadfile)) {
					list($msec, $sec) = explode(".", microtime(true));
					$uploadfile = $this->uploadDir . "/" . $msec . $sec . basename($_FILES[$fileCntrlName]['name']);
				}

				if (move_uploaded_file($_FILES[$fileCntrlName]['tmp_name'], $uploadfile)) {
					$resultArray['error'] = '';
					$resultArray['displayMessage'] = 'File Uploaded Successfully!!!';
				}

				return $resultArray;
			} //End of copyFile

		} //End of DocumentHandler
	?>
	
The following things can be added,

  • Check for the file type
  • Check for the file Size
  • Scan the document using the shell commands
  • Check for the special chars in the file name
  • File name length check also can be added
Idea’s are accepted!!!
Digg This
Reddit This
Stumble Now!
Buzz This
Vote on DZone
Share on Facebook
Bookmark this on Delicious
Kick It on DotNetKicks.com
Shout it
Share on LinkedIn
Bookmark this on Technorati
Post on Twitter
Google Buzz (aka. Google Reader)
  1. No comments yet.
  1. No trackbacks yet.
Get Adobe Flash playerPlugin by wpburn.com wordpress themes