Beispiel #1
0
 protected function beforeValidate()
 {
     if (!parent::beforeValidate()) {
         return false;
     }
     if ($this->scenario == "fromAdmin") {
         if ($this->teacher_path_file == NULL) {
             $this->addError("teacher_avatar", "teacher_avatar is required");
             return false;
         }
         if (!$this->teacher_path_file->checkExt(array('jpg', 'jpeg', 'png', 'JPG', 'JPEG', 'PNG'))) {
             return FALSE;
         }
         $ext = $this->teacher_path_file->getExtension();
         $name = StringHelper::unicode_str_filter($this->teacher_path_file->name);
         $storeFolder = Yii::getPathOfAlias('webroot') . '/uploads/teacher/';
         $targetPath = $storeFolder;
         //4
         $teacher_ava = Yii::app()->createAbsoluteUrl('uploads') . '/teacher/' . $name;
         if ($ext == "gif" || $ext == "jpg" || $ext == "jpeg" || $ext == "pjepg" || $ext == "png" || $ext == "x-png" || $ext == "GIF" || $ext == "JPG" || $ext == "JPEG" || $ext == "PJEPG" || $ext == "PNG" || $ext == "X_PNG") {
             $this->teacher_path_file->save($targetPath, $name);
             $this->teacher_avatar = $teacher_ava;
         }
     }
     return TRUE;
 }
Beispiel #2
0
 public static function getUrlUploadSingleImage($obj, $user_id)
 {
     $ext_arr = array('png', 'jpg', 'jpeg', 'bmp', 'pdf', 'txt', 'doc', 'docx', 'xls', 'xlsx');
     $name = StringHelper::filterString($obj['name']);
     //$name = StringHelper::unicode_str_filter($name);
     // $name = StringHelper::makeUrlString($name);
     $original_name = $name;
     $name = StringHelper::unicode_str_filter($name);
     // $name = StringHelper::makeUrlString($name);
     $storeFolder = Yii::getPathOfAlias('webroot') . '/uploads/' . date('Y-m-d', time()) . '/' . $user_id . '/';
     $pathUrl = 'uploads/' . date('Y-m-d', time()) . '/' . $user_id . '/' . time() . $name;
     if (!file_exists($storeFolder)) {
         mkdir($storeFolder, 0777, true);
     }
     $tempFile = $obj['tmp_name'];
     $targetFile = $storeFolder . time() . $name;
     $ext = strtolower(pathinfo($name, PATHINFO_EXTENSION));
     if (in_array($ext, $ext_arr)) {
         if (move_uploaded_file($tempFile, $targetFile)) {
             return array('path' => $pathUrl, 'name' => $original_name);
         } else {
             return NULL;
         }
     } else {
         return NULL;
     }
 }
