How to view our uploaded resume online with php in website

<?php
if($_SERVER["REQUEST_METHOD"] == "POST" && isset($_POST['submit']) == 'upload')
{
var_export($_POST); 

$allowedExts = array("pdf", "doc", "docx");



$temp = explode(".", $_FILES["file"]["name"]);
$extension = end($temp);
if ((($_FILES["file"]["type"] == "application/pdf")
|| ($_FILES["file"]["type"] == "application/msword")
|| ($_FILES["file"]["type"] == "application/vnd.openxmlformats-officedocument.wordprocessingml.document"))
&& ($_FILES["file"]["size"] < 20000)
&& in_array($extension, $allowedExts))
  {
  if ($_FILES["file"]["error"] > 0)
    {
    echo "Return Code: " . $_FILES["file"]["error"] . "<br>";
    }
  else
    {
    echo "Upload: " . $_FILES["file"]["name"] . "<br>";
    echo "Type: " . $_FILES["file"]["type"] . "<br>";
    echo "Size: " . ($_FILES["file"]["size"] / 1024) . " kB<br>";
    echo "Temp file: " . $_FILES["file"]["tmp_name"] . "<br>";

    if (file_exists("resume/" . $_FILES["file"]["name"]))
      {
      echo $_FILES["file"]["name"] . " already exists. ";
      }
    else
      {
      move_uploaded_file($_FILES["file"]["tmp_name"],
      "resume/" . $_FILES["file"]["name"]);
      echo "Stored in: " . "resume/" . $_FILES["file"]["name"];

      $sql = mysqli_query($con,"update user_personal_details 
      set resume='".$_FILES["file"]["name"]."' where user_id='".$_SESSION['user_id']."'");

      }
    }
  }
else
  {
  echo "Invalid file";
  }
}
?>


<form action="update_resume.php" method="post" enctype="multipart/form-data" >
<input type="file" name="file" />
<input type="submit" name="submit" value="upload" />
</form>

<?php 

echo $id1=$_SESSION['user_id'];
$sql="SELECT *FROM user_personal_details WHERE user_id='$id1'";
$res=mysqli_query($con,$sql);
while($row=mysqli_fetch_array($res))
{?>
<br/>

<br/>
<table cellpadding="5" cellspacing="3" style="border:1px solid #808080;" width="100%">
<tr>
<td class="blaaa"><b>Resume</b></td><td class="blaaa"><a href="resume/<?php echo $row['resume']; ?>" target="_blank">View Here</a></td></tr>
<?php
}
?>
</table>

Dani AI

Generated

@iamthwee is right: browsers do not reliably render .doc/.docx inline. You have two practical choices: (1) hand the file to a viewer service, or (2) convert to PDF and stream it yourself. If you want a quick viewer for Word files, you can build an Office Web Viewer URL like https://view.officeapps.live.com/op/view.aspx?src= plus the public, URL-encoded document URL. Note that Microsoft’s online viewer can only fetch files that are publicly accessible and not behind auth, and that is by design. See Microsoft’s guidance on the Online Viewers’ limitations. Plan Office Web Apps Server and Office Online Server overview.

For a privacy-friendly, cross-browser approach, store resumes outside your web root and stream PDFs through a PHP controller with the right headers. This renders inline in most browsers and avoids direct file URLs.

<?php
// example: stream a stored PDF securely
$path = "/var/appdata/resumes/$serverFileName"; // NOT web-accessible
$finfo = finfo_open(FILEINFO_MIME_TYPE);
$mime  = finfo_file($finfo, $path);
if ($mime !== 'application/pdf') { http_response_code(415); exit; }

header('Content-Type: application/pdf');
header('Content-Disposition: inline; filename="resume.pdf"');
header('X-Content-Type-Options: nosniff');
readfile($path);

A few fixes to your upload code to keep it robust, building on ’s distinction between “viewing a file” vs “regenerating” it:

  • Validate type with PHP’s Fileinfo; do not trust $_FILES['type']. PHP Fileinfo. OWASP also warns that the Content-Type header can be spoofed. OWASP Unrestricted File Upload.
  • Rename to a server-generated filename and store outside web root; serve through a controller after access checks. OWASP File Upload guidance.
  • Stream PDFs with Content-Disposition: inline (so they open in-browser) and consider X-Content-Type-Options: nosniff. MDN: Content-Disposition and MDN: X-Content-Type-Options.
  • Tiny logic bug: isset($_POST['submit']) == 'upload' does not check the value. Use isset($_POST['submit']) && $_POST['submit'] === 'upload'. Also raise your size cap; 20000 bytes is ~20 KB and far too small for real resumes.

Recommended Answers

All 4 Replies

Member Avatar for Member #120589

Is it a question, a code snippet or what?

Member Avatar for Member #46692

Maybe the OP wants to display pdfs, docs and docxs within the web browser? As in render it inside the browser.

its a question. how to view doc file online using php?????????????

I think we need to sort things out.

Viewing it as a file is the easiest, all you have to do is provide the url of the doc file and the user will be able to view it, depending on the user's desktop and browser's settings.

Viewing it as regenerated file created by parsing a doc file is a different method and it is a lot harder to achieved.

Viewing a doc file as generated by PHP from the collected data through a form is highly feasible. If you want to go for this option, you can use this .

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.