<?php

namespace SCF\Additionals;

class Download
{

    /**
     * @var string
     */
    private $absolutePath;

    /**
     * @var string
     */
    private $fileName;

    public function __construct()
    {

    }

    /**
     * @param string $fileName
     */
    public function setFileName($fileName)
    {
        $this->fileName = $fileName;
    }

    /**
     * @return string
     */
    public function getFileName()
    {
        return $this->fileName;
    }

    /**
     * @return string
     */
    public function getAbsolutePath()
    {
        return $this->absolutePath;
    }

    /**
     * @param string $absolutePath
     */
    public function setAbsolutePath($absolutePath)
    {
        $this->absolutePath = $absolutePath;
    }

    /**
     * Download Control. Starts Downloads with the selected File in $tmpFile
     * Absolute Path must be given!
     */
    public function startDownloadFile()
    {
        $tmpPath = $this->getAbsolutePath();
        $tmpFile = $this->getFileName();

        $fh = fopen($tmpPath . $tmpFile, "r");
        header("Cache-Control: must-revalidate, post-check=0, pre-check=0");
        header('Content-Description: File Transfer');
        header('Content-Type: application/octed-stream');
        header('Content-Length: ' . @filesize($tmpPath . $tmpFile));
        header("Content-Disposition: attachment; filename=" . $tmpFile);
        header("Content-Transfer-Encoding: binary");

        while (true) {
            echo fgets($fh, 4096);
            if (feof($fh)) break;
            if (connection_status() != 0) break;
        }
        fclose($fh);

    }
}