Array problem in Class
Posted: Wed Oct 07, 2009 7:49 am
I'm pretty new to classes, currently trying to convert some of my php snippet to reusable classes. I have made an upload class, the allowed_extensions is supposed to be an array but I don't believe it is treated as such because when trying to upload a file it returns 1 which means that the filetype is treated as invalid.
Below I have provided the two files for testing,
index.php
file-upload-class.php
p.s. Also can someone tell me how to declare constants in a class? define() doesn't work.
Below I have provided the two files for testing,
index.php
Code: Select all
<?php
require_once('file-upload-class.php');
?>
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<title>File Upload</title>
<style type="text/css">
body
{
font-size:14px;
cursor:default;
}
</style>
</head>
<body>
<?php
$uploader = new file_uploader('filename',100000, 2000, 2000, 30, 0, 0, array('.gif, .png, .jpg'),'uploads');
if ($_GET['action'] == 'upload')
{
$upload_message = $uploader->file_upload();
echo '<br /><strong>'.$upload_message.'</strong><br />';
}
?>
<h3>File Upload</h3>
<form name="upload" style="width:400px;overflow-x:hidden;" id="upload" enctype="multipart/form-data" method="post" action="index.php?action=upload">
File:
<br /><input type="file" id="filename" name="filename" />
<br />
<label for="overwrite"><input type="checkbox" id="overwrite" name="overwrite" value="1" /> overwrite if exists</label>
<br /><br />
<input type="submit" name="upload" value="Upload" />
</form>
</body>
</html>Code: Select all
class file_uploader
{
//declare variables
var $max_filesize;
var $max_file_width;
var $max_file_height;
var $max_filename_length;
var $filename_to_md5;
var $filename_to_lowercase;
var $upload_dir;
var $allowed_extensions = array();
var $file_name;
var $file_size;
var $file_width;
var $file_height;
var $filesize_in_kbs;
var $file_extension;
var $temp_name;
var $file_path;
var $overwrite;
var $is_uploaded;
var $successful;
var $file_field_name;
var $UPLOAD_NO_FILE;
var $UPLOAD_FILETYPE_INVALID;
var $UPLOAD_FILENAME_EXISTS;
var $UPLOAD_FILENAME_LONG;
var $UPLOAD_FILENAME_INVALID;
var $UPLOAD_FILESIZE_LARGE;
var $UPLOAD_FILEDIMENSIONS_LARGE;
var $UPLOAD_ERROR;
var $UPLOAD_SUCCESS;
var $UPLOAD_SUCCESS_OVERWRITTEN;
public function file_uploader($file_field_name, $max_filesize, $max_file_width, $max_file_height, $max_filename_length, $filename_to_md5, $filename_to_lowercase, $allowed_extensions, $upload_dir)
{
$this->file_field_name = $file_field_name;
$this->max_filesize = $max_filesize;
$this->max_file_width = $max_file_width;
$this->max_file_height = $max_file_height;
$this->max_filename_length = $max_filename_length;
$this->filename_to_md5 = $filename_to_md5;
$this->filename_to_lowercase = $filename_to_lowercase;
$this->allowed_extensions = array($allowed_extensions);
$this->upload_dir = $upload_dir.'/';
$this->UPLOAD_NO_FILE = 0;
$this->UPLOAD_FILETYPE_INVALID = 1;
$this->UPLOAD_FILENAME_EXISTS = 2;
$this->UPLOAD_FILENAME_LONG = 3;
$this->UPLOAD_FILENAME_INVALID = 4;
$this->UPLOAD_FILESIZE_LARGE = 5;
$this->UPLOAD_FILEDIMENSIONS_LARGE = 6;
$this->UPLOAD_ERROR = 7;
$this->UPLOAD_SUCCESS = 8;
$this->UPLOAD_SUCCESS_OVERWRITTEN = 9;
return 0;
}
public function file_upload()
{
function filename_validate($str)
{
if (
preg_match("/^([a-z0-9]+[\040_\-]?)*\.[a-z]{3}$/i",$str)
)
{
return true;
}
else
{
return false;
}
}
//post file name and other attributes from upload field
$this->file_name = $_FILES[$this->file_field_name]['name'];
$this->temp_name = $_FILES[$this->file_field_name]['tmp_name'];
$this->file_size = $_FILES[$this->file_field_name]['size'];
$this->overwrite = $_POST['overwrite'];
//get file extension
$this->file_extension = strtolower(substr($this->file_name,strrpos($this->file_name,".")));
//md5 filename
if ($this->filename_to_md5 == 1)
{$this->file_name = md5($this->file_name).$this->file_extension;}
//filename to lowercase
if ($this->filename_to_lowercase == 1){$this->file_name = strtolower($this->file_name);}
//determine final upload path
$this->file_path = $this->upload_dir.$this->file_name;
//get file dimensions
list($this->file_width, $this->file_height) = getimagesize($this->temp_name);
//calculate filesize in kbs
$this->filesize_in_kbs = $this->file_size/1000;
//initialize successful variable
$this->successful = 1;
//no file selected
if ($this->file_name == '' && $this->successful == 1)
{
$this->successful = 0;
return $this->UPLOAD_NO_FILE;
}
/*__________PROBLEM___________*/
//file type check
if (!in_array($this->file_extension, $this->allowed_extensions) && $this->successful == 1)
{
$this->successful = 0;
return $this->UPLOAD_FILETYPE_INVALID;
}
/*__________end of PROBLEM___________*/
//check if file exists
if (file_exists($this->upload_dir.$this->file_name) && $this->successful == 1 && !$this->overwrite == 1)
{
$this->successful = 0;
return $this->UPLOAD_FILENAME_EXISTS;
}
//check file name length
if ((strlen (basename(($this->file_name))) > $this->max_filename_length) && $this->successful == 1)
{
$successful = 0;
return $this->UPLOAD_FILENAME_LONG;
}
//check filename validity
if (filename_validate(basename($this->file_name)))
{}
else
{
$this->successful = 0;
return $this->UPLOAD_FILENAME_INVALID;
}
//file size check
if( $this->file_size > $this->max_filesize && $this->successful == 1)
{
$successful = 0;
return $this->UPLOAD_FILESIZE_LARGE;
}
//file dimensions check
if( ($this->file_width > $this->max_file_width || $this->file_height > $this->max_file_height) && $this->successful == 1)
{
$this->successful = 0;
return $this->UPLOAD_FILEDIMENSIONS_LARGE;
}
if ($this->successful == 1)
{
//move the file to the upload folder
$this->is_uploaded = move_uploaded_file($this->temp_name, $this->file_path);
if ($this->is_uploaded == 1)
{
if ($this->overwrite == 1)
{
return $this->UPLOAD_SUCCESS_OVERWRITTEN;
}
else
{
return $this->UPLOAD_SUCCESS;
}
}
else
{
return $this->UPLOAD_ERROR;
}
}
}
}p.s. Also can someone tell me how to declare constants in a class? define() doesn't work.