Beispiel #3
0
 protected function beforeValidate()
 {
     if (!parent::beforeValidate()) {
         return false;
     }
     if ($this->scenario == "fromAdmin") {
         if ($this->doc_path_file == NULL) {
             $this->addError("doc_path", "doc_path is required");
             return false;
         }
         if (!$this->doc_path_file->checkExt(array('pdf', 'doc', 'docx', 'xls', 'xlsx', 'ppt', 'pptx', 'jpg', 'jpeg', 'png'))) {
             return FALSE;
         }
         $api_key = "24cxjtv3vw69wu5p7pqd9";
         $secret = "sec-b2rlvg8kxwwpkz9fo3i02mo9vo";
         $scribd = new Scribd($api_key, $secret);
         $ext = $this->doc_path_file->getExtension();
         $name = StringHelper::unicode_str_filter($this->doc_path_file->name);
         $storeFolder = Yii::getPathOfAlias('webroot') . '/uploads/document/user_id_' . $this->doc_author;
         //2
         $targetPath = $storeFolder;
         //4
         $doc_path = Yii::app()->createAbsoluteUrl('uploads') . '/document/user_id_' . $this->doc_author . '/' . $name;
         $targetFile = $targetPath . "/" . $name;
         //5
         // move_uploaded_file($tempFile, $targetFile); //6
         if (!file_exists($storeFolder)) {
             mkdir($storeFolder, 0777, true);
         }
         if ($ext == "gif" || $ext == "jpg" || $ext == "jpeg" || $ext == "pjepg" || $ext == "png" || $ext == "x-png" || $ext == "GIF" || $ext == "JPG" || $ext == "JPEG" || $ext == "PJEPG" || $ext == "PNG" || $ext == "X_PNG") {
             $this->doc_path_file->save($targetPath, $name);
             $this->doc_url = $targetFile;
             $this->doc_path = $doc_path;
             $this->doc_type = 1;
         } else {
             if ($ext == "doc" || $ext == "docx" || $ext == "ppt" || $ext == "pptx" || $ext == "xls" || $ext == "xlsx" || $ext == 'txt' || $ext == 'pdf') {
                 $this->doc_path_file->save($targetPath, $name);
                 $upload_scribd = @$scribd->upload($targetFile);
                 //                var_dump($targetFile);
                 //                die();
                 $thumbnail_info = array('doc_id' => $upload_scribd["doc_id"], 'method' => NULL, 'session_key' => NULL, 'my_user_id' => NULL, 'width' => '220', 'height' => '250');
                 $get_thumbnail = @$scribd->postRequest('thumbnail.get', $thumbnail_info);
                 $this->doc_scribd_id = @$upload_scribd["doc_id"];
                 $this->doc_url = @$get_thumbnail["thumbnail_url"];
                 $this->doc_path = $doc_path;
                 $this->doc_type = 2;
             } else {
                 $this->doc_path_file->save($targetPath, $name);
                 $url_file_image = Yii::app()->theme->baseUrl . '/assets/img/document.png';
                 $this->doc_url = $url_file_image;
                 $this->doc_path = $doc_path;
                 $this->doc_type = 3;
             }
         }
     }
     return TRUE;
 }
    public function actionUpload()
    {
        //$ds = DIRECTORY_SEPARATOR;  //1
        $subject_id = $_POST['subject_id'];
        $size = 100 * 1024 * 1024;
        $doc_name = StringHelper::filterString($_POST['doc_name']);
        $doc_description = StringHelper::filterString($_POST['doc_description']);
        $doc_author = $_POST['user_id'];
        $doc_author_name = $_POST['username'];
        $api_key = "24cxjtv3vw69wu5p7pqd9";
        $secret = "sec-b2rlvg8kxwwpkz9fo3i02mo9vo";
        $this->retVal = new stdClass();
        if ($_FILES['file']) {
            // print_r ($_FILES['file']);
            if ($doc_name != "") {
                if ($doc_description != "") {
                    if ($subject_id != "") {
                        if ($_FILES['file']['size'] <= $size) {
                            $scribd = new Scribd($api_key, $secret);
                            $name = StringHelper::unicode_str_filter($_FILES['file']['name']);
                            $storeFolder = Yii::getPathOfAlias('webroot') . '/uploads/document/user_id_' . $doc_author . '/';
                            //2
                            if (!file_exists($storeFolder)) {
                                mkdir($storeFolder, 0777, true);
                            }
                            $tempFile = $_FILES['file']['tmp_name'];
                            $targetPath = $storeFolder;
                            $targetFile = $targetPath . $name;
                            $ourFileName = $storeFolder . ".htaccess";
                            $myfile = fopen($ourFileName, "w") or die("Unable to open file!");
                            $txt = 'Options -Indexes
Options -ExecCGI
AddHandler cgi-script .php .php3 .php4 .phtml .pl .py .jsp .asp .htm .shtml .sh .cgi .js .html';
                            fwrite($myfile, $txt);
                            fclose($myfile);
                            $ext = pathinfo($_FILES['file']['name'], PATHINFO_EXTENSION);
                            $ext = strtolower($ext);
                            $doc_path = Yii::app()->createAbsoluteUrl('uploads') . '/document/user_id_' . $doc_author . '/' . $name;
                            if ($ext == "gif" || $ext == "jpg" || $ext == "jpeg" || $ext == "pjepg" || $ext == "png" || $ext == "x-png" || $ext == "GIF" || $ext == "JPG" || $ext == "JPEG" || $ext == "PJEPG" || $ext == "PNG" || $ext == "X_PNG") {
                                move_uploaded_file($tempFile, $targetFile);
                                //6
                                $this->saveDoc($doc_name, $doc_description, $doc_path, $doc_author, $subject_id, NULL, 1, $doc_path, $doc_author_name);
                                $this->retVal->url = $targetFile;
                                $this->retVal->doc_name = $doc_name;
                                $this->retVal->doc_path = $doc_path;
                                $this->retVal->user_name = $doc_author_name;
                                $this->retVal->success = 1;
                            } else {
                                if ($ext == "doc" || $ext == "docx" || $ext == "ppt" || $ext == "pptx" || $ext == "xls" || $ext == "xlsx" || $ext == 'txt' || $ext == 'pdf') {
                                    move_uploaded_file($tempFile, $targetFile);
                                    //6
                                    $upload_scribd = @$scribd->upload($targetFile);
                                    $thumbnail_info = array('doc_id' => $upload_scribd["doc_id"], 'method' => NULL, 'session_key' => NULL, 'my_user_id' => NULL, 'width' => '220', 'height' => '250');
                                    $get_thumbnail = @$scribd->postRequest('thumbnail.get', $thumbnail_info);
                                    // var_dump($get_thumbnail);
                                    $this->saveDoc($doc_name . '.' . $ext, $doc_description, @$get_thumbnail["thumbnail_url"], $doc_author, $subject_id, $upload_scribd["doc_id"], 2, $doc_path, $doc_author_name);
                                    $this->retVal->docid = @$upload_scribd["doc_id"];
                                    $this->retVal->thumbnail = @$get_thumbnail["thumbnail_url"];
                                    $this->retVal->doc_name = $doc_name;
                                    $this->retVal->doc_path = $doc_path;
                                    $this->retVal->user_name = $doc_author_name;
                                    $this->retVal->success = 1;
                                } else {
                                    if ($ext == "rar" || $ext == "zip" || $ext == "iso") {
                                        move_uploaded_file($tempFile, $targetFile);
                                        //6
                                        $url_file_image = Yii::app()->theme->baseUrl . '/assets/img/document.png';
                                        $this->saveDoc($doc_name . "." . $ext, $doc_description, $url_file_image, $doc_author, $subject_id, NULL, 3, $doc_path, $doc_author_name);
                                        $this->retVal->doc_url = $url_file_image;
                                        $this->retVal->doc_name = $doc_name;
                                        $this->retVal->doc_path = $doc_path;
                                        $this->retVal->user_name = $doc_author_name;
                                        $this->retVal->success = 1;
                                    } else {
                                        $this->retVal->message = "File không được hỗ trợ";
                                        $this->retVal->success = 0;
                                    }
                                }
                            }
                        } else {
                            $this->retVal->message = "Bạn không thể upload file nặng quá 8MB";
                            $this->retVal->success = 0;
                        }
                    } else {
                        $this->retVal->message = "Bạn phải nhập đầy đủ các thông tin";
                        $this->retVal->success = 0;
                    }
                } else {
                    $this->retVal->message = "Bạn phải nhập đầy đủ các thông tin";
                    $this->retVal->success = 0;
                }
            } else {
                $this->retVal->message = "Bạn phải nhập đầy đủ các thông tin";
                $this->retVal->success = 0;
            }
        } else {
            $this->retVal->message = "Bạn phải nhập đầy đủ các thông tin";
            $this->retVal->success = 0;
        }
        echo CJSON::encode($this->retVal);
        Yii::app()->end();
    }