<html>
<body>
<center>
<form action="upload.php" method="post"
enctype="multipart/form-data">
Select pdf1
<input type="file" name="file1"
id="file1"
accept="application/pdf">
Select pdf2
<input type="file" name="file2"
id="file2"
accept="application/pdf">
Select pdf3
<input type="file" name="file3"
id="file3"
accept="application/pdf">
<p><input type="submit" value="Merge"
name="submit"></p>
</form>
</center>
</body>
</html>
The action="upload.php" attribute send the form data to upload.php script for input processing. The method="post"
attribute instruct the web browser to send input data to web server. The attribute enctype="multipart/form-data" allows user to upload files through the form.
The type="file" attribute allows user to select the file from device storage. Attribute name="file1" is used to reference the form-data after submitting the form.
The id="file1" is most used in a style sheet or JavaScript to manipulate the element with the specific id.
The accept="application/pdf" attribute is used to specifies the type of file that the server accepts in this case the server accepts pdf files.
The type="submit" defines a submit button to submit the form to server. The attribute value="Merge" defines the text on the submit button.
<?php
// Uploads files
// if save button on the form is clicked
if (isset($_POST['submit'])) {
// name of the uploaded file
$fn1 = $_FILES['file1']['name'];
$fn2 = $_FILES['file2']['name'];
$fn3 = $_FILES['file3']['name'];
//Take 1st character of the file
//and append file ext.
//to the 1st character
//Override the previous files
$fnA = substr($fn1,1,1) . "." .
substr($fn1,-3);
$fnB = substr($fn2,1,1) . "." .
substr($fn2,-3);
$fnC = substr($fn3,1,1) . "." .
substr($fn3,-3);
// destination of the file on the server
//Create the path for temporary pdf files
//This path must be created in cPanel
$des1='/home/username/public_html/'.$fnA;
$des2='/home/username/public_html/'.$fnA;
$des3='/home/username/public_html/'.$fnA;
// get the file extension
$ext1 = pathinfo($fname1,
PATHINFO_EXTENSION);
$ext2 = pathinfo($fname2,
PATHINFO_EXTENSION);
$ext3 = pathinfo($fname3,
PATHINFO_EXTENSION);
// Temporary uploads
//directory on the server
$file1 = $_FILES['file1']['tmp_name'];
$file2 = $_FILES['file2']['tmp_name'];
$file3 = $_FILES['file3']['tmp_name'];
$size1 = $_FILES['file1']['size'];
$size2 = $_FILES['file2']['size'];
$size3 = $_FILES['file3']['size'];
//Upload files to the temporary folder
move_uploaded_file($file1, $des1);
move_uploaded_file($file2, $des2);
move_uploaded_file($file3, $des3);
//Delete the previous merged file
//This will explain later
$fdelete = '/home/username/public_html/'
. "mergedPDF.pdf";
unlink($fdelete);
//Validate the values from HTML form
if(!in_array($extension1,['pdf','PDF'])||
!in_array($extension2,['pdf', 'PDF'])||
(!in_array($extension3,['pdf','PDF'])
&& $filename33 != '')) {
$message2 = "You file extension must
be .pdf format, not blank and
upload atleast 2 pdf files!";
} elseif ($_FILES['fileToUpload1']
['size']
> 10000000 ||
$_FILES['fileToUpload2']['size']
> 10000000 ||
$_FILES['fileToUpload3']['size']
> 10000000) {
$message3 = "File size must be less
than 1gigabyte!";
} else {
echo "No error";
}
}
//Delete pdf files after done merging
$fdelete='/home/username/public_html/'.
$fnA;
unlink($fdelete);
$fdelete='/home/username/public_html/'.
$fnB;
unlink($fdelete);
$fdelete='/home/username/public_html/'.
$fnC;
unlink($fdelete);
?>
The (isset($_POST['submit'])) logic code check if the the submit button was clicked by the user.
The next thing the script does fetch the names (using $_FILES['file1']['name'];) of the pdf files uploaded through the html form
and assign them to viariables. Next the script extract the first character and extention of the pdf file name and append them
($fnA = substr($fn1,1,1).".".substr($fn1,-3);). This logic will help us to override the pdf files loaded before.
Next the script create the path where the pdf files will be temporary saved or stored ($des1='/home/username/public_html/'.$fnA;).
The path '/home/username/public_html/' is where all the files (index.html etc.) are stored and will be explain in cPanel.
($ext1=pathinfo($fname1,PATHINFO_EXTENSION);) get the file extention. The ($size1=$_FILES['file1']['size'];) get the file size
or file name and assign it to the variable. The (move_uploaded_file($file1,$des1);) upload or move files to the temporay folder.
The (unlink($fdelete);) delete the specified file. This will delete already merged pdf file that was merged before. The merge
process will be explain in next section. The next twe parts of the scripts validate the file to check if the file is pdf and
size of file is not large. The last part of the script delete uploaded files loaded before.
<?php
// Uploads files
// if save button on the form is clicked
if (isset($_POST['submit'])) {
// name of the uploaded file
$fn1 = $_FILES['file1']['name'];
$fn2 = $_FILES['file2']['name'];
$fn3 = $_FILES['file3']['name'];
//Take 1st character of the file
//and append file ext.
//to the 1st character
//Override the previous files
$fnA = substr($fn1,1,1) . "." .
substr($fn1,-3);
$fnB = substr($fn2,1,1) . "." .
substr($fn2,-3);
$fnC = substr($fn3,1,1) . "." .
substr($fn3,-3);
//Delete previous file
$fdelete='/home/username/public_html/'
. "mergedPDF.pdf";
unlink($fdelete);
//Merge files using Ghostscript
if (isset($_POST['submit'])) {
$fileArray= array($fnA,$fnB,$fnC);
$ddir="/home/username/public_html/";
$outputName = $ddir."mergedPDF.pdf";
$cmd = "gs -q -dNOPAUSE -dBATCH
-sDEVICE=pdfwrite
-sOutputFile=$outputName ";
//Add each pdf file to the
//end of the command
foreach($fileArray as $file) {
$cmd .= $file." ";
}
?>
When the user input data in the form and click submit button the script above get values or names of the files.
Then the script extract the the first character of the name of the file and file extention. Then append the file extention to the first character.
Next the script delete the old pdf file merged before. Next the script create the new merged pdf file and save it to the path. In order for this
to work you must install Ghostscript in your server. In cPanel this works without installing Ghostscript.
<a href="http://www.example.com/ download.php?file=mergedPDF"> Download your file </a>
<?php
//Get the file name from the link
$file = $_GET["file"] . ".pdf";
//Check if the link is clicked
//and download the merged files
if (isset($_GET['file'])) {
$filepath='/home/username/public_html/'
. $file;
if (file_exists($filepath)) {
header('Content-Description:
File Transfer');
header('Content-Type:
application/octet-stream');
header('Content-Disposition:
attachment; filename='
. basename($filepath));
header('Expires: 0');
header('Cache-Control:
must-revalidate');
header('Pragma: public');
header('Content-Length: '.filesize
('/home/username/public_html/'
. $file));
readfile('/home/username/public_html/'
. $file);
}
}
?>
When the user click on the download link the ($_GET["file"] . ".pdf";) fetches the file name from the link.
Then append .pdf to the file name. The script next assigns the results to the variable. The ($filepath) variable create the the directory
with a file name. The next part of the script download your pdf file to your local computer. The value of the file from the downloadable
link must be same name as the merged pdf file named in step 3.