1: <?php
2: /**
3: * Description of fileuploader
4: *
5: * @author SARTAJ
6: */
7:
8:
9:
10: class FileUploader extends Control{
11: public $maxLen = '';
12: public $minLen = '';
13: private $formName = '';
14: private $msgName = '';
15: private $req = false;
16: private $fileType = '';
17: private $fileExtention = '';
18: private $fileTypeA = '';
19: private $fileSize = '';
20: private $fileTempName = '';
21: private $fildName = '';
22: private $tablName = '';
23: private $fileSavePath = '';
24:
25: public function __construct($name='',$fieldName='',$tableName='') {
26: global $page,$JSServer,$tblName,$mysql;
27: $this->name = $name;
28: $this->fildName = $fieldName;
29: $this->tablName = $tableName;
30: $this->unsetEndTag();
31: $this->fileType = $_FILES["$name"]["type"];
32: $this->fileSize = $_FILES["$name"]["size"];
33: $this->fileTempName = $_FILES["$name"]["tmp_name"];
34: if($this->fileTempName!=''){
35: $_REQUEST[$this->name] = $_FILES["$name"]["name"];
36: $ft = pathinfo($_FILES["$name"]["name"]);
37: $this->fileExtention = $ft['extension'];
38: }
39: $this->tagName = "input";
40: $this->parameterA['type'] = 'file';
41: $this->init($this->name,$this->fildName,$this->tablName);
42: if(SphpBase::page()->sact== $name.'del'){
43: $file = decrypt($_REQUEST['pfn']);
44: $pt = pathinfo($file);
45: if(file_exists($file)){unlink($file);}
46: if(file_exists('cache/'.$pt['basename'])){unlink('cache/'.$pt['basename']);}
47: $mysql->executeQueryQuick("UPDATE $tblName SET $this->fildName='' WHERE id='SphpBase::page()->evtp'");
48: $JSServer->addJSONBlock('html','out'.$this->name,'Pic Deleted!');
49: }
50: }
51:
52: public function setForm($val) { $this->formName = $val;}
53: public function setMsgName($val) { $this->msgName = $val;}
54: public function setRequired() {
55: if($this->issubmit){
56: if(strlen($this->value) < 1){
57: setErr($this->name.'-req',"Can not submit Empty");
58: }
59: }
60: $this->req = true;
61: }
62: public function setFileMaxLen($val)
63: {
64: $this->maxLen = $val;
65: if($this->issubmit){
66: if($this->getFileSize() > $val){
67: setErr($this->name.'-maxfl',"Maximum File Length should not be exceed then $val bytes");
68: }
69: }
70: }
71: public function getFileMaxLen() { return $this->maxLen; }
72: public function setFileMinLen($val)
73: {
74: $this->minFileLen = $val;
75: if($this->issubmit){
76: if($this->getValue()!='' && $this->getFileSize() < $val ){
77: setErr($this->name.'-minfl',"Minimum File Length should be $val bytes");
78: }
79: }
80: }
81: public function getFileMinLen() { return $this->minLen; }
82: public function setFileType($val){$this->fileType = $val;}
83: public function getFileType(){return $this->fileType;}
84: public function setFileSize($val){$this->fileSize = $val;}
85: public function getFileSize(){return $this->fileSize;}
86: public function setFileTypesAllowed($val){$this->fileTypeA = $val; $this->findTypeAllowed();}
87: public function getFileTypesAllowed(){return $this->fileTypeA;}
88: public function getFileTempName(){return $this->fileTempName;}
89: public function getFilePrevName(){return $_REQUEST['hid'.$this->name];}
90: public function getFileExtention(){return $this->fileExtention;}
91: public function setFileSavePath($val){$this->fileSavePath = $val;}
92:
93: private function findTypeAllowed(){
94: $blnFound = false;
95: if($this->issubmit){
96: $types = split(',',$this->getFileTypesAllowed());
97: foreach($types as $key=>$val){
98: if($val == $this->getFileType()){
99: $blnFound = true;
100: break;
101: }
102: }
103: if(!$blnFound){
104: setErr($this->name.'-filetype',$this->getFileType(). ' File Type is not allowed');
105: }
106: }
107:
108: }
109: private function saveFile($FilePath){
110: if ($this->issubmit && !getCheckErr()){
111: if ($_FILES[$this->name]["error"] > 0)
112: {
113: setErr($this->name.'-save',$_FILES[$this->name]["error"]);
114: }
115: else
116: {
117: if(!move_uploaded_file($_FILES[$this->name]["tmp_name"], $FilePath )){
118: setErr($this->name.'-save',"Can not save file on server");
119: }else{
120: $this->value = $this->fileSavePath;
121: }
122: // $this->value = $FilePath;
123: }
124: }
125: }
126:
127: public function oncreate($element){
128: $this->saveFile($this->fileSavePath);
129: }
130:
131: public function onjsrender(){
132: global $JSServer;
133: $JSServer->getAJAX();
134: addFileLink($this->myrespath."res/jquery.MultiFile.pack.js");
135: $this->setPostTag('<div id="'.$this->name.'-list"></div>');
136: addHeaderJSFunctionCode('ready', $this->name, "
137: $('#{$this->name}').MultiFile({
138: list: '#{$this->name}-list',
139: max: 1,
140: accept: 'gif|jpg|png|bmp|swf'
141: });
142: ");
143: if($this->formName!=''){
144: if($this->req){
145: addFooterJSFunctionCode("{$this->formName}_submit", "{$this->name}req", "
146: ctlReq['$this->name']= Array('$this->msgName','TextField');");
147: }
148: }
149:
150: }
151:
152: public function onrender(){
153: global $JSClient,$page;
154: if($this->value!=''){
155: $this->parameterA['value'] = $this->value;
156: if($this->value!=''){
157: $this->setPostTag('<input type="hidden" name="hid'.$this->name.'" value="'.$this->value.'" /><div id="out'.$this->name.'">
158: <img src="'.$this->value.'" width="150" height="100" /><a href="javascript: '.$JSClient->postServer("'".getEventURL($this->name.'del',SphpBase::page()->evtp,'','pfn='.encrypt($this->value),'',true)."'").'">Delete</a></div>');
159: }
160: }
161: }
162:
163: // javascript functions used by ajax control and other control
164: public function getJSValue(){
165: return "document.getElementById('$this->name').value" ;
166: }
167:
168: public function setJSValue($exp){
169: return "document.getElementById('$this->name').value = $exp;" ;
170: }
171:
172: }
173: ?>