Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- namespace calsses;
- class uploadfile{
- protected $destination;
- protected $msg = [];
- protected $goodTypes = ['srt', 'application/octet-stream'];
- protected $maxSize = 700 * 1024;
- protected $newName;
- protected $zipName;
- protected $repalce = [' ', '-'];
- protected $renameDuplicates;
- //FTP
- protected $ftp_server = "server";
- protected $ftp_username = "username";
- protected $ftp_userpass = "password";
- protected $ftp_dir = "/src/";
- function __construct($uploadFolder)
- {
- if (!is_writable($uploadFolder) || !is_dir($uploadFolder)) {
- throw new Exception('no folder');
- }
- if ($uploadFolder[strlen($uploadFolder) - 1] != '/') {
- $uploadFolder .= '/';
- }
- $this->destination = $uploadFolder;
- //FTP
- }
- public function upload()
- {
- $sub = current($_FILES);
- if ($this->check($sub)) {
- if ($this->moveSub($sub)) {
- if ($this->makeZip()) {
- $this->moveFTP();
- }
- }
- }
- }
- protected function check($sub)
- {
- if ($sub['error'] != 0) {
- $this->getMessges($sub);
- return false;
- }
- if (!$this->checkSize($sub)) {
- return false;
- }
- if (!$this->checkType($sub)) {
- return false;
- } else {
- return true;
- }
- }
- protected function getMessges($file)
- {
- switch ($file['error']) {
- case 1:
- case 2:
- $this->msg[] = $file['name'] . ' is too big max:';
- break;
- case 3:
- $this->msg[] = $file['name'] . ' partily uploaded';
- break;
- case 4:
- $this->msg[] = $file['name'] . ' no file selected';
- break;
- default:
- $this->msg[] = $file['name'] . ' there is a problem wit your file';
- break;
- }
- }
- public function returnMsg()
- {
- return $this->msg;
- }
- protected function checkType($sub)
- {
- if (!in_array($sub['type'], $this->goodTypes)) {
- $this->msg[] = 'bad type';
- return false;
- } else {
- return true;
- }
- }
- protected function checkSize($sub)
- {
- $serverMaxSize = 1000 * 1024;
- if ($sub['size'] > $serverMaxSize) {
- return $this->msg[] = 'max file size';
- return false;
- }
- if ($sub['size'] == 0) {
- return $this->msg[] = '0 size';
- return false;
- }
- if ($sub['size'] > $this->maxSize) {
- return $this->msg[] = 'max file size error';
- return false;
- } else {
- return true;
- }
- }
- protected function newName($sub)
- {
- $this->newName = null;
- $justname = pathinfo($sub['name'], PATHINFO_FILENAME);
- $noSpace = str_replace(' ', '.', $justname);
- $newName = $noSpace . '_' . time() . date('Ymd') . '.' . 'srt';
- $this->newName = $newName;
- $exists = scandir($this->destination);
- if (in_array($this->newName, $exists)) {
- $i = 1;
- $newName = explode('.', $newName);
- $newName = array_slice($newName, 0, -1);
- $newName = implode($newName, '.');
- do {
- $this->newName = $newName . '.' . $i++ . '.srt';
- } while (in_array($this->newName, $exists));
- }
- return $this->newName;
- }
- protected function moveSub($sub)
- {
- $upload = move_uploaded_file($sub['tmp_name'], $this->destination . $this->newName($sub));
- if ($upload) {
- return $this->msg[] = 'uploaded ' . $this->newName;
- return true;
- } else {
- return $this->msg[] = 'problem with uploading file ' . $this->newName;
- return false;
- }
- }
- protected function zipName()
- {
- $this->zipName = null;
- $this->zipName = time();
- $this->zipName .= '.zip';
- return $this->zipName;
- }
- protected function makeZip()
- {
- $zip = new ZipArchive();
- $zip->open($this->destination . $this->zipName(), ZipArchive::CREATE);
- $zip->addFile($this->destination . $this->newName, $this->newName);
- $zip->setArchiveComment('Subzila');
- $zip->close();
- unlink($this->destination . $this->newName);//this unlik work
- if (file_exists($this->destination . $this->zipName)) {
- return true;
- } else {
- return $this->msg[] = 'problem with zipping';
- return false;
- }
- }
- function delete() {
- unlink($this->destination . $this->zipName);
- }
- protected function moveFTP()
- {
- $ftpCon = ftp_connect($this->ftp_server);
- $ftpLogin = ftp_login($ftpCon, $this->ftp_username, $this->ftp_userpass);
- ftp_pasv($ftpCon, true);
- $ftpPut = ftp_put($ftpCon, $this->ftp_dir . $this->zipName, $this->destination . $this->zipName, FTP_ASCII);
- if ($ftpPut) {
- return true;
- } else {
- return $this->msg[] = 'problem with uploadding';
- return false;
- }
- ftp_close($ftpCon);
- unlink($this->destination . $this->zipName);//this unlink wont work
- }
Add Comment
Please, Sign In to add comment