You asked for it; here's the class I'm using...
class file_upload {
var $errormsg;
var $the_file;
var $the_temp_file;
var $upload_dir;
var $replace;
var $do_filename_check;
var $max_length_filename = 255;
var $extensions;
var $ext_string;
var $language;
var $http_error;
var $rename_file; // if this var is true the file copy get a new name
var $file_copy; // the new name
var $message = array();
function file_upload() {
$this->language = "en"; // choice of en, nl, es
$this->rename_file = true;
$this->ext_string = "";
}
function show_error_string() {
$msg_string = "";
foreach ($this->message as $value) {
$msg_string .= $value."<br>\n";
}
return $msg_string;
}
function set_file_name($new_name = "") { // this "conversion" is used for unique/new filenames
if ($this->rename_file) {
$conf = strtotime("now");
$name = ($new_name == "") ? $conf : $new_name;
$name = $name.$this->get_extension($this->the_file);
} else {
$name = $this->the_file;
}
return $name;
}
function upload($to_name = "") {
$new_name = $this->set_file_name($to_name);
if ($this->check_file_name($new_name)) {
if ($this->validateExtension()) {
if (is_uploaded_file($this->the_temp_file)) {
$this->file_copy = $new_name;
if ($this->move_upload($this->the_temp_file, $this->file_copy)) {
//$this->message[] = $this->error_text($this->http_error);
//if ($this->rename_file) $this->message[] = $this->error_text(16);
return true;
}
} else {
//$this->message[] = $this->error_text($this->http_error);
//echo( "Not the file" );
return true;
}
} else {
$this->show_extensions();
//$this->message[] = $this->error_text(11);
//echo( "Not the right ext" );
return false;
}
} else {
return false;
}
}
function check_file_name($the_name) {
if ($the_name != "") {
if (strlen($the_name) > $this->max_length_filename) {
//$this->message[] = $this->error_text(13);
return false;
} else {
if ($this->do_filename_check == "y") {
if (ereg("^[a-zA-Z0-9_]*\.[a-zA-Z]{3,4}$", $the_name)) {
return true;
} else {
//$this->message[] = $this->error_text(12);
return false;
}
} else {
return true;
}
}
} else {
//$this->message[] = $this->error_text(10);
return false;
}
}
function get_extension($from_file) {
$ext = strtolower(strrchr($from_file,"."));
return $ext;
}
function validateExtension() {
$extension = $this->get_extension($this->the_file);
$ext_array = $this->extensions;
if (in_array($extension, $ext_array)) {
return true;
} else {
return false;
}
}
// this method is only used for detailed error reporting
function show_extensions() {
$this->ext_string = implode(" ", $this->extensions);
}
function move_upload($tmp_file, $new_file) {
umask(0);
if ($this->existing_file($new_file)) {
$newfile = $this->upload_dir.$new_file;
if ($this->check_dir()) {
if (move_uploaded_file($tmp_file, $newfile)) {
if ($this->replace == "y") {
system("chmod 0777 $newfile");
} else {
system("chmod 0755 $newfile");
}
return true;
} else {
return false;
}
} else {
//$this->message[] = $this->error_text(14);
return false;
}
} else {
//$this->message[] = $this->error_text(15);
return false;
}
}
function check_dir() {
if (!is_dir($this->upload_dir)) {
return false;
} else {
return true;
}
}
function existing_file($file_name) {
if ($this->replace == "y") {
return true;
} else {
if (file_exists($this->upload_dir.$file_name)) {
return false;
} else {
return true;
}
}
}
}
I left out some of the error messages, but you should find what you're looking for here. I obtained the class from http://www.finalwebsites.com... for the record...