Function for easily uploading files in PHP

A function for easily uploading files. This function will automatically generate a new file name so that files are not overwritten.

Arguments: $file_id- The name of the input field contianing the file.
$folder – The folder to which the file should be uploaded to – it must be writable. OPTIONAL
$types – A list of comma(,) seperated extensions that can be uploaded. If it is empty, anything goes OPTIONAL
Returns : This is somewhat complicated – this function returns an array with two values…
The first element is randomly generated filename to which the file was uploaded to.
The second element is the status – if the upload failed, it will be ‘Error : Cannot upload the file ‘name.txt’.’ or something like that
Sample
PHP Part

 PHP |  copy code |? 
1
if($_FILES['image']['name']) {
2
list($file,$error) = upload('image','uploads/','jpeg,gif,png');
3
if($error) print $error;
4
}
5

HTML Part
 HTML |  copy code |? 
1
<form action="" method="post" enctype="multipart/form-data">
2
<input type="file" name="image" /><input type="submit" value="Upload" name="action" />
3
</form
4

Function defination:
 PHP |  copy code |? 
01
function  upload($file_id, $folder="", $types="") {
02
	if(!$_FILES[$file_id]['name'])
03
		return array('','No file specified');
04
	$file_title = $_FILES[$file_id]['name'];
05
	//Get file extension
06
	$ext_arr = split("\.",basename($file_title));
07
	$ext = strtolower($ext_arr[count($ext_arr)-1]);
08
	$filelog = "filename".$ext_arr[0]." ext.".$ext." original".$file_title;
09
 
10
	//Get the last extension
11
	//Not really uniqe - but for all practical reasons, it is
12
	$uniqer = substr(md5(uniqid(rand(),1)),0,5);
13
	//$file_name =$uniqer.'_'.$file_title;
14
	//$file_name =$ext_arr[0].'_'.$uniqer.'.'.$ext;
15
	$file_name =$ext_arr[0].'_'.$uniqer.'.jpg';
16
	//Get Unique Name
17
	$all_types = explode(",",strtolower($types));
18
	if($types) {
19
		if(in_array($ext,$all_types));
20
		else {
21
			$result = "'".$_FILES[$file_id]['name']."' is not a valid file.";
22
			//Show error if any.
23
			return array('',$result);
24
		}
25
	}
26
	//Where the file must be uploaded to
27
	if($folder)
28
		$folder .= '/';
29
	//Add a '/' at the end of the folder
30
	$uploadfile = $folder . $file_name;
31
	$result = '';
32
	//Move the file from the stored location to the new location
33
	if (!move_uploaded_file($_FILES[$file_id]['tmp_name'], $uploadfile)) {
34
		$result = "Cannot upload the file '".$_FILES[$file_id]['name']."'";
35
		//Show error if any.
36
		if(!file_exists($folder)) {
37
			$result .= " : Folder don't exist.";
38
		}
39
		elseif(!is_writable($folder)) {
40
			$result .= " : Folder not writable.";
41
		}
42
		elseif(!is_writable($uploadfile)) {
43
			$result .= " : File not writable.";
44
		}
45
		$file_name = '';
46
	}
47
	else {
48
		if(!$_FILES[$file_id]['size']) {
49
			//Check if the file is made
50
			@unlink($uploadfile);	//Delete the Empty file
51
			$file_name = '';
52
			$result = "Empty file found - please use a valid file."; //Show the error message
53
		} else {
54
			chmod($uploadfile,0777);//Make it universally writable.
55
		}
56
	}
57
	return array($file_name,$result);
58
}

  • Share/Bookmark
No Responses to “Function for easily uploading files in PHP”

Post a Comment