what is the best way the save fpdf into database? - php

I'm new in PHP and i want to saving pdf into database
I would like to know what the best way to save fpdf into database?
I'm using BLOB type just to save the content of pdf but i want to get the file pdf to read into table and see the content when i clicked to the file.
My code to get the content is:
$query = "select nomeUser,email,nomeVoucher,categoria,preco,confirmacao,filePDF
from historico
LIMIT $start, $per_page";
while ($stmt->fetch()) {
if($confirmacao == "a confirmar"){
echo("<tbody >");
echo("<tr><td>$nomeUser</td>");
echo "<td>$email</td>";
echo("<td >$nomeVoucher</td>");
echo("<td >$categoria</td>");
echo("<td>$preco €</td>");
echo("<td>$confirmacao</td>");
filePDF is the name where i save the content of my fpdf
How can i do this?

Related

image from Database to a link in a tweet card (PHP)

I am trying to insert an image from database to twitter card (link) but the links appearing in the tweet without an image
example of the tweet card (link)
tried to make a default image for the tweet card and it work perfectly
but when i try to retrieve the name of the image from the database and put it in the <meta> it shows up as an empty image in twitter
tried also echoing $picture and the path shows up as its in the database : /image.png
<head>
<?php
//identify the user
$un = $_SESSION['active_user'];
//query to get that specific users information
$query = "SELECT * FROM users WHERE username='$un'";
//save the query in result variable
$result = $db->prepare($query);
//excecute the variable result
$result->execute();
//if the result has records in the database save the users info in the variables below
if ($row = $result->fetch())
{
//validation variables
$emptyFields = false;
$error = false;
$success = false;
$n = $row['name'];
$usn = $row['username'];
$picture=$row['picture'];
}
echo "<meta property='twitter:card' content='summary'>";
echo "<meta property='twitter:site' content=''>";
echo "<meta property='twitter:title' content=''>";
echo "<meta property='twitter:description' content=''>";
echo "<meta property='twitter:image' content='https://website.com/".$picture."'>";
echo "<meta name='twitter:creator' content='#abc'>";
echo "<meta name='twitter:text:title' content='website'>";
?>
what am i missing here ? is the way i am retrieving data ? please i would try any suggestion
i do not know how you saved the image to the location or to the database, but based on your question,
Read this and see if you missed something
twitter:image is a url to a unique image representing the content of the page. You should not use a generic image such as your website logo, author photo, or other image that spans multiple pages. Images for this Card support an aspect ratio of 1:1 with minimum dimensions of 144x144 or maximum of 4096x4096 pixels. Images must be less than 5MB in size. The image will be cropped to a square on all platforms. JPG, PNG, WEBP and GIF formats are supported. Only the first frame of an animated GIF will be used. SVG is not supported.
and also one more thing, it seems like you saved the entire or part of the image directory on the database, based on database : /image.png in your question. Note that this is not good practice since you might have to access the image from different pages of your website, and even though its not impossible, it's not worth it, just sve the image name only like image.png

Creating Image file from image stored in mssql as type Image

I have images stored in mssql database . I want to retrieve the data via php and create image file . I have tried numerous possibilities but unable to find any correct solution
Image i have in following format stored in database
0xFFD8FFE000104A46494600010200000100010000FFDB00840001010101010101010101010101010101010101010101010101010101010101010101010101010101010101010101010101010101010
I have tried
$row = mssql_fetch_assoc($result);
$db_img = $row['imgdata'];
$db_img = pack("H" . strlen($db_img), $db_img);
header("Content-Type: image/jpeg");
echo $db_img;
displays black image
Also i have tried
file_put_contents( 'test_image.jpg', $binary_data);
Not Working ....
Also tried ...
imagecreatefromstring("0xFFD8FFE000104A46494600010200000100010000FFDB00840001010101010101010101010101010101010101010101010101010101010101010101010101.........");

code to expand excel column width in php while exporting

I am new to PHP and trying to export some DB data to Excel file as below. But in the output I want the column widths to be adjusted automatically to view the entire content. See the code I am using to export data. I am not using any third party libraries for export.
<?php
require_once("db.php");
$contents="email,time_in,v_id,time_out\n";
$user_query = mysql_query('SELECT * FROM table');
while($row = mysql_fetch_array($user_query))
{
$contents.=$row['email'].",";
$contents.=$row['time_in'].",";
$contents.=$row['v_id'].",";
$contents.=$row['time_out']."\n";
}
// remove html and php tags etc.
$contents = strip_tags($contents);
//header to make force download the file
header("Content-Disposition: attachment; filename=Report".date('d-m-Y').".xls");
print $contents;
?>
Is there any way to format the output ?

Displaying images stored in a database using PHP

For my current assignment I'm trying to store files into the DB and display them. Currently I have no problem storing the files but I can't display them properly. I'm using this in my main form.
<td><a href=getImage.php?id='.$file[0].'" />Resume</a></td>
And this is the query I'm using to get the file out of the DB.
$sql = "SELECT file FROM table WHERE Res_ID=$referenced_ID";
$result = mysqli_query($dbc, $sql);
$row = mysqli_fetch_assoc($result);
header('Content-type: image/jpg');
echo "<img src=\"{$row['file']}\" /><br />";
The result is
…³V!µÔ¢ošweöÿZ–îÌèEÈÎpEJ·˜kä€òþòâ­Gas
È*G¨¥vA¤uEN¤S]‰:ñY“iwØ8‡¥e]ØÝGÑ‘øQÍ!«3¨ÄvÙÁu§]zÕÿOã^ssÌuáY7WP“ÔT6Æà™ëâþÊQË!üi­oe8
9ô5ã?ÛSÇÔ‘RÃ⛘VCBh¹>ϳ=BïÃVwåçÔW#¯|8†ufŠ4ob1PiÞ=q(,>µÐYx®Æ÷¥ò›Ñ5b½äy6«ðÆbͲϡ®fóá–
í„ξ‹’xvîYVEöæ«6§]|²½ýj‡Ï#åËßjv™-k.u\ŠÈ›O–#ó!ЊúÊmcLþ-¹ïšÆÔ4ïkùñE¸ÿ0h±j§‘òù
Ԉ켯íº×ÂÝã/g:»¸âïþ=³-²û?áHÑI3ŒKÙW¨ÍNºŽî-¶_Ã#IÙ¸{Tiû¸>â€3ÿu7#V[àü­š‘í•ùT[9\Ó’ª?oçJ©ù…69Ùxß÷¹©ö¼
|ÊOµ°™W##êàÔ²iw®»–uõ5Eíåˆà«ôÅHä¯"‘\PˆÙŽ>ïÖœÖò'¡÷†^Ž#3Sg+ÀéYžt±uBG­>=H'ÞB>”À¾7ƒè)É#)åsU×S‰‡R>µ"\£ô4‹é"ç=D/ÿ-
š¦¼ô©àuCó'´‰ Ë7çPëÑ¿Z½åÛÌ>\Mk
&ʱœÒ0ê*#)ÏAV怩#Bd`^):ܲ¯ü*îÛERk†^ù¦CiÃsõ¦
ší¹Îj¬“*øhf﶑¬†CqíH’ëëIæsŒŠÐ{=ÍW{
:dÓ¸aÐœýjXîæCÃA:æ”CŽÿe5iG£}h7I7ßCüê¬8èqP–t<Ò…©£¸MÄúbª¼EM'ÚJš‘oPýôϽHˆ2GµZ¶¾T
H/Ò¢yb“§ò¦R9ÓC6,n...
I'm using images as a sample at the moment but the finished php should eventually allow to display PDF documents. If it's any help I'm using phpmyadmin and MySQLi.
As the comments suggest, storing the file location in the database is much better than storing the file's data in the database. BUT if you need to store the file contents in the database for some reason, there are two methods. One is using the HTML data: URL and the other is having a PHP file as the middle man.
For the PHP Middle Man method, look at the answer above mine where it sets the header information and then echo's the file contents. The file extension doesn't matter since browsers refer to the content-type header instead of file extensions.
For the Data URL method, which can be placed directly in your code easily check out http://en.wikipedia.org/wiki/Data_URI_scheme
<?php
// Array of valid Mime Types, prevents possible XSS methods.
$valid_mimes = array(
'image/png',
'image/gif',
'image/jpeg'
);
// Obtain Mime Type using finfo
// Finfo allows for strings instead of file path
$finfo = new finfo(FILEINFO_MIME_TYPE);
$mime = $finfo->buffer($row['file']);
// Check if mime is in our $valid_mimes array
if(!in_array($mime,$valid_mimes)) {
// Handle Error
echo 'Illegal Mime Type: '.$mime;
die();
}
$b64 = base64_encode($row['file']);
echo '<img src="data:'.$mime.';base64,'.$b64.'" />';
?>
You cant output <img src=\ as an output it will corrupt the image file and
Please use getImage.php as a different file to output images or verify that no output is printed before,after or in mid of image else it will corrupt the image.
if(isset($_GET['id'])){
$sql = "SELECT file FROM table WHERE Res_ID=$referenced_ID";
$result = mysqli_query($dbc, $sql);
$row = mysqli_fetch_assoc($result);
header('Content-type: image/jpg');
echo $row['file'];
}

export mysql database table contents on to a PDF file using php

How to export MySQL database table contents on to a PDF file. It has to be displayed in the website as download/print data. When the customer click on that he/she has to get the PDF opened or an option to download the PDF with all the contents of the table using php.
<?php
require('fpdf17/fpdf.php');
$pdf=new FPDF();
$pdf->AddPage();
$pdf->SetFont('Arial','B',10);
$pdf->Ln();
$pdf->Ln();
$pdf->SetFont('times','B',10);
$pdf->Cell(25,7,"Stud ID");
$pdf->Cell(30,7,"Student Name");
$pdf->Cell(40,7,"Address");
$pdf->Cell(30,7,"Class");
$pdf->Cell(30,7,"Phone No");
$pdf->Cell(30,7,"E-mail");
$pdf->Ln();
$pdf->Cell(450,7,"----------------------------------------------------------------------------------------------------------------------------------------------------------------------");
$pdf->Ln();
include ('db.php');
$sql = "SELECT studid,name,address,class,phone,email FROM studinfo";
$result = mysql_query($sql);
while($rows=mysql_fetch_array($result))
{
$studid = $rows[0];
$name = $rows[1];
$address = $rows[2];
$class = $rows[3];
$phone = $rows[4];
$email = $rows[5];
$pdf->Cell(25,7,$studid);
$pdf->Cell(30,7,$name);
$pdf->Cell(40,7,$address);
$pdf->Cell(30,7,$class);
$pdf->Cell(30,7,$phone);
$pdf->Cell(30,7,$email);
$pdf->Ln();
}
$pdf->Output();
?>
You should make use of PDF creator libraries like:
FPDF
TCPDF
EzPDF
All three are easy to learn in the order I've put. The documentation of FPDF and EzPDF are very neat and clean. But TCPDF Documentation is not that readable.
Take a look at http://www.tcpdf.org/. I never heard about out of box way to print mysql table data into PDF, but with TCPDF you can programmatically build a PDF file with table filled with data inside it. It also allows to output document created on the fly easily into a browser.
You can use http://www.tcpdf.org/ to create your own PDF. The examples and the doc tabs will help you ^^ (http://www.tcpdf.org/doc/code/classTCPDF.html for all the methods)
You can create a HTML table will all your data and put it in your PDF using the WriteHTML() method.

Categories