daily pastebin goal
68%
SHARE
TWEET

Untitled

a guest Oct 17th, 2017 47 Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. namespace calsses;
  2. class uploadfile{
  3.  
  4.  
  5.  protected $destination;
  6.  
  7. protected $msg = [];
  8. protected $goodTypes = ['srt', 'application/octet-stream'];
  9. protected $maxSize = 700 * 1024;
  10. protected $newName;
  11. protected $zipName;
  12. protected $repalce = [' ', '-'];
  13. protected $renameDuplicates;
  14.  
  15. //FTP
  16. protected $ftp_server = "server";
  17. protected $ftp_username = "username";
  18. protected $ftp_userpass = "password";
  19. protected $ftp_dir = "/src/";
  20.  
  21.  
  22. function __construct($uploadFolder)
  23. {
  24.     if (!is_writable($uploadFolder) || !is_dir($uploadFolder)) {
  25.         throw new Exception('no folder');
  26.     }
  27.     if ($uploadFolder[strlen($uploadFolder) - 1] != '/') {
  28.         $uploadFolder .= '/';
  29.     }
  30.     $this->destination = $uploadFolder;
  31.  
  32.     //FTP
  33. }
  34.  
  35.  
  36. public function upload()
  37. {
  38.     $sub = current($_FILES);
  39.     if ($this->check($sub)) {
  40.         if ($this->moveSub($sub)) {
  41.             if ($this->makeZip()) {
  42.                 $this->moveFTP();
  43.             }
  44.         }
  45.     }
  46. }
  47.  
  48. protected function check($sub)
  49. {
  50.     if ($sub['error'] != 0) {
  51.         $this->getMessges($sub);
  52.         return false;
  53.     }
  54.     if (!$this->checkSize($sub)) {
  55.         return false;
  56.     }
  57.     if (!$this->checkType($sub)) {
  58.         return false;
  59.     } else {
  60.         return true;
  61.     }
  62. }
  63.  
  64. protected function getMessges($file)
  65. {
  66.     switch ($file['error']) {
  67.         case 1:
  68.         case 2:
  69.             $this->msg[] = $file['name'] . ' is too big max:';
  70.             break;
  71.         case 3:
  72.             $this->msg[] = $file['name'] . ' partily uploaded';
  73.             break;
  74.         case 4:
  75.             $this->msg[] = $file['name'] . ' no file selected';
  76.             break;
  77.         default:
  78.             $this->msg[] = $file['name'] . ' there is a problem wit your file';
  79.             break;
  80.     }
  81. }
  82.  
  83. public function returnMsg()
  84. {
  85.     return $this->msg;
  86. }
  87.  
  88. protected function checkType($sub)
  89. {
  90.     if (!in_array($sub['type'], $this->goodTypes)) {
  91.         $this->msg[] = 'bad type';
  92.         return false;
  93.     } else {
  94.         return true;
  95.     }
  96. }
  97.  
  98. protected function checkSize($sub)
  99. {
  100.     $serverMaxSize = 1000 * 1024;
  101.     if ($sub['size'] > $serverMaxSize) {
  102.         return $this->msg[] = 'max file size';
  103.         return false;
  104.     }
  105.     if ($sub['size'] == 0) {
  106.         return $this->msg[] = '0 size';
  107.         return false;
  108.     }
  109.     if ($sub['size'] > $this->maxSize) {
  110.         return $this->msg[] = 'max file size error';
  111.         return false;
  112.     } else {
  113.         return true;
  114.     }
  115. }
  116.  
  117. protected function newName($sub)
  118. {
  119.     $this->newName = null;
  120.     $justname = pathinfo($sub['name'], PATHINFO_FILENAME);
  121.     $noSpace = str_replace(' ', '.', $justname);
  122.     $newName = $noSpace . '_' . time() . date('Ymd') . '.' . 'srt';
  123.     $this->newName = $newName;
  124.  
  125.     $exists = scandir($this->destination);
  126.     if (in_array($this->newName, $exists)) {
  127.         $i = 1;
  128.         $newName = explode('.', $newName);
  129.         $newName = array_slice($newName, 0, -1);
  130.         $newName = implode($newName, '.');
  131.         do {
  132.             $this->newName = $newName . '.' . $i++ . '.srt';
  133.         } while (in_array($this->newName, $exists));
  134.     }
  135.  
  136.     return $this->newName;
  137. }
  138.  
  139.  
  140. protected function moveSub($sub)
  141. {
  142.     $upload = move_uploaded_file($sub['tmp_name'], $this->destination . $this->newName($sub));
  143.     if ($upload) {
  144.         return $this->msg[] = 'uploaded ' . $this->newName;
  145.         return true;
  146.     } else {
  147.         return $this->msg[] = 'problem with uploading file ' . $this->newName;
  148.         return false;
  149.     }
  150.  
  151. }
  152.  
  153. protected function zipName()
  154. {
  155.     $this->zipName = null;
  156.     $this->zipName = time();
  157.     $this->zipName .= '.zip';
  158.     return $this->zipName;
  159.  
  160.  
  161. }
  162.  
  163. protected function makeZip()
  164. {
  165.     $zip = new ZipArchive();
  166.     $zip->open($this->destination . $this->zipName(), ZipArchive::CREATE);
  167.     $zip->addFile($this->destination . $this->newName, $this->newName);
  168.     $zip->setArchiveComment('Subzila');
  169.     $zip->close();
  170.     unlink($this->destination . $this->newName);//this unlik work
  171.  
  172.     if (file_exists($this->destination . $this->zipName)) {
  173.         return true;
  174.     } else {
  175.         return $this->msg[] = 'problem with zipping';
  176.         return false;
  177.     }
  178. }
  179.  
  180. function delete() {
  181.     unlink($this->destination . $this->zipName);
  182. }
  183.  
  184. protected function moveFTP()
  185. {
  186.     $ftpCon = ftp_connect($this->ftp_server);
  187.     $ftpLogin = ftp_login($ftpCon, $this->ftp_username, $this->ftp_userpass);
  188.     ftp_pasv($ftpCon, true);
  189.     $ftpPut = ftp_put($ftpCon, $this->ftp_dir . $this->zipName, $this->destination . $this->zipName, FTP_ASCII);
  190.     if ($ftpPut) {
  191.         return true;
  192.     } else {
  193.         return $this->msg[] = 'problem with uploadding';
  194.         return false;
  195.     }
  196.     ftp_close($ftpCon);
  197.     unlink($this->destination . $this->zipName);//this unlink wont work
  198.  
  199. }
RAW Paste Data
We use cookies for various purposes including analytics. By continuing to use Pastebin, you agree to our use of cookies as described in the Cookies Policy. OK, I Understand
 
Top