1

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.

Learner
  • 611
  • 3
  • 12
  • 28
  • try this [1]: http://stackoverflow.com/questions/4346117/how-can-i-view-open-a-word-document-in-my-browser-using-with-php-or-html – Rastislav Struhár Mar 31 '15 at 06:04
  • What actually is your problem? – Keep Coding Mar 31 '15 at 06:05
  • http://stackoverflow.com/questions/1251956/is-there-a-pdf-parser-for-php – Ghostman Mar 31 '15 at 06:05
  • @Testing I want to open the cv but my code open a download window :( – Learner Mar 31 '15 at 06:51
  • Would you dont mind posting your complete code? Lets fix it now. Post your complete code with proper flow how and where code flow is going in site. – Keep Coding Mar 31 '15 at 07:04
  • Let me understand: are you trying to open IN A BROWSER a word document? – STT LCU Mar 31 '15 at 07:08
  • @STTLCU No not in the BROWSER I want to view CV in Microsoft word not downloading it – Learner Mar 31 '15 at 07:10
  • You want to see a remote file with a local program without downloading it, right? Good luck. – STT LCU Mar 31 '15 at 07:13
  • @STTLCU No OMG I have an issue in writing a clear question :") ,, I want to enable users of opening or downloading their CVs of doc format witch is stored in db not in file system .. sorry for the misunderstanding – Learner Mar 31 '15 at 09:17
  • oh, now that makes more sense. well, that's outside of your control. It relies on the user's browser preference. You can't auto-open a file unless the user has already decided to automatically do so. Standard behaviour is prompt-download-action where action can either be save or open file. – STT LCU Mar 31 '15 at 10:02
  • @STTLCU alright I wanna follow the standers but how ? – Learner Mar 31 '15 at 10:56

2 Answers2

0

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.

Rasika
  • 1,980
  • 13
  • 19
  • I change it to application/msword for doc file But still it open a downloading window :( – Learner Mar 31 '15 at 06:55
  • Is it a doc or a docx? docx files are gzip files. Try with a PDF first to see if it works. – Rasika Mar 31 '15 at 11:16
  • I allow doc, docx and pdf, None is working as I want even now I tried the pdf mime content type in browsing member profile who's CV is in pdf format it still give me a window to select where to download that pdf :( – Learner Mar 31 '15 at 12:53
  • I could not open any version both pdf and docx it says that its corrupted and the file name that appears in the window is the server name, Could you guess the error ? – Learner Mar 31 '15 at 12:59
0

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 />";
        }
    }
?>
Keep Coding
  • 636
  • 9
  • 26
  • Even though I added case "docx": $ctype="application/vnd.openxmlformats-officedocument.wordprocessingml.document"; break; It still ask where to download :"( – Learner Mar 31 '15 at 07:30
  • See edited answer. You should use your code this way. – Keep Coding Mar 31 '15 at 07:30
  • I store CVs in db not in system files ! – Learner Mar 31 '15 at 08:39
  • Also I want to enable (open CV and download) both options to the users. – Learner Mar 31 '15 at 08:41
  • But CVs should be somewhere physically present or not? :) – Keep Coding Mar 31 '15 at 08:44
  • Means? Open in browser and then download in browser? 2 processes? – Keep Coding Mar 31 '15 at 08:45
  • OK I will explain more clearly : When User open his/her profile, he/she can click on "open my Cv link" then my site will open a window asking the user to select open or download ,, just link those pictuers in [this question]:(http://stackoverflow.com/questions/16515046/force-file-download-pdf-open-with-box-says-its-a-firefox-document) – Learner Mar 31 '15 at 09:11