简体   繁体   中英

How to open a file from db in php

I created a profile page to view members info's one of the information is his/her cv. I Could manage viewing a window that have an open or download options BUT I have two problems:

1- in Chrome open option dose not appear I have download option only.

2- I cant open nor download the cv in both chrome and Firefox, the file name appear with the server name in the open or save window.

Here is my code: PHP:

if(isset($_GET['cv'])) 
 {
   $id = $_GET['cv'];

   $query = "SELECT `fileName`, `fileType`, `fileSize`, `fileContent` FROM `boardteam` WHERE `nationalID` = '$id'";

    $r = mysql_query($query) or die('Error, query failed');

    list($fname, $ftype, $fsize, $fcontent) = mysql_fetch_array($r);
    header('Content-Type: application/vnd.openxmlformats-officedocument.wordprocessingml.document');
    header("Content-Disposition: attachment; filename=$fname");
    header('Content-Length: ' . filesize($fsize));
    ob_clean();
    flush(); 
    readfile($fname);
    exit;
}

HTML:

<a href='browseMember.php?cv=<?php echo $member_id?>'>
<?php 
     $q = "SELECT `fileName`, `fileType`, `fileSize`, `fileContent` FROM `boardteam` WHERE `nationalID` = '$member_id'";

     $r = mysql_query($q) or die('Error, query failed');

     list($fname, $ftype, $fsize, $fcontent) = mysql_fetch_array($r);
     if ($fname)
       {
         echo "click here to open member CV";
       }
?>
</a>

UPDATE:

Lets assume cv file type is docx, If it works I will switch to case doc and pdf and change content type.

It is because of your content type setting. Your browser doesn't know which application to open the document with when it is binary. Try changing the content type with a more descriptive content type that lets the browser select the appropriate application. For example for PDF it would be application/x-pdf . Refer the MIME Media type directory for others.

Are you looking for something like this:

Since, you only want to open it in browser so, it should work for you.

Please replace YOUR_DIRECOTRY_WHERE_FILE_IS_PRESENT_PHYSICALY with your directory name where you document files are physically present.

Your browseMember.php code will be like this:

<?php
if(isset($_GET['cv'])) 
 {
    $id = $_GET['cv'];

    $query = "SELECT `fileName`, `fileType`, `fileSize`, `fileContent` FROM `boardteam` WHERE `nationalID` = '$id'";

    $r = mysql_query($query) or die('Error, query failed');

    while ($rowDoc = mysql_fetch_array($r)){ ?>
    <div id="wrapper">
        <div><a href="YOUR_DIRECOTRY_WHERE_FILE_IS_PRESENT_PHYSICALY/<?=$rowDoc['fileName']; ?>" target="_blank" class="text_2" style="text-decoration:none;" ><strong><? echo($row_pdf['fileName']);?></strong></a></div>
    </div>
<?php
echo "<br />";
        }
    }
?>

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM