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