What i have done is i have put direct access restriction on one of the directories that contain pdf. Now i have to open the files in iframe. when i create a copy of file and pass that files address to iframe src it downloads instead of opening the file.
below is my code.
<?php
session_start();
?>
<!DOCTYPE html>
<html>
<head>
<title><?php echo $_GET['filename'] ?></title>
<meta name="viewport" content="width=device-width, initial-scale=1" />
</head>
<body>
<?php
$mime = mime_content_type($_GET['filename']);
if(strstr($mime, "image/")){
?>
<img src="fileViewer.php?filename=<?php echo $_GET['filename'] ?>" style="width: 70%;margin-left: 15%;">
<?php
}
else if(strstr($mime, "application/pdf")){
$myfile = fopen("Temps/".$_SESSION['userName']."file.pdf", "w");
fwrite($myfile, file_get_contents($_GET['filename']));
$fileName= "Temps/".$_SESSION['userName']."file.pdf";
?>
<iframe src="<?php echo $fileName ?>" style="width: 100%;height: 100vh;"></iframe>
<?php
}
else{
?>
<iframe src="fileViewer.php?filename=<?php echo $_GET['filename'] ?>" style="width: 100%;height: 100vh;"></iframe>
<?php
}
?>
</body>
</html>