Set the directory where Thunder-TUS should save uploads.

@param string $uploadDir

@return \ThunderTUS\Server
@throws \ThunderTUS\ThunderTUSException
169062
public function setUploadDir(string $uploadDir): Server
	{

		$uploadDir = \realpath($uploadDir);
		if (!is_dir($uploadDir))
			throw new ThunderTUSException("Invalid upload directory. Path doesn't exist or it isn't a directory.");

		$this->uploadDir = $uploadDir;

		return $this;
	}
182570
public function setDataDirectory(string $directory)
    {
        $directory = rtrim(str_replace(['/', '\\'], DIRECTORY_SEPARATOR, $directory), DIRECTORY_SEPARATOR);

        // Check main data directory
        $this->checkDirectory($directory, false);

        // Check/create sub directory
        $directory = $directory . DIRECTORY_SEPARATOR . self::SUB_DIRECTORY;
        $this->checkDirectory($directory, true);

        $this->dataDirectory = $directory;
    }
1360460
public function setSortDir($dir)
    {
        if (in_array($dir, [ self::DIR_ASC, self::DIR_DESC ]))
            $this->sortDir = $dir;
        else
            $this->sortDir = self::DIR_ASC;

        return $this;
    }
1333491
public function compactFiles(array $files, $uploadDir)
    {
        if ($this->type !== self::TYPE_FILE) {
            throw FormFieldException::notAFileUpload($this->tagName);
        }

        if (empty($files)) {
            throw new NoFileUploadedException("Files are missing from payload");
        }

        foreach (UploadedFile::create($this->name, $files, $uploadDir) as $uploadedFile) {
            $this->files[] = $uploadedFile;
        }
    }
174509
public function setFilePath($subdir)
    {
        $path = $this->getFilePath($subdir, '', true);
        if(is_dir($path))
            return $path;
        elseif(mkdir($path, 0777)) {
            foreach($this->owner->sizes as $size) {
                if($size['catalog']) {
                    if(mkdir($path . $size['catalog'], 0777))
                        continue;
                    else
                        return false;
                } else
                    continue;
            }
            return $path;
        } else
            return false;
    }
209520
public static function hasValidUploadDirectory($uploadName, $uploadDir)
    {
        if (!isset($_FILES[$uploadName]['name'])) {
            return false;
        }

        return \file_exists($uploadDir)
        && \is_dir($uploadDir)
        && \is_writable($uploadDir);
    }
200159
public function handleUpload($uploadDirectory, $name = null){

        if (is_writable($this->chunksFolder) &&
            1 == mt_rand(1, 1/$this->chunksCleanupProbability)){

            // Run garbage collection
            $this->cleanupChunks();
        }

        // Check that the max upload size specified in class configuration does not
        // exceed size allowed by server config
        if ($this->toBytes($this->iniGet('post_max_size')) < $this->sizeLimit ||
            $this->toBytes($this->iniGet('upload_max_filesize')) < $this->sizeLimit){
            $size = max(1, $this->sizeLimit / 1024 / 1024) . 'M';
            return array('error'=>"Server error. Increase post_max_size and upload_max_filesize to ".$size);
        }

        if ($this->isInaccessible($uploadDirectory)){
            return array('error' => "Server error. Uploads directory isn't writable");
        }

        if(!isset($_SERVER['CONTENT_TYPE'])) {
            return array('error' => "No files were uploaded.");
        } else if (strpos(strtolower($_SERVER['CONTENT_TYPE']), 'multipart/') !== 0){
            return array('error' => "Server error. Not a multipart request. Please set forceMultipart to default value (true).");
        }

        // Get size and name
        $file = $_FILES[$this->inputName];
        $size = $file['size'];

        if ($name === null){
            $name = $this->getName();
        }

        // Validate name
        if ($name === null || $name === ''){
            return array('error' => 'File name empty.');
        }

        // Validate file size
        if ($size == 0){
            return array('error' => 'File is empty.');
        }

        if ($size > $this->sizeLimit){
            return array('error' => 'File is too large.');
        }

        // Validate file extension
        $pathinfo = pathinfo($name);
        $ext = isset($pathinfo['extension']) ? $pathinfo['extension'] : '';

        if($this->allowedExtensions && !in_array(strtolower($ext), array_map("strtolower", $this->allowedExtensions))){
            $these = implode(', ', $this->allowedExtensions);
            return array('error' => 'File has an invalid extension, it should be one of '. $these . '.');
        }

        // Save a chunk
        $totalParts = isset($_REQUEST['qqtotalparts']) ? (int)$_REQUEST['qqtotalparts'] : 1;

        $uuid = $_REQUEST['qquuid'];
        if ($totalParts > 1){
        # chunked upload

            $chunksFolder = $this->chunksFolder;
            $partIndex = (int)$_REQUEST['qqpartindex'];

            if (!is_writable($chunksFolder) && !is_executable($uploadDirectory)){
                return array('error' => "Server error. Chunks directory isn't writable or executable.");
            }

            $targetFolder = $this->chunksFolder.DIRECTORY_SEPARATOR.$uuid;

            if (!file_exists($targetFolder)){
                mkdir($targetFolder);
            }

            $target = $targetFolder.'/'.$partIndex;
            $success = move_uploaded_file($_FILES[$this->inputName]['tmp_name'], $target);

            // Last chunk saved successfully
            if ($success AND ($totalParts-1 == $partIndex)){

                $target = join(DIRECTORY_SEPARATOR, array($uploadDirectory, $uuid, $name));
                //$target = $this->getUniqueTargetPath($uploadDirectory, $name);
                $this->uploadName = $uuid.DIRECTORY_SEPARATOR.$name;

                if (!file_exists($target)){
                    mkdir(dirname($target));
                }
                $target = fopen($target, 'wb');

                for ($i=0; $i<$totalParts; $i++){
                    $chunk = fopen($targetFolder.DIRECTORY_SEPARATOR.$i, "rb");
                    stream_copy_to_stream($chunk, $target);
                    fclose($chunk);
                }

                // Success
                fclose($target);

                for ($i=0; $i<$totalParts; $i++){
                    unlink($targetFolder.DIRECTORY_SEPARATOR.$i);
                }

                rmdir($targetFolder);

                return array("success" => true, "uuid" => $uuid);
            }

            return array("success" => true, "uuid" => $uuid);

        }
        else {
        # non-chunked upload

            $target = join(DIRECTORY_SEPARATOR, array($uploadDirectory, $uuid, $name));
            //$target = $this->getUniqueTargetPath($uploadDirectory, $name);

            if ($target){
                $this->uploadName = basename($target);

                if (!is_dir(dirname($target))){
                    mkdir(dirname($target));
                }
                if (move_uploaded_file($file['tmp_name'], $target)){
                    return array('success'=> true, "uuid" => $uuid);
                }
            }

            return array('error'=> 'Could not save uploaded file.' .
                'The upload was cancelled, or server error encountered');
        }
    }
