getHasError() public method

public getHasError ( ) : boolean
return boolean whether there is an error with the uploaded file. Check [[error]] for detailed error code information.
 /**
  * Загрузка изображения.
  *
  * @param UploadedFile $uploadedFile
  * @param $userId
  * @return bool
  */
 public function uploadPhoto(UploadedFile $uploadedFile, $userId)
 {
     if ($uploadedFile->getBaseName() && !$uploadedFile->getHasError()) {
         $photoName = 'profile_photo_' . $userId . '.' . $uploadedFile->getExtension();
         $photoSaveFolder = \Yii::getAlias('@app') . $this->profilePhotoFolder . '/' . $userId;
         if (!file_exists($photoSaveFolder)) {
             mkdir($photoSaveFolder);
         }
         $photoPath = $photoSaveFolder . '/' . $photoName;
         $uploadedFile->saveAs($photoPath);
         return $photoName;
     }
     return false;
 }
Example #2
0
 /**
  * upload by UploadedFile instance.
  *
  * @param UploadedFile $uploadedFile Uploaded file instance
  * @param string $dir uploading file directory.
  * @param string $savePath upload file save path. If null, call [[getSavePath()]] to generate one.
  * @return File
  */
 public function uploadByUploadFile($uploadedFile, $dir, $savePath = null)
 {
     $file = new File();
     if ($uploadedFile === null) {
         $file->error = File::UPLOAD_ERROR_NO_UPLOADED_FILE;
         return $file;
     }
     // 检查上传文件是否有错
     if ($uploadedFile->getHasError()) {
         $file->error = $uploadedFile->error;
         return $file;
     }
     if ($savePath !== null) {
         $dir = $this->getDir($savePath);
     }
     $type = $uploadedFile->getExtension();
     $file->error = $this->getErrors($uploadedFile->size, $dir, $type);
     if ($file->error !== UPLOAD_ERR_OK) {
         return $file;
     }
     if ($savePath === null) {
         $savePath = $this->getSavePath($dir, $type);
     }
     if ($uploadedFile->saveAs($savePath)) {
         $file->url = $this->savePath2Url($savePath);
     } else {
         $file->error = File::UPLOAD_ERROR_UPLOAD;
     }
     return $file;
 }