Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Download excel file instead of store in Folder #102

Closed
boedyirh opened this issue Feb 5, 2019 · 2 comments
Closed

Download excel file instead of store in Folder #102

boedyirh opened this issue Feb 5, 2019 · 2 comments
Labels
common Common problems

Comments

@boedyirh
Copy link

boedyirh commented Feb 5, 2019

Thank you for this great extension,
This extension is very fast. But How do I download generated excel file. instead of storing in folder?
Thank,
Boedy

@viest
Copy link
Owner

viest commented Feb 5, 2019

Hi @boedyirh

Thank you for your support, extensions do not have built-in direct download, but can be implemented through the following code:

function getTmpDir(): string
{
    if (ini_get('upload_tmp_dir') !== false) {
        if ($temp = ini_get('upload_tmp_dir')) {
            if (file_exists($temp)) {
                return realpath($temp);
            }
        }
    }

    return realpath(sys_get_temp_dir());
}

$config = [
    'path' => getTmpDir() . '/',
];

$fileName   = 'tutorial01.xlsx';
$xlsxObject = new \Vtiful\Kernel\Excel($config);

// Init File
$fileObject = $excel->fileName($fileName);

// Writing data to a file ......

// Outptu
$filePath = $fileObject->output();

// Set Header
header('Content-Type: application/vnd.ms-excel');
header('Content-Disposition: attachment;filename="' . $fileName . '"');
header('Cache-Control: max-age=0');

if (copy($filePath, 'php://output') === false) {
    // Throw exception
}

// Delete temporary file
@unlink($filePath);

I hope this will help you.

@viest viest pinned this issue Feb 5, 2019
@viest viest added help wanted common Common problems and removed help wanted labels Feb 5, 2019
@boedyirh
Copy link
Author

boedyirh commented Feb 5, 2019

Thanks it works! I am able to download it.
I modify MIME Type to match XLSX (Excel 2007).
header("Content-Type: application/vnd.openxmlformats-officedocument.spreadsheetml.sheet");

@boedyirh boedyirh closed this as completed Feb 5, 2019
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
common Common problems
Projects
None yet
Development

No branches or pull requests

2 participants