Uploads Class Difficulty

PHP programming forum. Ask questions or help people concerning PHP code. Don't understand a function? Need help implementing a class? Don't understand a class? Here is where to ask. Remember to do your homework!

Moderator: General Moderators

Post Reply
aspekt9
Forum Commoner
Posts: 43
Joined: Wed Dec 06, 2006 5:03 pm

Uploads Class Difficulty

Post by aspekt9 »

I'm having some problems with my Uploads class, it just wont upload. I don't get any errors and when I echo out values it all looks good.

Upload.class.php:

Code: Select all

<?php
ini_set('display_error', 1);
error_reporting(E_ALL);
class Upload_Files {
	
	// Define vars
	var $temp_file_name;
	var $file_name;
	var $upload_dir;
	var $upload_log_dir;
	var $max_file_size;
	var $banned_array;
	var $ext_array;
	
	// Validate file extensions
	function validate_ext() {
		$file_name = trim($this->file_name);
		$extension = strtolower(strrchr($file_name,"."));
		$ext_array = $this->ext_array;
		$ext_count = count($ext_array);
		
		if (!$file_name) {
			return false;
		} else {
			if (!$ext_array) {
				return true;
			} else {
				foreach ($ext_array as $value) {
					$first_char = substr($value,0,1);
						if ($first_char <> ".") {
							$extensions[] = ".".strtolower($value);
						} else {
							$extensions[] = strtolower($value);
						}
				}
				
				foreach ($extensions as $value) {
					if ($value == $extension) {
						$valid_extension = "TRUE";
					}
				}
				
				if ($valid_extension) {
					return true;
				} else {
					return false;
				}
			}
		}
	}
	
	// Validate file size
	function validate_size() {
		$temp_file_name = trim($this->temp_file_name);
		$max_file_size = trim($this->max_file_size);
		
		if (!$temp_file_name) {
			$size = filesize($temp_file_name);
				if ($size > $max_file_size) {
					return false;
				} else {
					return true;
				}
		} else {
			return false;
		}
	}
	
	function file_exists() {
		$file_name = trim($this->file_name);
		$upload_dir = $this->get_upload_dir();
		
		if ($upload_dir == "ERROR") {
			return true;
		} else {
			$file = $upload_dir . $file_name;
			if (file_exists($file)) {
				return true;
			} else {
				return false;
			}
		}
	}
	
	// Check the size of the file to be uploaded
	function get_file_size() {
		$temp_file_name = trim($this->temp_file_name);
		$kb = 1024;
		$mb = 1024 * $kb;
		$gb = 1024 * $mb;
		$tb = 1024 * $gb;
		
			if ($temp_file_name) {
				$size = filesize($temp_file_name);
				if ($size < $kb) {
					$final_size = "$size Bytes";
				}
				elseif ($size < $mb) {
					$final = round($size/$kb,2);
					$final_size = "$final KB";
				}
				elseif ($size < $gb) {
					$final = round($size/$mb,2);
					$final_size = "$final MB";
				}
				elseif ($size < $tb) {
					$final = round($size/$gb,2);
					$final_size = "$final GB";
				} else {
					$final = round($size/$tb,2);
					$final_size = "$final TB";
				}
			} else {
				$final_size = "ERROR: NO FILE TO PARSE";
			}
			return $final_size;
	}
	
	// Check the max file size allowed
	function get_max_size() {
		$max_file_size = trim($this->max_file_size);
		$kb = 1024;
		$mb = 1024 * $kb;
		$gb = 1024 * $mb;
		$tb = 1024 * $gb;
		
		if ($max_file_size) {
			if ($max_file_size < $kb) {
				$max_file_size = "$max_file_size Bytes";
			}
			elseif ($max_file_size < $mb) {
				$final = round($max_file_size/$kb,2);
				$max_file_size = "$final KB";
			}
			elseif ($max_file_size < $gb) {
				$final = round($max_file_size/$mb,2);
				$max_file_size = "$final MB";
			}
			elseif ($max_file_size < $tb) {
				$final = round($max_file_size/$gb,2);
				$max_file_size = "$final GB";
			} else {
				$final = round($max_file_size/$tb,2);
				$max_file_size = "$final TB";
			}
		} else {
			$max_file_size = "ERROR: NO SIZE PARAMETER PASSED";
		}
			return $max_file_size;
	}
	
