Pdf data type in MySQL
Like always I am sharing new things I learn here on my blog. I was recently working on a requirement for a LAMP stack web application reporting dashboard in which I needed to store – and eventually – retrieve a .pdf file. I have read in several places (this fantastic book is a great resource) that a viable option is storing images or documents (.pdf in this case) in the actual database table as opposed to on the server file system. MySQL has the Show The Newsletter for PHP and MySQL DevelopersReceive a copy of my ebook, “10 MySQL Tips For Everyone”, absolutely free when you subscribe to the OpenLampTech newsletter. Photo by Laika Notebooks on UnsplashSelf-Promotion:If you enjoy the content written here, by all means, share this blog and your favorite post(s) with others who may benefit from or like it as well. Since coffee is my favorite drink, you can even buy me one if you would like! I am using a table named ‘project_pdf’ with 3 columns (see accompanying screenshot) to store the data:
With the below simple HTML web Below is the HTML source code for the above web <link rel="stylesheet" href="https://stackpath.bootstrapcdn.com/bootstrap/4.5.2/css/bootstrap.min.css" integrity="sha384-JcKb8q3iqJ61gNV9KGb8thSsNjpSL0n8PARn9HuZOnIxN0hoP+VmmDGMN5t9UJ0Z" crossorigin="anonymous"/> Discover premium articles, in-depth guides, instructional videos, and much more by joining the “MySQL Learning Tier” membership. You have access to exclusive content unpublished anywhere else with this membership. With new content being added regularly, continue learning MySQL at any level. Learn MySQL! Looking at the code…We will use this PHP code stored in a script prepare($insert_sql); Verify the button is clicked and a file is attachedThe first 1 isset($_POST['submit']) && !empty($_FILES['pdf_file']['name'])) Tip: More validation can be implemented here to verify the file type is an actual .pdf since that file type is what we are expecting to store in the database. Informational: Visit the official PHP online documentation for more information on isset() and empty(). Check PHP $_FILES array for errorsThe 1 $_FILES['pdf_file']['error'] != 0 Related: See the official PHP online documentation for more information on file upload errors. PHP $_POST and $_FILES DataThe
1 $project_name = htmlspecialchars($_POST['project_name']); Related: Read more about POST upload methods in the official online PHP documentation. Read in .pdf binary data and prepare to store in MySQL BLOB column with PHPThe call to 1 $pdf_blob = fopen($file_tmp, "rb") MySQL database connection and prepared statementsFinally, we look at the entire I have all database connection information stored in a separate file named Since we are introducing user-supplied input from the web
INSERT .pdf file into MySQL BLOB column with PHPIf the call to 1 try { Note: It is generally not a good practice to Using the form to store .pdf in MySQL BLOB column with PHPLet’s try out the form and PHP code to verify the upload works. Here is a simple sample .pdf file I created for a demo run: Contents of SamplePDF.pdf document to upload with form.See this screenshot in which I fill in the ‘project name’ field with a generic ‘First Project’ name and attach the SimplePDF.pdf file: Filled out web form with pdf attachment for upload.Upon clicking the ‘Submit’ button, the information is successfully stored in the database and the success message is displayed in the browser: Message displayed in the browser after successful pdf upload.Here is the data saved in the MySQL database table from the successful upload via our web form and the PHP code: The project_pdf table with inserted pdf and project name.It works!!!In the next blog post, I will cover how to retrieve the .pdf file from the database and display it in the browser. If you see anything in the code that I can improve on or any mistake, please let me know via the comments section below. Additional PHP/MySQL ReadingBe sure and visit these other blog posts I have written on PHP and MySQL:
Like what you have read? See anything incorrect? Please comment below and thanks for reading!!! A Call To Action!Thank you for taking the time to read this post. I truly hope you discovered something interesting and enlightening. Please share your findings here, with someone else you know who would get the same value out of it as well. Visit the Portfolio-Projects page to see blog post/technical writing I have completed for clients. To receive email notifications (Never Spam) from this blog (“Digital Owl’s Prose”) for the latest blog posts as they are published, please subscribe (of your own volition) by clicking the ‘Click To Subscribe!’ button in the sidebar on the homepage! (Feel free at any time to review the Digital Owl’s Prose Privacy Policy Page for any questions you may have about: email updates, opt-in, opt-out, contact forms, etc…) Be sure and visit the “Best Of” page for a collection of my best blog posts. Josh Otwell has a passion to study and grow as a SQL Developer and blogger. Other favorite activities find him with his nose buried in a good book, article, or the Linux command line. Among those, he shares a love of tabletop RPG games, reading fantasy novels, and spending time with his wife and two daughters. Disclaimer: The examples presented in this post are hypothetical ideas of how to achieve similar types of results. They are not the utmost best solution(s). The majority, if not all, of the examples provided, is performed on a personal development/learning workstation-environment and should not be considered production quality or ready. Your particular goals and needs may vary. Use those practices that best benefit your needs and goals. Opinions are my own. More ways I can help
Disclosure: Some of this blog post’s services and product links are affiliate links. At no additional cost to you, should you make a purchase by clicking through one of them, I will receive a commission. Can you store PDF in MySQL?A Binary Large Object ( BLOB ) is a MySQL data type that can store binary data such as images, multimedia, and PDF files.
What data type is a PDF file?Portable Document Format (PDF), standardized as ISO 32000, is a file format developed by Adobe in 1992 to present documents, including text formatting and images, in a manner independent of application software, hardware, and operating systems.
Can you store a PDF in a SQL database?Using file table, any documents or PDF or for that matter any file can be stored in the SQL Server.
How to insert PDF file in SQL database?How to add a file(txt, pdf, etc) to a SQL database with Insert.... read the excel file and create output var is datatable.. Use for each row =>put var datatable => use execute query inside it.. Use import arguements and assign row value variables to sql arguments=>Write insert query… hpy learning.. |