723812
public function mkdir($dir)
    {
        return ($this->isConnected() === true) ? ftp_mkdir($this->getConnection(), $dir) : false;
    }
711503
public function chdir($dir)
    {
        return ($this->isConnected() === true) ? ftp_chdir($this->getConnection(), $dir) : false;
    }
123244
public function handle(): Server
	{

		// Check the upload directory
		if (!is_dir($this->uploadDir))
			throw new ThunderTUSException("Invalid upload directory. Path wasn't set, it doesn't exist or it isn't a directory.");

		// Call handlers
		$method = "handle" . $this->request->getMethod();
		if (!method_exists($this, $method))
			throw new ThunderTUSException("Invalid HTTP request method. Not TUS or ThunderTUS compliant.");

		// Check if this server supports the client protocol version
		if ($this->request->getHeaderLine("Tus-Resumable") != self::PROTOCOL_VERSION) {
			$this->response = $this->response->withStatus(412);
			return $this;
		}

		// Gather the filename from the last part of the URL and set the resource location
		$this->apiPath   = rtrim($this->apiPath, "/") . "/";
		$this->uploadDir = rtrim(realpath($this->uploadDir), \DIRECTORY_SEPARATOR) . \DIRECTORY_SEPARATOR;
		$url             = $this->request->getUri();
		$this->file      = str_replace(["\\", "/"], "", substr($url, strrpos($url, $this->apiPath) + strlen($this->apiPath)));
		$this->location  = $this->apiPath . $this->file;
		$this->file      = $this->uploadDir . $this->file;

		// Replicate the input stream so we can control its pointer
		$this->stream = fopen('php://temp', 'w+');
		stream_copy_to_stream(fopen($this->streamURI, 'r'), $this->stream);
		rewind($this->stream);

		$this->response = $this->$method();

		return $this;
	}
753143
public function setWorkingDir(string $workingDir)
    {
        $this->workingDir = $workingDir;

        $this->raw = array_merge($this->raw, ['WorkingDir' => $workingDir]);

        return $this;
    }
896821
public function copyDir($srcDir, $dstDir)
    {
        $this->returnCode = 'no';

        return $this->send('v-copy-fs-directory', $this->userName, $srcDir, $dstDir);
    }
367309
public function addFolder(string $directory, string $alias = '')
	{
		$loader = $this->getRenderer()->getLoader();

		// This can only be reliably tested with a loader using the filesystem loader's API
		if (method_exists($loader, 'addPath'))
		{
			if ($alias === '')
			{
				$alias = FilesystemLoader::MAIN_NAMESPACE;
			}

			$loader->addPath($directory, $alias);
		}

		return $this;
	}
1340022
public static function upload(String $fileName = 'upload', String $rootDir = UPLOADS_DIR) : Bool
    {
        return Upload::start($fileName, $rootDir);
    }
295752
public function upload($targetDirectory = null) {
		$helper = new FileUpload();

		if ($targetDirectory !== null) {
			$helper->withTargetDirectory($this->getStoragePath($targetDirectory));
		}

		return $helper;
	}
811822
public function setDirectory($directory)
    {
        $this->directory = $directory . DIRECTORY_SEPARATOR . $this->id;
        if (file_exists($this->directory) === false) {
            mkdir($this->directory, $this->fileMode, true);
        }
    }
721553
public function setDirectory(Directory $directory)
    {
        $this->directory = $directory;
        $this->iterator = new \ArrayIterator($directory->children());
    }