	// Grab information about the user uploading the file
	function validate_user() {
		$banned_array = $this->banned_array;
		$ip = trim($_SERVER['REMOTE_ADDR']);
		$cpu = gethostbyaddr($ip);
		$count = count($banned_array);
		
		if ($count < 1) {
			return true;
		} else {
			foreach($banned_array as $key => $value) {
				if ($value == $ip ."-". $cpu) {
					return false;
				} else {
					return true;
				}
			}
		}
	}
	
	// Retrieve the upload directory
	function get_upload_dir() {
		$upload_dir = trim($this->upload_dir);
		if ($upload_dir) {
			$ud_len = strlen($upload_dir);
			$last_slash = substr($upload_dir,$ud_len-1,1);
				if ($last_slash <> "/") {
					$upload_dir = $upload_dir."/";
				} else {
					$upload_dir = $upload_dir;
				}
				$handle = @opendir($upload_dir);
					if($handle) {
						$upload_dir = $upload_dir;
						closedir($handle);
					} else {
						$upload_dir = "ERROR";
					}
		} else {
			$upload_dir = "ERROR";
		}
		return $upload_dir;
	}
	
	// Retrieve the upload log directory
	function get_upload_log_dir() {
		$upload_log_dir = trim($this->upload_log_dir);
		if ($upload_log_dir) {
			$ud_len = strlen($upload_log_dir);
			$last_slash = substr($upload_log_dir,$ud_len-1,1);
				if ($last_slash <> "/") {
					$upload_log_dir = $upload_log_dir."/";
				} else {
					$upload_log_dir = $upload_log_dir;
				}
				$handle = @opendir($upload_log_dir);
					if($handle) {
						$upload_log_dir = $upload_log_dir;
						closedir($handle);
					} else {
						$upload_log_dir = "ERROR";
					}
		} else {
			$upload_log_dir = "ERROR";
		}
		return $upload_log_dir;
	}
	
	// Upload the file without validating file size, type, or user
	function upload_file_no_validation() {
		$temp_file_name = trim($this->temp_file_name);
		$file_name = trim(strtolower($this->file_name));
		$upload_dir = $this->get_upload_dir();
		$upload_log_dir = $this->get_upload_log_dir();
		$ip = trim($_SERVER['REMOTE_ADDR']);
		$cpu = gethostbyaddr($ip);
		$m = date("m");
		$d = date("d");
		$y = date("Y");
		$date = date("m/d/Y");
		$time = date("h:i:s A");
		
		if (($upload_dir == "ERROR") OR ($upload_log_dir == "ERROR")) {
			return false;
		} else {
			if (is_uploaded_file($temp_file_name)) {
				if (move_uploaded_file($temp_file_name,$upload_dir . $file_name)) {
					$log = $upload_log_dir.$y."_".$m."_".$d.".txt";
					$fp = fopen($log,"a+");
					fwrite($fp,"$ip-$cpu | $file_name | $file_size | $date | $time");
					fclose($fp);
					return true;
				} else {
					return false;
				}
			} else {
				return false;
			}
		}
	}
	
	// Upload the file validating the file size, extension, and user
	function upload_file_with_validation() {
		$temp_file_name = trim($this->temp_file_name);
		$file_name = trim(strtolower($this->file_name));
		$upload_dir = $this->get_upload_dir();
		$upload_log_dir = $this->get_upload_log_dir();
		$ip = trim($_SERVER['REMOTE_ADDR']);
		$cpu = gethostbyaddr($ip);
		$m = date("m");
		$d = date("d");
		$y = date("Y");
		$date = date("m/d/Y");
		$time = date("h:i:s A");
		$existing_file = $this->file_exists();
		$valid_user = $this->validate_user();
		$valid_size = $this->validate_size();
		$valid_ext = $this->validate_ext();
		
		if (($upload_dir == "ERROR") OR ($upload_log_dir == "ERROR")) {
			return false;
		}
		elseif ((((!$valid_user) OR (!$valid_size) OR (!$valid_ext) OR ($existing_file)))) {
			return false;
		} else {
			if (is_uploaded_file($temp_file_name)) {
				if (move_uploaded_file($temp_file_name,$upload_dir . $file_name)) {
					$log = $upload_log_dir.$y."_".$m."_".$d.".txt";
					$fp = fopen($log,"a+");
					fwrite($fp,"$ip-$cpu | $file_name | $file_size | $date | $time");
					fclose($fp);
					return true;
				} else {
					return false;
				}
			} else {
				return false;
			}
		}
	}
}
	/*
	Implementation for validation of mime types coming soon
	*/
?>
Upload.php:

Code: Select all

<?php
ini_set('display_error', 1);
error_reporting(E_ALL);
include 'upload.class.php';
$upload_class = new Upload_Files;
$upload_class->temp_file_name = trim($_FILES['uploadedfile']['tmp_name']);
$upload_class->file_name = trim(strtolower($_FILES['uploadedfile']['name']));
$upload_class->upload_dir = "I:/Apache/htdocs/beta/uploads/";
$upload_class->upload_log_dir = "I:/Apache/htdocs/beta/uploads/logs/";
$upload_class->max_file_size = 524288;
$upload_class->banned_array = array("");
$upload_class->ext_array = array(".jpg",".gif",".jpeg",".png",".jpg");
// $upload_class->mime_array = array('image/gif','image/jpeg','image/jpg','image/tiff','image/bmp','image/png');

$valid_ext = $upload_class->validate_ext();
// $valid_mime = $upload_class->validate_mime;
$valid_size = $upload_class->validate_size();
$valid_user = $upload_class->validate_user();
$max_size = $upload_class->get_max_size();
$file_size = $upload_class->get_file_size();
$upload_directory = $upload_class->get_upload_dir();
$upload_log_directory = $upload_class->get_upload_log_dir();

echo $upload_class->temp_file_name ."<br />";
echo $upload_class->file_name ."<br />";
echo $valid_ext ."<br />";
echo $valid_size ."<br />";
echo $max_size ."<br />";
echo $file_size ."<br />";
echo $upload_directory ."<br />";
echo $upload_log_directory ."<br />";
printf("<pre>%s</pre>\n", print_r($_FILES, 1)); 
    if (!$valid_ext) {
        $result = "The file extension is invalid, please try again!";
    }
    elseif (!$valid_size) {
        $result = "The file size is invalid, please try again! The maximum file size is: $max_size and your file was: $file_size";
    }
    elseif (!$valid_user) {
        $result = "You have been banned from uploading to this server.";
    }
    elseif ($file_exists) {
        $result = "This file already exists on the server, please try again.";
    } else {
        $upload_file = $upload_class->upload_file_with_validation();
        if (!$upload_file) {
            $result = "Your file could not be uploaded!";
        } else {
            $result = "Your file has been successfully uploaded to the server.";
        }
    } 
?>
Upload.html:

Code: Select all

<form enctype="multipart/form-data" action="upload.php" method="POST">
Choose a file to upload: <input name="uploadedfile" type="file" /><br />
<input type="submit" value="Upload File" />
</form>
Echo'd and printed variables and arrays:

Code: Select all

I:\Temp\Upload\phpCD95.tmp
kevinsarand.jpg
1

512 KB
21.29 KB
I:/Apache/htdocs/beta/uploads/
I:/Apache/htdocs/beta/uploads/logs/

Array
(
    [uploadedfile] => Array
        (
            [name] => kevinsarand.jpg
            [type] => image/jpeg
            [tmp_name] => I:\Temp\Upload\phpCD95.tmp
            [error] => 0
            [size] => 21803
        )

)

Any ideas?
User avatar
feyd
Neighborhood Spidermoddy
Posts: 31559
Joined: Mon Mar 29, 2004 3:24 pm
Location: Bothell, Washington, USA

Post by feyd »

$file_exists is not set.

$result is never printed.
aspekt9
Forum Commoner
Posts: 43
Joined: Wed Dec 06, 2006 5:03 pm

Post by aspekt9 »

Ok, I fixed both those errors, now I get:

Code: Select all

The file size is invalid, please try again! The maximum file size is: 512 KB and your file was: 103.07 KB
User avatar
pickle
Briney Mod
Posts: 6445
Joined: Mon Jan 19, 2004 6:11 pm
Location: 53.01N x 112.48W
Contact:

Post by pickle »

$max_file_size isn't set.
Real programmers don't comment their code. If it was hard to write, it should be hard to understand.
User avatar
John Cartwright
Site Admin
Posts: 11470
Joined: Tue Dec 23, 2003 2:10 am
Location: Toronto
Contact:

Post by John Cartwright »

why don't you try setting

Code: Select all

error_reporting(E_ALL);
to see all your undefined variables
User avatar
Benjamin
Site Administrator
Posts: 6935
Joined: Sun May 19, 2002 10:24 pm

Post by Benjamin »

Or using my upload class.. viewtopic.php?t=70569
aspekt9
Forum Commoner
Posts: 43
Joined: Wed Dec 06, 2006 5:03 pm

Post by aspekt9 »

Jcart wrote:why don't you try setting

Code: Select all

error_reporting(E_ALL);
to see all your undefined variables
error_reporting(E_ALL) is set at the top of each file..
Post Reply