Translate

Saturday 22 November 2014

Uplaod PDF File into Database Using PHP and Javascript

Explaination:
In this tutorial i has explained about the upload the PDF file only using PHP  and Javascript.
SO this coding is easy to understand and very helpfull for the develoers for to code the 
sites.
enctype="multipart/form-data" - The enctype attribute specifies how the form-data should be encoded
when submitting it to the server.
action="" - The name of our PHP page that will be created, shortly.This can be used for after upload 
it work's through in that page which url u gave in that Action. 
method="POST" - Submits data to be processed to a specified resource.
input name="pdf" - uploadedfile is how we will access the file in our PHP script.

Now that we have the right HTML form wto upload the PDF files.

Typically, the PHP file should make a key decision with all uploads: keep the file or not accept. 
the folling files are not accept in the upload form.The including files are:

1.The file is too large and do not accept.

2.If u want to upload Images or CSV file or XLSX fole/Xls is should not upload.
3.There were problems uploading the file and so you can't keep it.


HTML FORM:
<form  action="pdf_upload.php" name="formLogin" method="post" enctype="multipart/form-data" onSubmit="return validate();">
<div class="form-group">
<input type="file" name="pdf" value="" id="file" class="form-control">
</div>
<div class="form-group">
<input type="submit" name="add" class="btn btn-primary">
</div>
</form>

pdf_upload.php:
When the pdf_upload.php file is executed, the uploaded file exists in a temporary storage area on the server. 
If the file is not moved to a different location it will be destroyed! To save our precious file we are going to need to make use of the
 $_FILES associative array.

The $_FILES array is where PHP stores all the information about files. 


   1.We have to POST the values throught the submit button.

   $_FILES['pdf']['name'] - name contains the original path of the user uploaded file.
   $_FILES['pdf']['name'] - tmp_name contains the path to the temporary file that resides on the server. The file should exist on the server in a temporary directory with a temporary name.
 Pdf is the name of the field.Now we can finally start to write a basic PHP upload manager script! Here is how we would get the temporary file name, choose a permanent name, and choose a place to store the file. 
<?php
error_reporting(0);
$conn=mysql_connect("localhost","root","") or die(mysql_error());
$db=mysql_select_db("db_name",$conn)or die(mysql_error());
$main=new main();

if(isset($_POST['add']))
{
if(($_FILES['pdf']['name']!='')) 
{ 
$name=str_replace("'","",$_FILES['pdf']['name']);
$pdf=$name;
$size=$_FILES['pdf']['size'];
$type=$_FILES['pdf']['type'];
$temp=$_FILES['pdf']['tmp_name'];
$target=("../pdf/".$pdf);
move_uploaded_file($temp,$target);
}
else
{
$error="** &nbsp; Check your file type &nbsp;!! &nbsp;**";
}
$insert="insert into file_tbl (id,file) values ('','$pdf')";
if($main->db->query($insert))
{
// header('location:dashboard.php');
}
else
{ 
$error="** &nbsp; Check your Connection &nbsp;!! &nbsp;**";
}
}
?>
Javascript:
<script type="text/javascript">
function validate(){var imgpath=$("#file").val();var imgpath1 = document.getElementById('file').value;if(imgpath1 == ""){alert( "Upload PDF File" );return false;}else{var arr1 = new Array;arr1 = imgpath.split("\\");var len = arr1.length;var img1 = arr1[len-1];var filext = img1.substring(img1.lastIndexOf(".")+1);if(filext == "pdf" || filext == "doc"|| filext == "docx"){}else{alert("Invalid File Format Selected");return false;}}}
</script>
Database Structure:
CREATE TABLE IF NOT EXISTS `file_tbl` (
  `id` int(11) NOT NULL AUTO_INCREMENT,
  `title` varchar(100) NOT NULL,
  `study` varchar(55) NOT NULL,
  `file` varchar(200) NOT NULL,
  PRIMARY KEY (`id`)
) ENGINE=InnoDB  DEFAULT CHARSET=latin1 AUTO_INCREMENT=34 ;


No comments:

Post a Comment