Im trying to allow my user to download the file that they select in my system. The code i have used downloads something but it says failed forbidden. I haven't used directories to store the files but rather stored the file in a longblob data type directly in the database. If someone could please show me how to get it to download successfully I would appreciate it.
<tbody>
<?php
require_once 'dbcon.php';
$query = $conn->query("SELECT fileID, filename, file, dateuploaded from empfiles WHERE eemail = '$_SESSION[login_employee]'");
$count = 1;
while($fetch = $query->fetch_array()){
?>
<tr>
<td style="height:50px;width:100px"><?php echo $count++?></td>
<td style="height:50px;width:200px"><?php echo $fetch['filename']?></td>
<td style="height:50px;width:100px"><?php echo $fetch['dateuploaded']?></td>
?>
</td>
<td colspan="2">
<td>
echo"<a download href=\"file/{$row['file']}\">Download this file</a>";
</td>
</tr>
<?php
}
?>
</tbody>
The value of the href attribute needs to be a URL.
You can't just take a blob of PDF out of the database and coerce it into a string.
You need to either base64 encode it and then convert it to a data URL (noting that there is a good change this will result in a URL that is too long for the browser to accept).
or
Create a new webservice that extracts the PDF from the database and generates the download based on, for example, the row id. Then link to that webservice with the row id in the query string.
Related
I am using following codes to create pdf.
first memberinfo.php page contains table with name, address, city etc of member from mysql database. This file is in www.example.com/admin folder
second is member_pdf.php it it used to create PDF. It is in www.example.com/admin/do-pdf/dompdf/ folder
Code in memberinfo.php :
<?
include("../include/function.php"); // to connect database
// Create an object for an class
$bsq=new bsq();
//use class function to connect the database
$bsq->connect_db();
//=================================================================================
$reg_id=$_GET["reg_id"];
$where="reg_id='".$_GET["reg_id"]."'";
$web_dataRS=$bsq->webdreamselect('tablename',$where,'','','','');
$member_data=mysql_fetch_array($web_dataRS);
?>
//This Table Is Shown When I click On View Details Link On Member's List Page...
<table>
<tr>
<td> Name </td><td><?=$member_data['name'];?></td>
<td> Address </td><td><?=$member_data['address'];?></td>
..
..
..
</tr>
</table>
Code In member_pdf.php :
<?php
require_once("dompdf_config.inc.php");
ob_start();
//be sure this file exists, and works outside of web context etc.)
require("http://example.com/admin/memberinfo.php");
$dompdf = new DOMPDF();
$dompdf->load_html(ob_get_clean());
$dompdf->render();
$dompdf->stream("file.pdf");
?>
Now problem is , PDF is created But table TDs containing $member_data['name'] , $member_data['address'] etc are empty.....
See Image =
This might have been already answered, yes, I used Google, and the search here stackoverflow, but the problem remains...
I'm using TCPDF library to generate PDF file, it works just fine, but I have the following situation, in order for my to generate a PDF ot just the HTML I need to put a few foreach and while's and IF's inside the HTML so that I can get the layout that the user is requesting...
so...
if(isset($_POST['submit'])) {
$ids = htmlentities($_POST['id'], ENT_NOQUOTE;
$con = conectionDB();
$query 'SELECT * FROM books WHERE id = "$ids"';
$doit = $con->query($query);
// at this point everything is file
// a few if's and we are done with the fetching "books" data
// a few other tuff that is required from the library nothing fancy...
// now here is the hard part
// next line will build my layout to display my PDF
$build_HTML = <<<EOD
<style>
.clasess {...}
.nother {...}
</style>
<table width="100%" border="1" cellpadding="0" bordercolor="#333333" >
<tr>
<td>Author</td>
<td>Books</td>
</tr>
<tr>
<td> Jong </td>
<td>
<table>
<tr>
<td>Title</td><td>Year</td>
</tr>
// Here is the problem I need to put a query to fetch the related data from
// another table
$books = conectionDB();
$bookQ = "SELECT * FROM titles WHERE name = '$author_name'";
$doitTitles = $books->query($bookQ);
if ($doitTitles->num_row > 1) {
while($dos = $doitTitles->fetch_assoc()){
// my td's, but this doest work...
}
}
</table>
</td>
</tr>
</table>
EOD;
$pdf->writeHTML($build_HTML, true, false, false, false, '');
} else {
// Go back...
}
As you can see I need that query right there, you may have notice that I have $doitTitles->num_row > 1 why? because if there is more than 1 tittle the layout would different if there is only 1 record...
We know that, that wont work, so the question is, is there another way to do that?
now, before the user go to the PDF, I display the information in plain html, which mean that the HTML that I use before the user go in to the PDF will be the same... so I was wondering, there another library that I can use to render the HTML in to PDF instead of building the PDF from inside the file... the user is able to see the result so those result just put 'em in a PDF...
Thank you!
I have the solution, so the thing is that I'm trying to output an html that is been build with dynamic content, if that was to be show as any other page, my_page.html there is no problem, but in PDF using <<
$raw_html = 'Tables';
// a few while and foreach's and queries
$raw_html .='Closed tables';
// Ready the HTML
$html = <<<EOD
$raw_html
EOD;
$pdf->writeHTML($html, true, false, false, false, '');
$pdf->Output('pdf_file_with_dynamic_html.pdf', 'I');
// the end.
by preparing the html before the output it gives me the data I need the way i need it...
that solved my problem.
I want to generate XLS files with a PHP script from a MySQL database. The big problem here is that when I open the new exported .xls file, I see the values OK (i.e. it is correctly formatted) but the colour of the field changed to white. However, I need the colour unchanged, as is by default in Excel.
Here is the PHP script that I use to extract data from my database.
<?php
include 'connect.php';
$result = mysql_query('SELECT * FROM projects2');
?>
<center><h1>Lista valorilor din tabela</h1>
<h2>Exporta lista Clienti</h2></center>
<?php
include_once 'tabel_clientipm.php';
?>
The PHP file used to generate the XLS file is:
<?php
include 'connect.php';
$result = mysql_query('SELECT * FROM projects2');
if (isset($_GET['exporta_lista_clienti'])) {
$filename = 'raportnou.xls';
header("Content-type: application/ms-excel");
header("Content-Disposition: attachment; filename=$filename");
include_once 'tabel_clientipm.php';
exit();
}
?>
I added the tabel_clientipm.php:
<center>
<table border="1">
<tr>
<th>surname</th>
<th>name</th>
<th>age</th>
</tr>
<?php
while ($client = mysql_fetch_assoc($result)) {
?>
<tr>
<td><?php echo $client['surname'];?></td>
<td><?php echo $client['name'];?></td>
<td><?php echo $client['age'];?></td>
</tr>
<?php
}
?>
</table>
</center>
If you want to create real .xls file NOT csv or html hidden in .xls extension use PHPExcel or
It supports the following formats.
BIFF 8 (.xls) Excel 95 and above
Office Open XML (.xlsx) Excel 2007 and above
If PHPExcel is slow for you check these alternatives (provided by an author of PHPExcel. All of them are faster than PHPExcel
If you export as a csv, you can probably import the data as colorless.
Trying to get the value of Internet Data Volume Balance - the script should echo 146.30mb
New to all these, having a look at all the tutorials.
How can this be done?
<tr >
<td bgcolor="#F8F8F8"><div align="left"><B><FONT class="tplus_text">Account Status</FONT></B></div></td>
<td bgcolor="#FFFFFF"><div align="left"><FONT class="tplus_text">You exceeded your allowed credit.</FONT></div></td>
</tr>
<tr >
<td bgcolor="#F8F8F8"><div align="left"><B><FONT class="tplus_text">Period Free Time Remaining</FONT></B></div></td>
<td bgcolor="#FFFFFF"><div align="left"><FONT class="tplus_text">0:00:00 hours</FONT></div></td>
</tr>
<tr >
<td bgcolor="#F8F8F8"><div align="left"><B><FONT class="tplus_text">Internet Data Volume Balance</FONT></B></div></td>
<td bgcolor="#FFFFFF"><div align="left"><FONT class="tplus_text" style="text-transform:none;">146.30 MB</FONT></div></td>
</tr>
If you were willing to or have already installed phpQuery, you can use that.
phpQuery::newDocumentFileHTML('htmlpage.html');
echo pq('td:eq(6)')->text();
PHP can interact with the DOM just like JavaScript can. This is vastly superior to parsing the markup, as most people will tell you is the wrong approach anyway:
Loading from an HTML File
// Start by creating a new document
$doc = new DOMDocument();
// I've loaded the table into an external file, and am loading it into the $doc
$doc->loadHTMLFile( 'htmlpage.html' );
// Since you have six table cells, I'm calling up all of them
$cells = $doc->getElementsByTagName("td");
// I'm grabbing the sixth cell's textContent property
echo $cells->item(5)->textContent;
This code will output "146.30 MB" to the screen.
Loading from a String
If you have the HTML stored within a string, you can load that into your document as well. We'll change the method used to load the file, into the method used to load from a string:
$str = "<table><tr><td>Foo</td></tr>...</table>";
$doc->loadHTML( $str );
We would then proceed with the same code as above to select the cells, and show their textContent in the output.
Check out the DOMDocument Class.
I am creating a web application where users can upload/download/view online pdfs. I want to change the name of the pdf file to view in new tab link (like we see in all websites).
Does anyone know how to make any field in the database that gives a link that when user clicks, it opens the pdf in a new tab?
if($result) {
if($result->num_rows == 0) {
echo '<p>There are no files in the database</p>';
}
else {
// Print the top of a table
echo '<table width="100%">
<tr>
<td><b>Name</b></td>
<td><b>type</b></td>
<td><b>Size (bytes)</b></td>
<td><b>Created</b></td>
<td><b> </b></td>
<td><b>view><b/></td>
</tr>';
// Print each file
while($row = $result->fetch_assoc()) {
echo "
<tr>
<td>{$row['name']}</td>
<td>{$row['type']}</td>
<td>{$row['size']}</td>
<td>{$row['created']}</td>
</tr>";
}
// Close table
echo '</table>';
}
// Free the result
$result->free();
}
else
{
echo 'Error! SQL query failed:';
echo "<pre>{$dbLink->error}</pre>";
}
// Close the mysql connection
$dbLink->close();
The solution doesn't have to be within the database.
You can use an anchor tag to redirect the user to the link :
<a href="{$path_to_pdf}" target="_blank" >Click here to view the PDF</a>
Put that in a cell in the table you are generating.
I could be mistaken - but I do believe that the users browser is also a factor here. The browser has to be able to display pdf's. The latest and greatest* browsers have it baked in but user preferences' might also be a factor.
* Let your imagination run wild
You can use target="_blank" in your a tag
PDF
This works in Google Chrome, not sure if it works in other browsers.
You can look here http://www.allaccessliving.com/residences/floorplans#1bed15bath:35A
There's a pdf link and you see for yourself if you inspect that element.
When outputting the link to the PDF, use this:
Visit StackOverflow
setting target will tell the browser to open the link in a new tab.