Search code examples
phpfile-typeopen-with

"Open with" dialog for custom file format


I have an file file.magicExt which is just a text file (utf-8), e.g.:

hello

I have an index.php:

<?php

echo '<a href="file.magicExt">file</a>';

What I would like is to show "open with" dialog when user click on "file" link.

Instead the browser (Firefox, Chrome) shows the file content.


Solution

  • I got it done with such file.php:

    <?php
    
    // We'll be outputting a .magicExt
    header('Content-Type: application/magicExt');
    
    // It will be called downloaded.magicExt
    header('Content-Disposition: attachment; filename="downloaded.magicExt"');
    
    // The .magicExt source is in original.magicExt
    readfile('original.magicExt');
    

    Based on PHP header() function

    and that index.php:

    echo '<a href="file.php">file</a>';