Hey all.
I am having some trouble applying dimension restrictions to image uploads. I have taken some of the code from an article on this site and used it to make an upload Class, which, for most intents and purposes, does its job quite well. However, the image dimension restrictions do not seem to work. Even when the restrictions are set, it is possible to upload an image of any size. Any help here is greatly appreciated.
Here are the key methods:
function Upload() { // constructor
$this->theFile = array();
$this->filePath = "";
$this->maxFileSize = 0;
$this->imageMaxHeight = 0;
$this->imageMaxWidth = 0;
$this->error = "";
$regTypes = array(
"application/x-gzip-compressed" => ".tar.gz, .tgz",
"application/x-zip-compressed" => ".zip",
"application/x-tar" => ".tar",
"text/plain" => ".html, .php, .txt, .inc (etc)",
"image/bmp" => ".bmp, .ico",
"image/gif" => ".gif",
"image/pjpeg" => ".jpg, .jpeg",
"image/jpeg" => ".jpg, .jpeg",
"application/x-shockwave-flash" => ".swf",
"application/msword" => ".doc",
"application/vnd.ms-excel" => ".xls",
"application/octet-stream" => ".exe, .fla (etc)"
);
$this->allowedTypes = array();
}
function validateUpload() {
print("OK");
if($this->theFile == null) {
$this->error = "No file uploaded!";
return false;
} else {
if(!in_array($this->theFile['type'], $this->allowedTypes)) {
$this->error = "Unacceptable file format!";
return false;
}
if(ereg("image", $this->theFile['type']) && (in_array($this->theFile['type'], $this->allowedTypes))) {
$size = GetImageSize($this->theFile);
list($foo, $width, $bar, $height) = explode("\"", $size[3]);
if($width > $this->imageMaxWidth) {
$this->error = "Exceded image width!";
return false;
}
if($height > $this->imageMaxHeight) {
$this->error = "Exceded image height!";
return false;
}
}
}
return true;
}
function doUpload() {
if($this->validateUpload = false) {
return false;
} else {
if(!copy($this->theFile['tmp_name'], $this->filePath . "/" . $this->theFile['name'])) {
$this->error = "Can't copy the file!";
return false;
}
}
return true;
}
And here is how the object is created and used:
$Upload = new Upload(); // create new Upload object
$Upload->setTheFile($_FILES['ulImage']); // get the upload file
$Upload->setFilePath($cfg->uploadedImageDir); // set upload options
$Upload->setMaxFileSize($cfg->maxUploadSize);
$Upload->setImageMaxHeight($cfg->maxUploadHeight);
$Upload->setImageMaxWidth($cfg->maxUploadWidth);
if(!$Upload->doUpload()) { // execute upload
Any suggestions as to how I can get the max height and width restrictions to apply?
Regards.
Steve.