Skip to content

Commit

Permalink
Multiple file uploads support
Browse files Browse the repository at this point in the history
  • Loading branch information
snk002 committed Feb 28, 2018
1 parent a87169d commit 08e04fa
Show file tree
Hide file tree
Showing 2 changed files with 28 additions and 11 deletions.
34 changes: 24 additions & 10 deletions classes/files.php
Original file line number Diff line number Diff line change
Expand Up @@ -21,11 +21,13 @@ class TUploadH extends TControl
public $imagetype; //if checkimage is true, this contain type of image, e.g. image/gif
public $silent; //if true no error messages displayed
public $blacklist; //array of forbidden extentions
public $multi; //multiple files support

public function __construct($name = "", $uploaddir = "")
public function __construct($name = "", $uploaddir = "", $multi = false)
{
parent::__construct(NULL);
$this->name = $name;
$this->multi = $multi;
$this->silent = true;
$this->blacklist = array(".php", ".phtml", ".php3", ".php4", ".htm", ".html", ".xhtml", ".asp", ".jsp", ".aspx", ".exe", ".com", ".sh");
if ($uploaddir == "") $this->uploaddir = $_SERVER['DOCUMENT_ROOT'] . "/uploads/";
Expand All @@ -38,31 +40,43 @@ public function GetFileExt($filename = "")
return substr(strrchr($filename, '.'), 1);
}

public function Upload($filename = "")
public function Upload($filename = "", $i=0)
{
if ($this->multi) {
$fname = $_FILES[$this->name]['name'][$i];
$tname = $_FILES[$this->name]['tmp_name'][$i];
} else {
$fname = $_FILES[$this->name]['name'];
$tname = $_FILES[$this->name]['tmp_name'];
}
foreach ($this->blacklist as $item) {
if (preg_match("/$item\$/i", $_FILES[$this->name]['name'])) {
if (!$this->silent) echo $_FILES[$this->name]['name'] . " CHECK EXTENTION FAILS<br />";
if (preg_match("/$item\$/i", $fname)) {
if (!$this->silent) echo $fname . ": CHECK EXTENTION FAILS<br />";
return false;
}
}
if ($this->checkimage) {
if (($_FILES[$this->name]['type'] != "image/gif") && ($_FILES[$this->name]['type'] != "image/jpeg") && ($_FILES[$this->name]['type'] != "image/png")) {
if (!$this->silent) echo "CHECK MIMETYPE FAILS<br />";
if ($this->multi) {
$ftype = $_FILES[$this->name]['type'][$i];
} else {
$ftype = $_FILES[$this->name]['type'];
}
if (($ftype != "image/gif") && ($ftype != "image/jpeg") && ($ftype != "image/png")) {
if (!$this->silent) echo $ftype . ": CHECK MIMETYPE FAILS<br />";
return false;
}
$imageinfo = getimagesize($_FILES[$this->name]['tmp_name']);
$imageinfo = getimagesize($tname);
if ($imageinfo['mime'] != 'image/gif' && $imageinfo['mime'] != 'image/jpeg' && $imageinfo['mime'] != 'image/png') {
if (!$this->silent) echo "CHECK IMAGESIZE FAILS<br />";
if (!$this->silent) echo $fname . ": CHECK IMAGESIZE FAILS<br />";
return false;
}
$this->imagetype = $imageinfo['mime'];
}
if ($filename == "") $this->filename = basename($_FILES[$this->name]['name']);
if ($filename == "") $this->filename = basename($fname);
else $this->filename = $filename;
$uploadfile = $this->uploaddir . $this->filename;
@mkdir($this->uploaddir, 0777, true);
return (move_uploaded_file($_FILES[$this->name]['tmp_name'], $uploadfile));
return (move_uploaded_file($tname, $uploadfile));
}

public function CheckOn()
Expand Down
5 changes: 4 additions & 1 deletion classes/forms.php
Original file line number Diff line number Diff line change
Expand Up @@ -86,7 +86,10 @@ protected function SetDefAttrs()
{
parent::SetDefAttrs();
$this->SetAttr("type", $this->tagtype);
if ($this->size > 0) $this->SetAttr("size", $this->size);
if ($this->size > 0) {
if ($this->tagtype == "file") $this->SetAttr("multiple", "multiple");
else $this->SetAttr("size", $this->size);
}
if ($this->readonly) $this->SetAttr("readonly", "readonly");
}

Expand Down

0 comments on commit 08e04fa

Please sign in to comment.