For you PHP experts out there...
[code:1] # ----------------------------------- #
# FUNCTION: upload
# ARGS: $filename, $accept_type, $extension
# DESCRIPTION: Checks and sets the raw
# file data, checks if the content type is
# acceptable (if $accept_type) is set, and
# adds a default extension ($extention) if
# there is no ".xxx" in the filename
# ----------------------------------- #
function upload($filename, $accept_type='', $extention='') {
// get all the properties of the file
$index = array("file", "name", "size", "type");
for($i = 0; $i < 4; $i++) {
$file_var = '$' . $filename . (($index[$i] != "file") ? "_" . $index[$i] : "");
eval('global ' . $file_var . ';');
eval('$this->file[$index[$i]] = ' . $file_var . ';');
}
if($this->file["file"] && $this->file["file"] != "none") {
// test max size
if($this->max_filesize && $this->file["size"] > $this->max_filesize) {
$this->errors[1] = "Maximum file size exceeded. File may be no larger than " . $this->max_filesize/1000 . "KB (" . $this->max_filesize . " bytes).";
return FALSE;
}
if(ereg("image", $this->file["type"])) {
$image = getimagesize($this->file["file"]);
$this->file["width"] = $image[0];
$this->file["height"] = $image[1];
// test max image size
if(($this->max_image_width || $this->max_image_height) && (($this->file["width"] > $this->max_image_width) || ($this->file["height"] > $this->max_image_height))) {
$this->errors[2] = "Maximum image size exceeded. Image may be no more than " . $this->max_image_width . " x " . $this->max_image_height . " pixels";
return FALSE;
}
switch($image[2]) {
case 1:
$this->file["extention"] = ".gif";
break;
case 2:
$this->file["extention"] = ".jpg";
break;
case 3:
$this->file["extention"] = ".png";
break;
default:
$this->file["extention"] = $extention;
break;
}
} elseif(!ereg("(\.)([a-z0-9]{3,5})$", $this->file["name"]) && !$extention) {
// add new mime types here
switch($this->file["type"]) {
case "text/plain":
$this->file["extention"] = ".txt";
break;
default:
break;
}
} else {
$this->file["extention"] = $extention;
}
// check to see if the file is of type specified
if($accept_type) {
if(ereg($accept_type, $this->file["type"])) {
$this->accepted = TRUE;
} else {
$this->accepted = FALSE;
$this->errors[3] = "Only " . ereg_replace("\|", " or ", $accept_type) . " files may be uploaded";
}
} else {
$this->accepted = TRUE;
}
} else {
$this->accepted = FALSE;
$this->errors[0] = "No file was uploaded";
}
return $this->accepted;
} [/code:1]
for some reason, this will not recognise JPG files as a valid file extention, even when passed image/jpeg and image/jpg in accept_type
any clues?
[code:1] # ----------------------------------- #
# FUNCTION: upload
# ARGS: $filename, $accept_type, $extension
# DESCRIPTION: Checks and sets the raw
# file data, checks if the content type is
# acceptable (if $accept_type) is set, and
# adds a default extension ($extention) if
# there is no ".xxx" in the filename
# ----------------------------------- #
function upload($filename, $accept_type='', $extention='') {
// get all the properties of the file
$index = array("file", "name", "size", "type");
for($i = 0; $i < 4; $i++) {
$file_var = '$' . $filename . (($index[$i] != "file") ? "_" . $index[$i] : "");
eval('global ' . $file_var . ';');
eval('$this->file[$index[$i]] = ' . $file_var . ';');
}
if($this->file["file"] && $this->file["file"] != "none") {
// test max size
if($this->max_filesize && $this->file["size"] > $this->max_filesize) {
$this->errors[1] = "Maximum file size exceeded. File may be no larger than " . $this->max_filesize/1000 . "KB (" . $this->max_filesize . " bytes).";
return FALSE;
}
if(ereg("image", $this->file["type"])) {
$image = getimagesize($this->file["file"]);
$this->file["width"] = $image[0];
$this->file["height"] = $image[1];
// test max image size
if(($this->max_image_width || $this->max_image_height) && (($this->file["width"] > $this->max_image_width) || ($this->file["height"] > $this->max_image_height))) {
$this->errors[2] = "Maximum image size exceeded. Image may be no more than " . $this->max_image_width . " x " . $this->max_image_height . " pixels";
return FALSE;
}
switch($image[2]) {
case 1:
$this->file["extention"] = ".gif";
break;
case 2:
$this->file["extention"] = ".jpg";
break;
case 3:
$this->file["extention"] = ".png";
break;
default:
$this->file["extention"] = $extention;
break;
}
} elseif(!ereg("(\.)([a-z0-9]{3,5})$", $this->file["name"]) && !$extention) {
// add new mime types here
switch($this->file["type"]) {
case "text/plain":
$this->file["extention"] = ".txt";
break;
default:
break;
}
} else {
$this->file["extention"] = $extention;
}
// check to see if the file is of type specified
if($accept_type) {
if(ereg($accept_type, $this->file["type"])) {
$this->accepted = TRUE;
} else {
$this->accepted = FALSE;
$this->errors[3] = "Only " . ereg_replace("\|", " or ", $accept_type) . " files may be uploaded";
}
} else {
$this->accepted = TRUE;
}
} else {
$this->accepted = FALSE;
$this->errors[0] = "No file was uploaded";
}
return $this->accepted;
} [/code:1]
for some reason, this will not recognise JPG files as a valid file extention, even when passed image/jpeg and image/jpg in accept_type
any clues?
Comment