Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- <?php
- //Errors
- ini_set('display_errors', 1);
- ini_set('display_startup_errors', 1);
- error_reporting(E_ALL);
- //Errors
- // Session
- if (!session_id()) {
- session_start();
- }
- $ses_id = session_id();
- // Session
- require '../classes/db.php';
- class imgUploader
- {
- public $exts = array( ".png", ".gif", ".png", ".jpg", ".jpeg" ); //all the extensions that will be allowed to be uploaded
- public $maxSize = 1024*1024*10; //if you set to "0" (no quotes), there will be no limit
- public $uploadTarget = "../uploads/"; //make sure you have the '/' at the end
- public $fileName = ""; //this will be automatically set. you do not need to worry about this
- public $tmpName = ""; //this will be automatically set. you do not need to worry about this
- public function startUpload($file, $logoType, $id, $ses_id)
- {
- $this->fileName = $file['name'][$logoType];
- $this->tmpName = $file['tmp_name'][$logoType];
- $this->tempExt = explode('/',$file['type'][$logoType]);
- $this->extension = $this->tempExt[1];
- $this->id = $id;
- $this->ses_id = $ses_id;
- $this->logoType = $logoType;
- if( !$this->isWritable() )
- {
- die( '{"status":0,"data":null,"error":["Musisz ustawić CHMOD na 777"]}' );
- }
- if( !$this->checkExt() )
- {
- die( '{"status":0,"data":null,"error":["Plik obrazka jest nieprawidłowy"]}' );
- }
- if( !$this->checkSize() )
- {
- die( '{"status":0,"data":null,"error":["Plik jest zbyt duży"]}' );
- }
- if( $this->uploadIt() )
- {
- //echo '{"status":1,"data":null,"error":[]}';
- }
- if( $fileNameToSave = $this->getFileName() )
- {
- echo '{"status":1,"data":null,"error":[]}';
- }
- else
- {
- echo '{"status":0,"data":null,"error":["Wystąpił błąd"]}';
- }
- }
- // Blob
- public function startUpload2($file, $logoType, $id, $ses_id)
- {
- $this->fileName = $file['name'];
- $this->tmpName = $file['tmp_name'];
- $this->id = $id;
- $this->ses_id = $ses_id;
- $this->logoType = $logoType;
- if( !$this->isWritable() )
- {
- die( '{"status":0,"data":null,"error":["Musisz ustawić CHMOD na 777"]}' );
- }
- if( !$this->checkSize() )
- {
- die( '{"status":0,"data":null,"error":["Plik jest zbyt duży"]}' );
- }
- if( $this->uploadIt2() )
- {
- //echo '{"status":1,"data":null,"error":[]}';
- }
- if( $fileNameToSave = $this->getFileName2() )
- {
- //echo '{"status":1,"data":null,"error":[]}';
- }
- if( $this->saveDataToDB($fileNameToSave, $logoType, $id, $ses_id) )
- {
- echo '{"status":1,"data":null,"error":[]}';
- }
- else
- {
- echo '{"status":0,"data":null,"error":["Wystąpił błąd"]}';
- }
- }
- public function uploadIt()
- {
- return ( move_uploaded_file( $this->tmpName, $this->uploadTarget . $this->id . '-' . $this->ses_id . '-' . $this->logoType . '.' . $this->extension ) ? true : false );
- }
- public function uploadIt2()
- {
- return ( move_uploaded_file( $this->tmpName, $this->uploadTarget . $this->id . '-' . $this->ses_id . '-' . $this->logoType ) ? true : false );
- }
- public function checkSize()
- {
- return ( ( filesize( $this->tmpName ) > $this->maxSize ) ? false : true );
- }
- public function getExt()
- {
- return strtolower( substr( $this->fileName, strpos( $this->fileName, "." ), strlen( $this->fileName ) - 1 ) );
- }
- public function checkExt()
- {
- return ( in_array( $this->getExt(), $this->exts ) ? true : false );
- }
- public function isWritable()
- {
- return ( is_writable( $this->uploadTarget ) );
- }
- public function getFileName2()
- {
- return $this->uploadTarget . $this->id . '-' . $this->ses_id . '-' . $this->logoType;
- }
- public function getFileName()
- {
- return $this->uploadTarget . $this->id . '-' . $this->ses_id . '-' . $this->logoType . '.' . $this->extension;
- }
- public function saveDataToDB($fileNameToSave, $logoType, $id, $ses_id)
- {
- $stmt = $pdo->prepare("UPDATE a_temp_session
- SET {$logoType} = :value
- WHERE session_id = :session_id AND id=:id");
- $stmt->bindValue(":session_id", $ses_id);
- $stmt->bindValue(":id", $id);
- $stmt->bindValue(":value", $fileNameToSave);
- $stmt->execute();
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement