How to use php readfile code to open PDF file in new tab (wysiwyg)

1.5k views Asked by At

I'm using following code to open a PDF document from database. My problem is the PDF document is opened in the current tab but I want it to open in a new tab instead.

$query = "SELECT name, type, size, content FROM files WHERE id = '$id'";

$result = mysql_query($query) or die('Error, query failed');
list($name, $type, $size, $content) = mysql_fetch_array($result);

header("Content-length: $size");
header("Content-type: $type");
header("Content-Disposition: inline; filename=$name");

echo $content;

I'm using redactor wysiwyg to add the PDF. Let me know if you need more info, thank you.

1

There are 1 answers

0
Evelyn On BEST ANSWER

Solved my problem to open the PDF in new tab. We can't open new tab by using php code. Only can do it by adding the "target="_blank" in redactor.js

Following is part of the code from redactor WYSIWYG. You can found the "target="_blank" inside the code.

fileUploadCallback: function(json)
{
    var data = $.parseJSON(json);

    var text = $('#redactor_filename').val();
    if (text == '') text = data.filename;
    var link = '<a href="' + data.filelink + '" target="_blank">' + text + '</a>';

    // chrome fix
    if ($.browser.webkit && !!window.chrome) link = link + '&nbsp;'; 

    if ($.browser.msie) 
    {
        if (text != '') $(this.doc.getElementById('span' + this.spanid)).replaceWith(a);
    else $(this.doc.getElementById('span' + this.spanid)).after(link).remove();
        this.syncCode();
    }
    else this.execCommand('inserthtml', link);

    // file upload callback
    if (typeof this.opts.fileUploadCallback == 'function') 
    {
        this.opts.fileUploadCallback(this, data);
    }

    this.modalClose();
},  

For more information, please get the code from redactor WYSIWYG website.