I want to download pdf files or any other file but my main motive is to download pdf files. Is it possible instead of download we can see the pdf files on browser when user submit a query.
There is my upload code this works fine`
<!DOCTYPE html>
<html>
<body>
<form method="post" enctype="multipart/form-data">
<table width="350" border="0" cellpadding="1" cellspacing="1" class="box">
<tr>
<td width="246">
<input type="hidden" name="MAX_FILE_SIZE" value="2000000">
<input name="userfile" type="file" id="userfile">
</td>
<td width="80"><input name="upload" type="submit" class="box" id="upload" value=" Upload "></td>
</tr>
</table>
</form>
</body>
</html>
<?php
$uploadDir = 'C:\wamp\www\images\passport images';
if(isset($_POST['upload']))
{
$fileName = $_FILES['userfile']['name'];
$tmpName = $_FILES['userfile']['tmp_name'];
$fileSize = $_FILES['userfile']['size'];
$fileType = $_FILES['userfile']['type'];
$filePath = $uploadDir . $fileName;
$result = move_uploaded_file($tmpName, $filePath);
if (!$result) {
echo "Error uploading file";
exit;
}
$servername = "localhost";
$username = "root";
$password = "";
$dbname = "db_dat";
$conn = mysqli_connect($servername, $username, $password, $dbname);
if(!get_magic_quotes_gpc())
{
$fileName = addslashes($fileName);
$filePath = addslashes($filePath);
}
$query = "INSERT INTO upload2 (name, size, type, path ) VALUES ('$fileName', '$fileSize', '$fileType', '$filePath')";
mysqli_query($conn,$query) or die('Error, query failed : ' . mysql_error());
echo "<br>Files uploaded<br>";
}
?>
**Now problem in download code**. Actually I want user submit a query in text box acc. to that text as well as it's image which is scanned So in pdf format shows to him
<?php
if(isset($_GET['id']))
{
$servername = "localhost";
$username = "root";
$password = "";
$dbname = "db_dat";
$conn = mysqli_connect($servername, $username, $password, $dbname);
$id = $_GET['id'];
$query = "SELECT name, type, size, path FROM upload2 WHERE id = '1'";
$result = mysqli_query($conn,$query) or die('Error, query failed');
list($name, $type, $size, $filePath) = mysqli_fetch_array($result);
header("Content-Disposition: attachment; filename='".$name."'");
header("Content-length: $size");
header("Content-type: $type");
echo "<a href=" . $uploadDir .($row['userfile']) . ">
{$row['userfile']}</a>";
exit;
}
?>
`
Your headers tell the browser that file data will follow:
header("Content-Disposition: attachment; filename='".$name."'");
[…]
But instead of the file data you just output a link:
echo "<a href=" . $uploadDir .($row['userfile']) . ">{$row['userfile']}</a>";
This will result in error. You have two options to fix it. Either output the link without misleading headers. When user clicks the link server will send the file to the browser. If the mime type is known to the server it will set the headers for you.
Alternatively send file data instead of link after your headers (you can read and output your file data with readfile
command):
header("Content-Disposition: attachment; filename='".$name."'");
header("Content-length: $size");
header("Content-type: $type");
readfile($uploadDir . $row['userfile']);
here is my download function that you can use to download any file.
function dawnload_file($path) {
if (file_exists($path) && is_file($path)) {
// file exist
header('Content-Description: File Transfer');
header('Content-Type: application/octet-stream');
header('Content-Disposition: attachment; filename=' . basename($path));
header('Content-Transfer-Encoding: binary');
header('Expires: 0');
header('Cache-Control: must-revalidate');
header('Pragma: public');
header('Content-Length: ' . filesize($path));
set_time_limit(0);
@readfile($path);//"@" is an error control operator to suppress errors
} else {
// file doesn't exist
die('Error: The file ' . basename($path) . ' does not exist!');
}
}
and here is my function for opening pdf files in the browser online.
function read_pdf_online($path){
if (file_exists($path) && is_file($path)){
// file exist
header("Pragma: public"); // required
header("Expires: 0");
header("Cache-Control: must-revalidate, post-check=0, pre-check=0");
header("Cache-Control: private",false); // required for certain browsers
header("Content-Type: application/pdf");
header("Content-Transfer-Encoding: binary");
header("Content-Length: ".filesize($path));
@readfile( $path );
else {
// file doesn't exist
die('File Not Found: ' . basename($path));
}
}